Jump to content
  • Advertisement
Sign in to follow this  
ff8

struct ..

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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:


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

Share this post


Link to post
Share on other sites
Hi,

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

Share this post


Link to post
Share on other sites
thank you for your fast reply
ok how can i allocate the space for this array?

Share this post


Link to post
Share on other sites
Well, if you are using C, something like this:


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;

Share this post


Link to post
Share on other sites
ew.


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;
};


Share this post


Link to post
Share on other sites
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 ??

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!