Web Development

General discussion


Automate initialization of raw structure

By Mark W. Kaelin Editor ·
The Jan. 15, C++ TechMail discusses the use of raw_initialize to automate initialization of raw structures. Have you been required to initialize a raw structure in your application development? How did you accomplish the task?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Assumes contiguous memory, no float bias

by tehilinski In reply to Automate initialization o ...

My understanding of the C and C++ standards is that member variables of a structure are not guaranteed to be contiguously allocated. For example:
struct RawData {
float x;
int n;
Here, if x and n are not contiguous, then memset will overwrite memory not allocated to the variables.
Also, on some hardware architectures, floats == 0.0 do not have zero in their machine representation, due to a bias value. In this case, filling an array of floats with NULL may not be the same (depending upon the implementation of memset) as assigning zero to the individual variables.

Collapse -

Yes, but...

by John Torjo In reply to Assumes contiguous memory ...

You are true: variables are not guaranteed to be contigously allocated. But in this case, you don't care about the "memory between" (the memory that is not used) - it can be initialized with zeros.

However, you're right about the float. Same applies to double/ long double. In such a (rare) case, you will need a different technique.



Collapse -

A simpler way

by achalk In reply to Automate initialization o ...

Why not just:

struct fred f = {0};


Related Discussions

Related Forums