Depends what you want to do. Generally, you can simply set the first character of each string in the array to '\0'. That's sufficient for most purposes.
Main Topics
Browse All TopicsI need to use good 'ole character arrays in a C++ class. Also, this is an embedded system so no dynamic memory allocation.
I've declared the array as a private member:
private:
char mc_log_entries[MAX_NUM_LOG
What is the appropriate way to initialize the entire array?
e.g. ??
for ( size_t i=0; i < MAX_NUM_LOG_MSGS; i++){
strncpy(this->mc_log_entri
}
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can use either strcpy or memcpy or bzero or something like
char arr[10] = { 0 };
Since here you are only writing a single character, you could have as well used
for ( size_t i=0; i < MAX_NUM_LOG_MSGS; i++){
this->mc_log_entries[i]= '\0';
}
This will save you some CPU cycles but beware - only the first char has been set to 0
>>>> I don't think so. The array is defined within a class. The initialization needs to occur at instantiation.
Yes, I missed that. But maybe the array can be made a static member. Then initialization would be
char MyOleClass::mc_log_entries
Note, big array members are only needed if the same array was used in different member functions. If it was used in one member function only it is better to use a local - maybe static - array.
All,
Thanks for the replies.
The array is used by most of the member functions so I'd rather not go static and can't be a local.
Even though it's an embedded system, CPU is not really an issue and the array is only about 500 x 500.
I'm leaning toward the memset method. Is there an advantage or disadvantage to filling the memory area with NULLs?
memset(mc_log_entries, '\0', MAX_NUM_LOG_MSGS * MAX_LOG_MSG_LEN * sizeof(char));
Thanks.
How often will you instantiate the object? If it's infrequent, memset() is probably the cleanest. Initializing 25K to zero is just a heartbeat. But if you're instantiating the item multiple times per second, you might consider just setting the first byte of each line to 0. A bit more coding, but faster.
Kent
>>>> The array is used by most of the member functions so I'd rather not go static and can't be a local.
The only criteria for making a big buffer static or not is whether you have only one instance of the class at one time (singleton) or there might be multiple instances of the class where each has its own logging (?).
Business Accounts
Answer for Membership
by: jkrPosted on 2008-09-04 at 09:37:24ID: 22389614
The easiest way would be to
Select allOpen in new window