|< 1 2 >| | 13 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
format_c@manchester:~> cat test.c && g++ test.c -otest && ./test && rm test
#include <stdio.h>
#include <stdlib.h>
int main () {
int array[2] = {1,2};
printf("Aktuelle Groesse: %d\n",sizeof(array));
realloc(*&array,sizeof(int));
printf("Aktuelle Groesse: %d\n",sizeof(array));
return 0;
}
Aktuelle Groesse: 8
Segmentation fault
format_c@manchester:~>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Array {
public:
Array(int def = 0) {
m_array = NULL;
m_def = def;
m_size = m_length = 0;
}
~Array() {
Free();
}
void Free() {
if(m_array != NULL) {
free(m_array);
m_array = NULL;
}
m_size = m_length = 0;
}
unsigned int Length() const {
return m_length;
}
unsigned int Size() const {
return m_size;
}
void Push(int e) {
if(m_length == m_size) {
unsigned int size = m_size ? m_size * 2 : 8;
if(m_array == NULL) {
m_array = (int *)malloc(size);
memset(m_array, m_def, size);
} else {
m_array = (int *)realloc(m_array, size);
memset(m_array+m_size, m_def, size-m_size);
}
m_size = size;
}
m_array[m_length++] = e;
}
int At(undigned int idx) const {
if(idx < m_size) return m_array[idx];
else return m_def;
}
protected:
int m_def;
unsigned int m_size, m_length;
int *m_array;
};
int main(int argc, char *argv[]) {
Array a;
a.Push(1);
a.Push(2);
a.Push(6);
for(unsigned int i = 0; i < a.Length(); i++)
printf("%i: %i\n", i, a.At(i));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
#include <vector>
int main(int argc, char *argv[]) {
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(6);
for(unsigned int i = 0; i < a.size(); i++)
printf("%i: %i\n", i, a.at(i));
return 0;
}
g++ datei.cpp -lncurses
1
2
3
4
5
6
7
$> g++ test.c -lvector
test.c: In function `int main(int, char**)':
test.c:6: error: `vector' undeclared (first use this function)
test.c:6: error: (Each undeclared identifier is reported only once for each
function it appears in.)
test.c:6: error: parse error before `>' token
test.c:7: error: `a' undeclared (first use this function)
std::vector<int> a;
|< 1 2 >| | 13 Einträge, 2 Seiten |