struct ..
hello everyone .
i want to use array inside struct but when i do this it give me some warning .
this is the struct :
struct data{
int x[];
};
this is the warning:
warning C4200: nonstandard extension used : zero-sized array in struct/union
[Edited by - ff8 on January 6, 2005 3:00:35 PM]
You are not giving your array any size. You either need to give it a constant size of 1 or greater, or make it a pointer, and then resize it.
Example:
In this example, you have to allocate the space for X using new or malloc.
Or
In this case, only x[0] is accessible, but you could use realloc() to reallocate enough space. Or just allocate enough size off the bat!
-visage
Example:
struct data{int *x;};
In this example, you have to allocate the space for X using new or malloc.
Or
struct data{ int x[1];};
In this case, only x[0] is accessible, but you could use realloc() to reallocate enough space. Or just allocate enough size off the bat!
-visage
Hi,
Arrays must have specified size, just like the warning says [wink], something like:
Otherwise the compiler will have no idea how big that struct actually is and cannot generate any code for functions that alter it.
hth,
CipherCraft
Arrays must have specified size, just like the warning says [wink], something like:
struct data { int X[10];};
Otherwise the compiler will have no idea how big that struct actually is and cannot generate any code for functions that alter it.
hth,
CipherCraft
Well, if you are using C, something like this:
That will give you x[5]. However, you MUST FREE IT. Everything malloced, realloced, and newed must be freed or deleted.
In C++, you have to use new.
struct data{ int *x;};...data D;D.x = (int *)malloc(sizeof(int)*5);...free(D.x);
That will give you x[5]. However, you MUST FREE IT. Everything malloced, realloced, and newed must be freed or deleted.
In C++, you have to use new.
struct data{ int *x;};...data D;D.x = new int[5];...delete[] D.x;
ew.
or even
or even (fixed)
struct data{ std::vector<int> x;};data d;d.x.resize(5);
or even
struct data{ data(int size = 0) : x(size) {}; std::vector<int> x;};data d(5);
or even (fixed)
struct data{ data(int size = 0) : x(new int[size]), size(size) {} ~data() { delete[] x; } data(const data& rhs) : x(new int[rhs.size]), size(rhs.size) {} data& operator=(const data& rhs) { if(this == &rhs) return *this; data tmp(rhs); std::swap(this->x, tmp.x); std::swap(this->size, tmp.size); return *this; } int* x; size_t size;};
sorry but i have one more question
struct data {
int *a;
};
void test2(Texture *jj){
Texture *test;
test=new Texture;
test->a=new int [2];
test->a[0]=0;
test->a[1]=1;
jj=test;
delete [] test->a;
}
void main(){
Texture oo;
oo=new Texture;
test2(oo);
cout<<oo->a[1];
}
i use this way .it's work without any warning or errors but when the program run its hang what is the problem ??
struct data {
int *a;
};
void test2(Texture *jj){
Texture *test;
test=new Texture;
test->a=new int [2];
test->a[0]=0;
test->a[1]=1;
jj=test;
delete [] test->a;
}
void main(){
Texture oo;
oo=new Texture;
test2(oo);
cout<<oo->a[1];
}
i use this way .it's work without any warning or errors but when the program run its hang what is the problem ??
I'm guessing data = Texture.
See what you are oding is that, when you do jjj = test, you are making htem "point" to the smae 'a'.
So when you delete test's a, you are also deleteing jjj's so you can't access it...
See what you are oding is that, when you do jjj = test, you are making htem "point" to the smae 'a'.
So when you delete test's a, you are also deleteing jjj's so you can't access it...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement