开发者

memset on array of structures in C++

开发者 https://www.devze.com 2022-12-30 01:26 出处:网络
I have another memset question. It appears as if the code I am editing may have some issues (or it\'s not done the same way in different files)

I have another memset question. It appears as if the code I am editing may have some issues (or it's not done the same way in different files)

A::LRM las[9]; //A and LRM are both structures with BOOLS and INTS
memset(&las, 0, sizeof(las));

typedef Sec SecArray[16];
SecArray rad_array;
memset(rad_array, 0, sizeof(SecArray));

The second example appears to be correct because rad_array is the same as the first position in the array. Then the sizeof(SecArray)) would make sense. The first one doesn't seem correct to me. All 开发者_JAVA技巧structs are just BOOLs and INTS nothing dynamic in them.

My understanding from my other post about memset was that it followed this format.

memset("pointer to object", "what to set it to", "size of object")

Can anyone tell me what exactly is going on here if I am incorrect with my theory.


Both calls to memset are correct. Both sizeof(las) (or just sizeof las) and sizeof(SecArray) will return the size of the entire array.

If you are worried about the first argument, then again, both will work. The pointer to the entire array (&las) or the pointer to the first element (rad_array in this context) will work equally well with memset.

In general, with regard to sizeof usage, I would recommend using the first approach, since it is type-independent. A good programming practice is to avoid mentioning type names in your statements, i.e. keep type names restricted to declarations as much as possible.

As for the first argument, in case of an array it is a matter of preference, but in general I would say that if you have an object of type T

T t;

and you want to fill it with zeros by using memset, you'd normally do it as

memset(&t, 0, sizeof t);

I don't see why an array should be an exception from that rule (especially if the exact nature of the type is hidden behind a typedef-name). The above memset(&t, ... will work regardless of whether T is an array type, a struct type or any other type. I don't see why one should suddenly drop the & just because T is an array type. Quite the opposite, I'd keep that & to keep the code as type-independent as possible.

Finally, in C++ in both cases the better way to do it would be not to use memset at all and just do

A::LRM las[9] = {};
SecArray rad_array = {};

instead.


You need to specify the total length of memory that should be set.
memset is a C function, that knows nothing about arguments. The actual meaning of the arguments is

memset("pointer to memory", "what to set each byte to", "number of bytes to set")

So this is actually better:

A::LRM las[9]; //A and LRM are both structures with BOOLS and INTS
memset(&las, 0, sizeof(A::LRM)*9);

typedef Sec SecArray[16];
SecArray rad_array;
memset(rad_array, 0, sizeof(Sec)*16);

Both examples you provided will actually work, but they are fragile, since its is easy for an array to decay to a pointer, after which it will not work as expected (for example, if you passed the array into a function which does the memset, or switched to dynamic allocation using new).


The last argument to memset is not the size of the object, it's the size of the "array" of objects pointed by "pointer to object".

Let's say you have:

memset(ptr, value, length);

memset will set all bytes between ptr and ptr+sizeof(*ptr)*length-1 to value.


Your first example will work, but I recommend:

A::LRM las[9]; 
memset(las, 0, sizeof(las)); // <== no ampersand needed

Note that it doesn't matter what A is, A::LRM is the datatype.

BUT

You should not initialize structs that way, because although today it's nothing but BOOLs and ints, tomorrow it might be, well, anything. If someone adds a class to A::LRM, memset() will overwrite its private data and you'll never know until it's too late.


memset fills the memory with zero bytes. It's not, in general, the same as initializing members to zero. So long as we're sticking to standard ISO C++ (or C, for that matter), the only guarantee you have is that all-bits-0 is a valid char or unsigned char value equal to 0. Even for integer types, this doesn't necessary have to be true, as they are allowed to have padding outside of value bits, and all-zero for those bits can be a trap representation.

It's definitely not a proper way to initialize a float or double to 0.0, or a pointer to null. There are existing conformant implementations for which all-bits-zero isn't value 0 for those types.

Anyway, your code can be written both much shorter, and in a fully portable way, by using an empty aggregate initializer:

A::LRM las[9] = {};

This will value-initialize the array - which, in turn, zero-initializes all elements - which, for POD structs, zero-initializes all fields.

Of course, this assumes that A::LRM is a POD struct. If it's not, then memset is even more likely to break things, though, and you really need to just use its constructor, and assign to any fields said constructor didn't initialize.

0

精彩评论

暂无评论...
验证码 换一张
取 消