Skip to main content
gamedev.net
🔒 Locked

Dynamically Allocated Array Problems

Started by Kustaz 1 reply 300+ views
Original Post
Kustaz
Kustaz
Umm, hey there people, iv been working on a small program of no significance, and im trying to use dynamically allocated arrays. The tutorial iv been working on is a bit dodgy (no offense to the author meant), and I cant get his method to work. Anyway, heres the details on my problem, the snippet and the errors: ********************************THE SOURCE***************************

char ***MapDATA;

*MapDATA = malloc(sizeof(int**) * LayerDepth);

	for(int LayersProcessed = 0; LayersProcessed < LayerDepth; ++LayersProcessed)
	{
	
		MapDATA[LayersProcessed] = malloc(sizeof(int*) * MapSizeInTilesX);

		for(int x = 0; x < MapSizeInTilesX; ++x)
		{

		MapDATA[LayersProcessed][x] = malloc(sizeof(int) * MapSizeInTilesY);

		}

	};

};

*****************************THE ERRORS************************************

Compiling...
Maps.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\GoodCopy\Maps.cpp(101) : error C2440: '=' : cannot convert from 'void *' to 'char ** '
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
C:\Program Files\Microsoft Visual Studio\MyProjects\GoodCopy\Maps.cpp(106) : error C2440: '=' : cannot convert from 'void *' to 'char ** '
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
C:\Program Files\Microsoft Visual Studio\MyProjects\GoodCopy\Maps.cpp(111) : error C2440: '=' : cannot convert from 'void *' to 'char *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.

GoodCopy.exe - 3 error(s), 0 warning(s)

Im trying to create an array of characters, incase thats not obvious. Any help is appreciated, thanks.
TheBluMage
TheBluMage
Well, to start off the structure of your code seems a bit off. In one area I see this:

*MapDATA = malloc(sizeof(int**) * LayerDepth);
Which could also be written:
MapDATA[0] = malloc(sizeof(int**) * LayerDepth);

And then inside a loop:

MapDATA[LayersProcessed] = malloc(sizeof(int*) * MapSizeInTilesX);

...where LayersProcessed starts at zero. This has memory leak written all over it, unless some code has been omitted. Also, why not use new instead of malloc? (I'm assuming you're working in C++ since all the C compilers I've worked with don't complain about implicit pointer conversions.) Then, out of curiosity (nothing wrong with it code-wise) why are you using sizeof(int) to create a char buffer?

Now, if you are working in C and it's still complaining about those conversions, you can solve the compile-time errors by casting the void pointers to the appropriate pointer types:

*MapDATA = (char**) malloc(sizeof(int**) * LayerDepth);...	MapDATA[LayersProcessed] = (char**) malloc(sizeof(int*) * MapSizeInTilesX);...MapDATA[LayersProcessed][x] = (char*) malloc(sizeof(int) * MapSizeInTilesY);


Again, this is a memory leak waiting to happen unless there's some code in between to deallocate the memory allocated in the first malloc call.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.