Dynamic allocation of arrays - problem

Started by
4 comments, last by Medgur 24 years ago
Here''s a quick clip of code: (maptile is a struct) maptile *map; int mapheight = 16, mapwidth = 12; (line 40)map = malloc(sizeof(int) * (mapwidth*mapheight)); So, now the error, I''m compiling with djgpp. 40: ANSI C++ forbids implicit from void * in assignment. I''m almost absolutely sure what this means, but every attempt I''ve made at a work around has failed. Any help would be appreciated, Thanks, -Medgur PS-I have making my first post here a question.
Advertisement
try:

map = (maptile *)malloc(sizeof(maptile)*mapwidth*mapheight);

First thing is, you should cast it yourself, and you should use the size of the actual structure when allocating)

Edited by - olp-fan on 4/13/00 5:14:12 PM
Rock on,- Tom
Hi,

Firstoff, if maptile is a struct, a maptile pointer must point to a maptile struct, so your malloc size had better be right on the money. Something's not right about how you're aproaching this, let us see how you define the internals of the map struct. It looks like maybe you want map to just be an int pointer, instead of a struct - or else maybe you want to be mallocing a pointer *inside* the struct rather than the struct pointer itself.

As to your specific compiler error question, looks like a lack of type cast is what it's complaining about ( ie. map = (maptile *) malloc(sizeof(int) * (mapwidth*mapheight)); ), because malloc returns a void pointer, but map is a maptile pointer - but although that may get rid of the compiler message I don't think you'll be solving anything.

If you want your map to be 12x16 (variables), each "cell" an int, generated at run time, why not use:

typedef int *maptile;
maptile map;
int mapheight = 16, mapwidth = 12;
map = (maptile)malloc(sizeof(int) * (mapwidth*mapheight));

Brian

Edited by - An Irritable Gent on 4/13/00 5:31:02 PM
aig
Hmm, better yet, since you''re using c++, how about following.

struct maptile { ... whatever ... }

int mapwidth = 12, mapheight = 16;
maptile *map = new maptile[mapwidth*mapheight];

... then at the end of the program ...

delete [] map;
Thanks for the replies. I''m going to be heading the C++ route, using new. As for the C route, you''re right, I was missing the (maptile *) the struct contained just a single int, as I was expecting to expand the type later. Of course, I did know that I would have to allocate more.
Guess I should dig deeper into the language of C++.

Thanks,
-Medgur
Of course, "new" and "delete" are the way to go if you''re in C++. I didn''t notice that, and assumed since you were using malloc that you were in C.

Reading your reply cleared up a misunderstanding I had about what was in the maptile struct. Just for the sake of completion, if you ever go back to the malloc method, don''t use "sizeof(int)" in the malloc, use "sizeof(maptile)" instead. It was the "sizeof(int)" that threw me as to what you were trying to accomplish. Sorry. ;-)

Brian
aig

This topic is closed to new replies.

Advertisement