Archived

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

coorrae

dynamic arrays Help

Recommended Posts

coorrae    115
I have this const max=99; int amount; char* names[max]; I need to allocate an array with a variable amount I can do it with int arrays like this int * updated=new int(amount); I tried this but it doesnt work char** names = new char*(amount); I need a new array of char*''s

Share this post


Link to post
Share on other sites
bah    100
This is how it can be done in ANSI C

char** names = (char**) malloc( sizeof(char*) * amount );

Note that you''ll have to include stdlib.h or malloc.h

Share this post


Link to post
Share on other sites
fooman    122

char** bla = new char*[amount];
//if you want to set up variablesized arrays 'in' this, just do

for(int i=0;i<amount;i++)
bla[i] = new char[something];


if you start using thw hwap, you should implement a memory leak tracker, either as incode, or using a codechecking outstanding programm for it.

[edited by - fooman on May 25, 2004 3:36:37 PM]

Share this post


Link to post
Share on other sites
coorrae    115
ok thanks. this one works without includes

char** names = new char*[vertexcount];

how do I free them all?

delete[] names; gives a runtime error

Share this post


Link to post
Share on other sites
OmniBrain    148
I am still not sure what you want.

do you want an array of pointers to char (your last posting), or an array of char (like i understood your original post: "I need to allocate an array with a variable amount")?

Share this post


Link to post
Share on other sites
fooman    122

// for the underlaying arrays

for(int i=0;i<amount;i++)
delete names[i];
// for the array itself:

delete names;

//edit: @fallenang3l:

std::vector<std::vector<char> > bla;
// or

std::vector<char *> bla;
//we still dont know if he wants strings or just some variable amount of char^^




[edited by - fooman on May 26, 2004 3:18:08 AM]

Share this post


Link to post
Share on other sites
coorrae    115
thanks
this works

for(int i=0;i<amount;i++)
delete names[i];




whats the difference between

int* stack=new int(amount);

and

int* stack=new int[amount];



how do I free this kind?
i tried
delete stack;
delete[] stack;
delete stack[];



[edited by - coorrae on May 27, 2004 7:19:26 PM]

[edited by - coorrae on May 27, 2004 7:21:46 PM]

Share this post


Link to post
Share on other sites
Xai    1848

int sizeOfInnerArrays = 5;
int numberOfArrays = 3;

// this created an array of arrays

int **arrayOfArrayOfInts = new int*[numberOfArrays];
for(int i=0; i<numberOfArrays; ++i)
{
arrayOfArrayOfInts[i] = new int[sizeOfInnerArrays];
}

// these lines set and read a value

arrayOfArrayOfInts[2][1] = 50;
int temp = arrayOfArrayOfInts[2][1];

// this deletes an array of arrays

int **arrayOfArrayOfInts = new int*[numberOfArrays];
for(int i=0; i<numberOfArrays; ++i)
{
delete[] arrayOfArrayOfInts[i];
}
delete[] arrayOfArrayOfInts;


but in C++, if you CAN use the std STL classes, I highly recommend it ... this list:

vector, list, deque, set, map, pair, string

will server you VERY well in the long run.

Share this post


Link to post
Share on other sites
JohnyB    122
quote:

whats the difference between

int* stack=new int(amount);

and

int* stack=new int[amount];



In your first example you allocate memory for a single int then amount is assigned to it. In your second example you allocate memory not for one but an array of ints which equals the size of amount. You could also have a combination of the two let's say you want to allocate memory for an array of classes and call the constructor then you would do something like this:

OBJ* obj = new OBJ[amount](constructor);

To delete a single object you just call "delete object" if you want to delete a single array then "delete [] object"


[edited by - JohnyB on May 27, 2004 7:04:20 PM]

Share this post


Link to post
Share on other sites
coorrae    115
well that sorts everything. project turned in. thanks.

I wish there was an option in the IDE to always break when writing on unallocated memory

Share this post


Link to post
Share on other sites
henrym    103
quote:
Original post by coorrae
well that sorts everything. project turned in. thanks.

I wish there was an option in the IDE to always break when writing on unallocated memory


Sadly though, you''ll only hear about stuff like that if it causes something to crash. e.g if a process trys to use the data you''ve just written to and doesn''t like it.

That''s generally why you''d want to stay away from things such as dynamic memory and pointers until you understand them fully.

Share this post


Link to post
Share on other sites