Dynamic allocate string array

H

huohaodian

Hi,

I have a char * mFileList[];

how can i dynamically allocate it to save some strings?

Thanks in advance
 
H

huohaodian

Hi,

I have a char * mFileList[];

how can i dynamically allocate it to save some strings? I'd like to allocate it to and array of POINTERS to char strings, some thing like char *mFileList[1000]

Thanks in advance
 
V

Victor Bazarov

[..]
I tried char * mFileList[] = new char[index]; but got a syntax error.

char **mFileList = new char*[size];

But let me reiterate, do NOT presume managing dynamic memory is
easy or recommended. Try to stick to standard containers before
you learn enough. Do

std::vector<std::string> mFileList(size);

It's basically all you need, trust me.

V
 
A

Alf P. Steinbach

* Victor Bazarov:
[..]
I tried char * mFileList[] = new char[index]; but got a syntax error.

char **mFileList = new char*[size];

But let me reiterate, do NOT presume managing dynamic memory is
easy or recommended. Try to stick to standard containers before
you learn enough.

Just a nit: I think you mean "until" you learn enough. "Before" you learn
enough implies, as I read it, that by sticking to standard containers, one may
avoid the catastrophe of learning enough about dynamic memory management. :)

Cheers,

- Alf
 
H

huohaodian

[..]
I tried char * mFileList[] = new char[index]; but got a syntax error.

char **mFileList = new char*[size];

But let me reiterate, do NOT presume managing dynamic memory is
easy or recommended. Try to stick to standard containers before
you learn enough. Do

std::vector<std::string> mFileList(size);

It's basically all you need, trust me.

V

Thanks a lot for the help
 
D

Default User

Hi,

I have a char * mFileList[];

This is an invalid declaration. You must either have an initializer
list or give that array a dimension.
how can i dynamically allocate it to save some strings?

Do what Victor said. Use the tools that have been developed to aid the
programmer.




Brian
 
J

Jim Langston

Hi,

I have a char * mFileList[];

how can i dynamically allocate it to save some strings?

Thanks in advance

If possible, try std::vector<std::string> mFileList;
If not possible, try std::vector<char*> mFileList;

The std::vector should be as effective as anything manaul, such as new
char*[count] and with a known value you can do:

std::vector<char*> mFileList( count );
Which will create a dynamic array of count elements of char*.

Still, std::vector<std::string> is much prefered, but it really depeneds on
your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,504
Latest member
SherryFerr

Latest Threads

Top