Jump to content
  • Advertisement
Sign in to follow this  
WndProc

uninitialized arrays in C++

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

Hi again! Please could anyone help me with this simple C++ problem: I want to declare an array of character strings (so an array of char arrays ;) ) with an undefined length. Here is how simple it works in VB.Net: Dim str() as String ... Redim str(mysize) And how do I do that with C++? My last try was: char* str; ... str = (char*) malloc(mysize*sizeof(char*)); But that gave me only crap. Please help me with that! Greetings WndProc

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by WndProc
Hi again!

Please could anyone help me with this simple C++ problem:

I want to declare an array of character strings (so an array of char arrays ;) ) with an undefined length.


Here is how simple it works in VB.Net:

Dim str() as String
...
Redim str(mysize)


And how do I do that with C++?

My last try was:

char* str;
...
str = (char*) malloc(mysize*sizeof(char*));

But that gave me only crap.


Please help me with that!

Greetings
WndProc
std::string and std::vector:

std::vector<std::string> str;
str.push_back("Hello world");
str.push_back("2nd String");
str.push_back("And another");


for(size_t i=0; i<str.size(); ++i)
{
std::cout << str << std::endl;
}




EDIT: Or, a bit closer to your example:

std::vector<std::string> str;
str.resize(3);
str[0] = "Hello world";
str[1] = "2nd String";
str[2] = "And another";

for(size_t i=0; i<str.size(); ++i)
{
std::cout << str << std::endl;
}




Share this post


Link to post
Share on other sites
Quote:
Original post by WndProc

And how do I do that with C++?

My last try was:

char* str;
...
str = (char*) malloc(mysize*sizeof(char*));

But that gave me only crap.


Please help me with that!

Greetings
WndProc


If you want to define the size of your array once at runtime, using an allocation like you did is the right idea. However in C++ it is much preferable to use new/delete as opposed to malloc/free.

If you require your string to update in length dynamically use std::String, or in some cases is it preferable to use std::vector<char> (which is an array that can dynamically resize itself).

When you say "But that gave me only crap.", I assume you mean you want the array zereod. To that you can use the POD constructor

char* str = new char[SIZE](); // note the round brackets.

[Edited by - fpsgamer on September 25, 2007 1:37:17 PM]

Share this post


Link to post
Share on other sites
Using a vector of strings is definitely the way to go if you can.

If for some reason you can't the old school 'C'ish way would be to malloc an array of pointers then malloc each pointer to the length you need. e.g. something like:


// completely untested btw

// give me three pointers to strings
char ** strings = malloc(3 * sizeof(char *));

// alloc each string
for (i = 0; i < 3; ++i)
{
strings = malloc(/* length of string i, not forgetting the NULL terminator */);
strcpy(strings, /* data for string i */);
}
To free everything you again have to free each string individually and then free the top-level array. You can see why vector<string> is a lot easier and safer...

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
Using a vector of strings is definitely the way to go if you can.

If for some reason you can't the old school 'C'ish way would be to malloc an array of pointers then malloc each pointer to the length you need. e.g. something like:


Since the comparison code is in VB, I'd say that this isn't an issue, and should be avoided with passion.

Because of this:
// completely untested btw
The examples above don't need testing, since they work.


Share this post


Link to post
Share on other sites
Thanks for all the help!

"std::vector<std::string>" really made my day. It works just as it should.

Greetings
WndProc

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!