tags:

views:

270

answers:

8
char * msg = new char[65546];

want to initialize to 0 for all of them. what is the best way to do this in C++?

+2  A: 
memset(msg, 0, 65546)
Eric Petroelje
Remember to `#include <cstring>`
Magnus Hoff
Is sizeof(char) guaranteed to be 1? I've never seen it not be, but unless it was guaranteed by the standard, I'd use `memset(msg,0,65546*sizeof(char));` just to be sure.
Paul Tomblin
@Paul: `sizeof(char)` is guaranteed to be 1.
Magnus Hoff
@Magnus Hoff: The size of a char is 1 on *some* implementations, but it is depending on the implementation. It is true though that the Visual c++ implementation use a size of 1 byte.
TommyA
@TommyA: sizeof(char) == 1 according to the standard. I don't have a copy of the official standard, but the C++03 draft standard says, "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1." (5.3.3)
Fred Larson
@TommyA: I do have a copy of the standard, and it says exactly what @Fred says it says. Of course, it doesn't have to be 8 bits...
Mike Seymour
My mistake Mike, I confused the char and byte size here.
TommyA
@Tommy: Well strictly speaking "char" and "byte" are exactly the same in C++. Octet is used to refer to a set of 8 bits.
GMan
+3  A: 

This method uses the 'C' memset function, and is very fast (avoids a char-by-char loop).

const uint size = 65546;
char* msg = new char[size];
memset(reinterpret_cast<void*>(msg), 0, size);
Steve Guidi
no need for the cast, implicit T* to void* is allowed in c++, just not implicit void* to T*
Evan Teran
... not to mention that the proper cast here would be `static_cast`.
avakar
And `std::fill` is much better anyway. Any modern compiler will use `memset` when it can, no use in being inconsistent.
GMan
A: 

memset or calloc.

joefis
+18  A: 
char * msg = new char[65546]();

It's known as value-initialisation, and was introduced in C++03. If you happen to find yourself trapped in a previous decade, then you'll need to use std::fill() (or memset() if you want to pretend it's C).

Note that this won't work for any value other than zero. I think C++0x will offer a way to do that, but I'm a bit behind the times so I can't comment on that.

UPDATE: it seems my ruminations on the past and future of the language aren't entirely accurate; see the comments for corrections.

Mike Seymour
+1 for the ideal solution :-).
Evan Teran
This works very well as 0 is the default integral value for a `char`.
Steve Guidi
+1 for doing it the "OO" way.
Eric Petroelje
@Eric, that has nothing to do with OOP :S
Peter Alexander
It wasn't really "introduced in C++03". The `()` initializer was present in the original C++98 as well. While the concept of value-initialization was indeed introduced in C++03, it has no relation to this specific case. `new char[N]()` is required to produce a zero-initialized array in C++98 as well.
AndreyT
@AndreyT: thanks, I didn't know that.
Mike Seymour
I believe neither C++0x provides a way to initialize *all* elements to a specific value. It allows `new char[65546]{1, 2, 3}` - but all remaining elements will still be initialized to `0`.
Johannes Schaub - litb
You guys allergic to std::string or vector<char>? I see little point in using heap-allocated char buffers over these alternatives and no point to encourage otherwise as the user asked for the best C++ way.
@stinky: Give the user a chance to decide.
Jacob
@Peter - I guess not, but when I look at the syntax I see it "constructing" a char with a default value - as opposed to the C-ish usage of memset.
Eric Petroelje
+-0, I consider myself quite an experienced c++ programmer and I wouldn't understand that line without a comment. Use std::fill or memset instead.
Viktor Sehr
+8  A: 

The "most C++" way to do this would be to use std::fill.

std::fill(msg, msg + 65546, 0);
Peter Alexander
Viktor Sehr
+3  A: 

what is the best way to do this in C++?

Because you asked it this way:

std::string msg(65546, 0); // all characters will be set to 0

Or:

std::vector<char> msg(65546); // all characters will be initialized to 0

If you are working with C functions which accept char* or const char*, then you can do:

some_c_function(&msg[0]);

You can also use the c_str() method on std::string if it accepts const char* or data().

The benefit of this approach is that you can do everything you want to do with a dynamically allocating char buffer but more safely, flexibly, and sometimes even more efficiently (avoiding the need to recompute string length linearly, e.g.). Best of all, you don't have to free the memory allocated manually, as the destructor will do this for you.

+1 for taking a step back from the question and suggesting how it probably should be done.
Mike Seymour
+3  A: 

Absent a really good reason to do otherwise, I'd probably use:

std::vector<char> msg(65546, '\0');
Jerry Coffin
+1 as the user asked for the best C++ way. :-D
A: 

You can use a for loop. but don't forget the last char must be a null character !

char * msg = new char[65546];
for(int i=0;i<65545;i++)
{
    msg[i]='0';
}
msg[65545]='\0';
Xpero