Advertisement

How do I delete an array: Map[1000][1000] and make a new one Map[400][16]?

Started by July 27, 2000 12:41 PM
6 comments, last by MindWipe 24 years, 4 months ago
How do I delete an array: Map[1000][1000] and make a new one Map[400][16]? Please help. MindWipe
"To some its a six-pack, to me it's a support group."
Well, i would think that you could use free() and malloc() in c, or ''new'' and ''delete'' in c++.

Martee
Magnum Games
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
free(map[1000][1000]);

map = (int)malloc(400*16);

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
with:

map = (int)malloc(400*16);

wouldn''t you get map[400*16], and not map[400][16]??

----------
Mail me, Drago's OpenGL Website
osu!
// to destroy...
for (int i = 0; i < 1000; i++)
delete [] map;<br>delete [] map;<br>…<br><br>400*16 = sizeof array [400][16]<br><br>here's the thepory.. you have 400 rows times 16 columns in each row.. that makes 400*16 elements…<br><br>gotta catch the train.. later<br><br>Edit: Dark, you can allocate ANY type of array (meaning of any dimension)!!!<br><br>——————————-<br>That's just my 200 bucks' worth!<br><BR>..-=gLaDiAtOr=-..<br><br>Edited by - Gladiator on July 27, 2000 6:03:20 PM
You cannot allocate a dynamic memory block [1000][1000]
Allocating this type of block would require

pPointer = new [1000 * 1000]

this will create an array [1000000] instead of [1000][1000]

you can simple delete this array using

delete[] pPointer

If you want to create an array with [1000][1000]... you will need to allocate it statically... in other words...

int Array[1000][1000]

this WILL create an integer array that can be indexed using

Array[..][..]

however... this is a bad idea since static allocation will
(1) Increase the .EXE file size considerably
(2) Not allow the programmer to deallocate memory for this array


Hope this helps enough.

Greetings from Dark

(Mail me at mitolah@hotmail.com)


ICQ: 130925152Email: e.j.folkertsma@student.utwente.nl
Advertisement
I forgot to mention

If you have to reallocate memory simply allocate memory again

so in code that would be

    int *pInt;pInt = new int [1000 * 1000];........ Do something here....delete[] pInt;pInt = new int [400 * 30];........ Do something here....delete[] pInt; // do not forget to free the newly allocated memory block    


Hope this answers your question completely

ICQ: 130925152Email: e.j.folkertsma@student.utwente.nl
(same code as other post)


// whatever you want to use, or load from a file, etc.
int width=200, height=15;

// create an array that holds 200x15 = 3000 tiles
int** map = new int*[width];
for( int x=0; x < width; x++ )
map[x] = new int[height];

// now to access a tile, we...
map[5][7];
map[9][2];

// delete the array
for( int x=0; x < width; x++ )
delete[] map[x];
delete[] map;




- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement