Jump to content
  • Advertisement

Archived

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

coorrae

dynamic arrays Help

This topic is 5140 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

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
Advertisement
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

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
Now that you've seen the hacks, here's proper C++:

std::vector<std::string> array_of_strings


[edited by - fallenang3l on May 25, 2004 4:04:46 PM]

Share this post


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

// 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
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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!