Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Monder

Crash when using malloc

This topic is 5593 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

Can anybody tell me why this line would crash? ObjStart = malloc(sizeof(Node3d_t)); ObjStart is declared as Node3d_t* ObjStart; and Node3d_t is defined as typedef struct Node3d { struct Node3d* next; struct Node3d* prev; obj3d_t* obj; } Node3d_t; I can''t see what would be causing the crash it''s just a simple malloc and ObjStart is declared up at the top of the file so I can''t be trying to load an address into a variable that does not exist or something. Oh I am using C in Dev-C++ BTW.

Share this post


Link to post
Share on other sites
Advertisement
I don''t think this line would compile, since malloc returns a (void*) and you need a (Node3d_t*). Just cast it to avoid compile error :

ObjStart = (Node3d_t*)malloc(sizeof(Node3d_t));

I don''t think THIS line would crash. on the other hand, your ObjStart is unusable unless you init it or something like that. Why? Because next, previous and obj hold random, junk memory. If you ever use these values, you''ll crash. Besides, an

if(next != NULL) { DoSomethingWith( next ); }

will wield no results either, because next hasn''t been initialized as NULL, so it is not NULL even if it points to nothing.

Could you post the first instance of code where ObjStart is being modified/used ?

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more

Share this post


Link to post
Share on other sites
I'm using c so no type casting for me (Oh and I did try using a typecast and it didn't prevent the crash)

As for initializing ObjStart straight after the line that it crashes on(I've verified this by printing a line to my log file before every line cause gdb says it crashes in some system function(probably to do with malloc)) this is straight after it[Obj Start malloc]

ObjEnd = ObjStart;
ObjCurrent = ObjStart;
ObjStart->obj = NULL;
ObjStart->next = NULL;
ObjStart->prev = NULL;

[edited by - Monder on February 1, 2003 10:23:05 AM]

Share this post


Link to post
Share on other sites
What nice logging habits we have here :-)

Stupid me, I didn''t read that last line on your original post, about that C thing.

Maybe it''s a problem you have with your malloc stuff? I''d check (just in case) if sizeof(Node3d_t) is what you really want... And then upload the exe somewhere and get it tested by others on their systems.

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more

Share this post


Link to post
Share on other sites
Fixed it. It was the previous malloc where I allocated a string

instead of

NewExtString = malloc(strlen(ExtString) + 1);

I had

NewExtString = malloc(sizeof(ExtString) + 1);

as ExtString was a char* this meant I was writing to NewExtString beyond the amount I had allocated. So when I did the next malloc is screwed things up. I should've spent more time looking at the code

Oh well. Thanks for your help anyway


[edited by - Monder on February 1, 2003 12:07:51 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
since when can you not typecast in C?

Share this post


Link to post
Share on other sites
what he meant was that he didn''t HAVE TO cast in C ... cause in C, void* will be automatically cast to any pointer type ...

in C ... none of this requires a cast

  
int *x = malloc(sizeof(x));
void *generic = x;
char *c = generic;


which of course is not true in C++, C++ will let you get TO a void * fine, but will never automatically cast FROM a void* (it must be explicit).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!