Jump to content
  • Advertisement

Archived

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

Medgur

Dynamic allocation of arrays - problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!