How do you initialize a dynamic array in C ++?

How to achieve the dynamic equivalent of this initialization of a static array:

char c[2] = {}; // Sets all members to '\0'; 

In other words, create a dynamic array with all the values ​​initialized by the termination character:

 char* c = new char[length]; // how do i amend this? 
+47
c ++ arrays
Jan 08 '10 at 18:10
source share
10 answers
 char* c = new char[length](); 
+92
Jan 08 '10 at 19:09
source share

Two ways:

 char *c = new char[length]; std::fill(c, c + length, INITIAL_VALUE); // just this once, since it char, you could use memset 

Or:

 std::vector<char> c(length, INITIAL_VALUE); 

In my second method, the second parameter defaults to 0, so in your case this is not needed:

 std::vector<char> c(length); 

[Edit: vote for Fred’s answer, char* c = new char[length](); ]

+28
Jan 08 '10 at 18:15
source share

Maybe use std::fill_n() ?

 char* c = new char[length]; std::fill_n(c,length,0); 
+16
Jan 08 '10 at 18:14
source share

C ++ has no special features for this. However, if you use std :: vector instead of an array (as you probably should), you can specify a value to initialize the vector with.

 std::vector <char> v( 100, 42 ); 

creates a vector of size 100 with all values ​​initialized to 42.

+5
Jan 08 '10 at 18:14
source share

The new-expression array form takes only one initializer form: empty () . This, BTW, has the same effect as empty {} in your non-dynamic initialization.

+4
Jan 08 '10 at 19:59
source share

You cannot do this in one line easily. You can do:

 char* c = new char[length]; memset(c, 0, length); 

Or you can overload the new statement:

 void *operator new(size_t size, bool nullify) { void *buf = malloc(size); if (!buf) { // Handle this } memset(buf, '\0', size); return buf; } 

Then you can:

 char* c = new(true) char[length]; 

but

 char* c = new char[length]; 

will support the previous behavior. (Note: if you want all new nullify what they create, you can do it using the same thing, but taking out the bool nullify part).

Please note: if you choose the second path, you must overload the standard new operator (the one that does not have bool), and the delete operator. This is because you are using malloc() , and the standard states that malloc() + delete undefined operations. So you need to overload delete to use free() , and the usual new one is malloc() .

In practice, although all implementations use malloc () / free () internally, therefore, even if you do not, you will most likely not encounter any problems (except that lawyers in the language yell at you)

+3
Jan 08 '10 at 18:14
source share

No internal funds, AFAIK. Use this: memset (c, 0, length);

+1
Jan 08 '10 at 18:14
source share

and implicit comment by many posters => Do not use arrays, use vectors. All the advantages of arrays without any disadvantages. PLUS you get many other goodies

If you don't know STL, read the Josuttis C ++ Standard Library and meyers effective STL

+1
Jan 08 '10 at 18:20
source share

you need to initialize it "manually":

 char* c = new char[length]; for(int i = 0;i<length;i++) c[i]='\0'; 
0
Jan 08 '10 at 18:21
source share

Since C ++ 11 we could use list initialization :

 char* c = new char[length]{}; 

For the aggregate type, aggregate initialization will be performed, which has the same effect as char c[2] = {}; .

0
Apr 03 '16 at 13:26
source share



All Articles