Memory Allocation in ANSI C

Started by
6 comments, last by Galileo430 23 years, 4 months ago
I am writeing a game app ment to run on a UNIX server so I have to use ANSI C. Everytime malloc expects a constant expressions. Anyone know a command that doesn''t?
------------------------------------------------------------I wrote the best video game ever, then I woke up...
Advertisement
malloc should be able to take a non-constant argument. How exactly are you using malloc, and what error are you getting?
  error C2057: expected constant expression  
------------------------------------------------------------I wrote the best video game ever, then I woke up...
I''m quite certain either the error is on the line before it, or you''re using an improper syntax while calling the variable. Please post the offending line of code (plus say about 5 lines before & 5 lines after). We can''t tell what''s wrong from the information you''ve given us.
Yeah some code would help us here.

i use something like,
int *myInt = (int*)malloc(sizeof(int));

nope,

here is the REALY screwy call I have to make.

  global[nGifNumber]->entry = (struct COLORTABLE *)malloc( sizeof(struct RGBTRIPLET[gdata[nGifNumber].nGCTSize]) );// Here are the errors I get.error C2057: expected constant expressionwarning C4047: ''='' : ''struct RGBTRIPLET *[1]'' differs in levels of indirection from ''struct COLORTABLE *''error C2106: ''='' : left operand must be l-value  


Remind me to hurt compuserve very much for their wonderful GIF format..
------------------------------------------------------------I wrote the best video game ever, then I woke up...
It''s sizeof that''s generating the error, not malloc. You really need to simplify this line of code.
I''m a mite confused at what you''re calculating the sizeof()...but I think that you''ve got a structure defined called RGBTRIPLET - have you typedef''ed it something like this ?
typedef struct {         BYTE    r, g,  b;} RGBTRIPLET; 

If you have I think what you should do is:
  global[nGifNumber]->entry = (struct COLORTABLE *)malloc( ( sizeof(RGBTRIPLET) * gdata[nGifNumber].nGCTSize );  


If when you typedef''ed the RGBTRIPLET (it''s not a standard typedef is it ?) you should have the name at the end, if you haven''t, then IMO you aught to do it as I illustrated at the top of this post

This topic is closed to new replies.

Advertisement