• Advertisement
Sign in to follow this  

struct ..

This topic is 4790 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
thank you Fruny for your reply .
is the struct has a construct

Share this post


Link to post
Share on other sites
Quote:
Original post by ff8
what should i do ?


Depends. Tell us what you are trying to do and we might find a clean solution.

Share this post


Link to post
Share on other sites
i think it solved :P
is this the right way?

struct data{
int *a;
};
void test2(Texture &jj){
Texture test;
//test=new data;
test.a=new int [2];
test.a[0]=0;
test.a[1]=1;
jj=test;
//delete [] test->a;
}

Share this post


Link to post
Share on other sites
No, that still looks quite wrong:
Somehow, it makes more sense that test2 should be Texture's constructor:
struct Texture {
int *a;
Texture();
~Texture();
};
Texture::Texture(){
a = new int [2];
a[0] = 0;
a[1] = 1;
}
Texture::~Texture(){
delete a;
}
void main(){
Texture *oo;
oo = new Texture;
cout << oo->a[1];
delete oo;
}

And of course instead of fixing your array at a size of two you would probably pass an integer to the constructor...


btw, this comment by visage is incorrect:
Quote:
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!
You cannot use realloc on an array defined in that way!

Share this post


Link to post
Share on other sites
hello .
the function test2 is just an example what i want to make is a non-sized (dynamic) array i think i fixed it by using Fruny's examples thanks for him and any one help me ;)
this the code :

#include <vector>
using namespace std;
struct data{
data(int size=0):a(size){}
vector<int>a;
};
void main(){
data d(2); //init value
d.a.resize(6);
}



Share this post


Link to post
Share on other sites
My name is visage.
Misinformation is my game!

iMalc is correct. You cannot reallocate static arrays. Here is some text as to why:
Quote:

"And remember: you cannot malloc, callor or realloc *static* arrays. The
following code fragment is a nice loaded shotgun aimed at a toe (yours):
char a[10];
char *b;
...
b=realloc(a, 20);
...
Remember the picture of the memory layout of a C program to see why this is
bad. The array a[10] is stored somewhere in the middle of the "Initialized
Data" segment. It may even be in the middle of the "Text" segment, if the
(optimizing) compiler inlines small static arrays for fast access. But malloc
is supposed to allocate memory *above* the data segment. So the above call to
realloc will fail returning NULL, and if you don't check for allocation
failures you get a segfault the next time you try to access b[]."


I always forget about that little rule. Nice catch, iMalc.

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
My name is visage.
Misinformation is my game!

iMalc is correct. You cannot reallocate static arrays. Here is some text as to why:
Quote:

"And remember: you cannot malloc, callor or realloc *static* arrays. The
following code fragment is a nice loaded shotgun aimed at a toe (yours):
char a[10];
char *b;
...
b=realloc(a, 20);
...
Remember the picture of the memory layout of a C program to see why this is
bad. The array a[10] is stored somewhere in the middle of the "Initialized
Data" segment. It may even be in the middle of the "Text" segment, if the
(optimizing) compiler inlines small static arrays for fast access. But malloc
is supposed to allocate memory *above* the data segment. So the above call to
realloc will fail returning NULL, and if you don't check for allocation
failures you get a segfault the next time you try to access b[]."


I always forget about that little rule. Nice catch, iMalc.
It's great to see that you are someone who recognises that people simply make mistakes and it's no big deal. I kind of felt mean for even bothering to point it out, as it's often hard to give the appearance of a lighthearted tone. ++2u!

Share this post


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

  • Advertisement