Odd Error with struct

Started by
39 comments, last by MARS_999 21 years, 2 months ago
Why doesn''t this code work? I get an error that talks about the heap?
  
struct FOO
{
char *a;
char *b;
};

FOO *foo = new FOO[2];

int main(int argc, char *argv[])
{
for(int a = 0; a < 2; a++)
{
foo[a].a = new char[255];
foo[a].b = new char[255];
}

//do stuff file accessing write the stuct FOO after user input

//do stuff file input and store in foo[1]


for(a = 0; a < 2; a++)
{
delete []foo[a].a;
delete []foo[a].b;
}

delete []foo;

return 0;
}
  
I am getting errors on this. If I take out the deletes it doesn''t error out?
Advertisement
Not sure, but try to use some () in those deletes, eg:

delete [](foo[a].a);
delete [](foo[a].b);

Although, I think it would be the same thing as you have. Besides that, I can not spot anything wrong in your code. Have you tested that particular code (that is, without whatever is inbetween the newing and deleting), to make sure it isn''t something there that writes to places it shouldn''t.

Btw, which is the exact error you get.

do you get the error with the code as posted (ie all the ''do stuff'' stuff commented out?)

what is the error code exactly and the explanation from your compilers documentation?
That particular code compiles and runs cleaning ( at least under gcc ). So as others say I believe that you may have troubles where you are ''doing stuff''. Perhaps you are mishandling the pointers some how. As somebody mentioned post the exact error message and check the code you are doing in the comments for possible errors.

-----------
Andrew
why not just have
struct FOO{char a[255];char b[255];};  


and if you dont want to do that, then try the delete without the "[]" things for the chars.

dynamic sigs are fresh!

edit:
ohh dude! i know what wrong. just try
FOO *foo = new FOO[3]; /* 3 or more or whatever */
with the same code as above.

what you are getting (the error):
you are writing to unallocated memory, hapendes all the time. its with the for(..=0

[edited by - aftermath on February 6, 2003 8:44:10 AM]
Rate me up.
quote:afterMath: FOO *foo = new FOO[3]; /* 3 or more or whatever */
with the same code as above.

what you are getting (the error):
you are writing to unallocated memory, hapendes all the time. its with the for(..=0


er... no. The array is of size 2 and the loops go 0, 1 and then exit. What language do you usually use?
When using delete, make sure you put the same number in the [] you have put in when calling new. So write

    for (int a = 0; a < 2; a++){delete [255] foo[a].a;delete [255] foo[a].b;}delete [2] foo;    

and it should work. At least it does on my PC

[My Lousy Page | Kings Of Chaos | Vampires | email.me]

[edited by - LordLethis on February 6, 2003 8:53:18 AM]
[My Lousy Page | Kings Of Chaos | Vampires | [email=lordlethis@hotmail.com]email.me[/email]]
quote:Original post by MARS_999
//do stuff file accessing write the stuct FOO after user input
//do stuff file input and store in foo[1]

Does the error mention anything about "damaged block"? If so, the "stuff" is important. Are you writing past an array boundary?
quote:Original post by LordLethis
When using delete, make sure you put the same number in the [] you have put in when calling new.

That''s not required.
Take out the [] after delete.

[] only required when you have an array of clas instances so that the destructors of each object will get called.

e.g

CMyClass *Cls;
Cls = new CMyClass[ 10 ];
delete [] Cls;

I''ve seen some compilers causing serious errors when doing this:
(Borland C++ Builder 5 to be exact)
char *Test;
Test = new char[ 10 ];
delete [] Test;


If this doesn''t solve your problem, post the code bits that you are omitting.

<$1,000,000 signature goes here>
i sorta disagree with Jaco vd Westhuizen. i''ev many a times made the following mistake:

char *something;something = new char[100];...delete something; 


if i''m not mistaken, i dont get any error reported but when u do a memory check.. lo-and-behold memory leak!

anyway, to my knowledge, all array-declared DMA must be cleared with ''delete [] ''. anyone has proof otherwise?
- To learn, we share... Give some to take some -

This topic is closed to new replies.

Advertisement