• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

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

# Memory Allocation in ANSI C

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

## Recommended Posts

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?

##### Share on other sites
malloc should be able to take a non-constant argument. How exactly are you using malloc, and what error are you getting?

##### Share on other sites
  error C2057: expected constant expression

##### Share on other sites
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.

##### Share on other sites
Yeah some code would help us here.

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

##### Share on other sites
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..

##### Share on other sites
It''s sizeof that''s generating the error, not malloc. You really need to simplify this line of code.

##### Share on other sites
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