From: "-hs-" Newsgroups: comp.os.msdos.djgpp Subject: Re: DJGPP malloc problem Date: Sat, 4 Mar 2000 14:31:40 +0100 Lines: 169 Message-ID: <89r37v$2as4$1@news5.isdnet.net> References: <89n24n$5rq$1 AT news DOT koti DOT tpo DOT fi> NNTP-Posting-Host: s111.paris-28.cybercable.fr Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit X-Trace: news5.isdnet.net 952176703 76676 212.198.28.111 (4 Mar 2000 13:31:43 GMT) X-Complaints-To: abuse AT isdnet DOT net NNTP-Posting-Date: 4 Mar 2000 13:31:43 GMT X-Newsreader: Microsoft Outlook Express 4.72.3110.5 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Toni Riikonen a écrit dans le message <89n24n$5rq$1 AT news DOT koti DOT tpo DOT fi>... >OK, now i come in here. Finnish coders couldn't help me with this problem. >And the problem is this. > >I'm creating software-3D-engine with (of course) DJGPP v2, and i use rhide >as my IDE. >So, the main problem is this.. >I have structures for my 3D-objects, vertices, faces, matrixes, and so on... >I have to allocate memory for my objects face_structure. > >Ok here are the structures that i'm using: > >------------------------------------------- >// Here are my structures (new_def.h) >typedef float matrix_type[3][3]; > >typedef struct vertex_type { > signed int x,y,z; > float rx,ry,rz; > unsigned int u,v; >} vertex_type; > >typedef struct face_type { > vertex_type vertex[3]; >} face_type; > >typedef struct object_type { > signed int x,y,z; > float rx,ry,rz; > float ax,ay,az; > face_type *face; > unsigned int faces; > matrix_type object_matrix, world_matrix; >} object_type; >-------------------------------------------- > >Ok, these should be fine, don't you think?? >Now, i have a init-part in my main-program.. >It's like this: You need to include for the use of malloc() and for printf() and for gotoxy() (btw, pointless here) >void InitObject(object_type *obj, int faces) style point : I should write void InitObject(object_type *const pObj, int faces) >{ > obj->face = (face_type *)malloc( sizeof(face_type) * faces); There absolutely no need for casting the return of malloc(). It is a void *. What you really what is this (whatever the type) obj->face =malloc( sizeof *obj->face * faces); > if(obj->face == NULL) exit(1); This is a very brutal way of handling a memory error. Better to return an error indicator, test it and let the user to decide what to do... personally (OO style) I prefer to design C-objects like this : object_type * InitObject(int faces); It means that a return of NULL is an error. > object.faces = faces; Probably a typo (better to copy&past). ITYM: obj->faces = faces; >} >int main() >{ > object_type object; > InitObject(&object, 50); // 1 arg: Object 2: arg: Number of faces >in object > free(object.face); good practice is if(object.face) { free(object.face); object.face=NULL; } and test against NULL (with the assert() macro for example) before use. > return 0; >} > >So, this code WORKS very well, if it is just like this, and i don't do I am not sure... >anything with my allocated structures.. > >BUT, when i'm trying to, for example, print something out of these >structures, my computer crashes, or boots up! Most of the structure elements have never been initialized. Anything can happen. (No prob with Borland 3.1). However, I dont want to try with DJGPP... >This is what i'm trying to do.. > >---------------------------------------------- >int main() >{ > int a; > object_type object; > InitObject(&object, 50); > for(a = 0;a < object.faces; a++) { > gotoxy(1,1+a); > printf("%1.1f\n",object.face[a].vertex[0].rx); > } > free(object.face); IMO, better to design a KillObject() function. The user is not interested by such details... > return 0; >} >-------------------------------------------------- > >OK, i know that malloc doesn't zero the allocated arrays, so there are lots >of different values in it. Ah you know it ! So what are you expecting ? Magic ? >I've heard that DJGPP expands it's datatype to 4-byte lenght, is this true? >Does it affect my program? What do you mean ? The size of the data are returned by the 'sizeof' macro. Check it if you want, but it has no relationship with your problem. >Please i NEED YOUR HELP ON THIS ONE!!!, PLEEEASEEEE help, meeee!! >I'm far behind schedule because of this problem.. Bad organization... -- -hs- CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html ISO-C Library: http://www.dinkum.com/htm_cl "It's specified. But anyone who writes code like that should be transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC