Archived

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

Crash when using malloc

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

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 on other sites
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!

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 on other sites
What nice logging habits we have here :-)

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!

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

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

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 on other sites
since when can you not typecast in C?

Share on other sites
quote:
Original post by Anonymous Poster
since when can you not typecast in C?

c = (type) variable;
c++ = type (variable)

:D

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).

1. 1
2. 2
JoeJ
17
3. 3
4. 4
frob
11
5. 5

• 13
• 16
• 13
• 20
• 13
• Forum Statistics

• Total Topics
632181
• Total Posts
3004625

×