array alignment

V

Vu Pham

If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );

Thanks,

Vu
 
G

Guest

Vu said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );


memset(ar, 0, sizeof ar);
 
V

Vu Pham

Dario (drinking coï¬?ee in the oï¬fceâ?¦) said:
Vu said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );


memset(ar, 0, sizeof ar);


Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(sometype) );"
works. I just want to understand how array aligned in different platforms.

The reason is sometimes I cast my array to , say , char * and access it
from there. Besides the problem of little/big endian, I would like to know
if I will meet any other problem.

Vu
 
B

Barry Schwarz

If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );

It will set every byte of the array to all bits 0. Whether or not all
bits 0 is an acceptable value for an object of type sometype is
implementation dependent. Even if it is an acceptable value, whether
that value is the same as zero may also be implementation dependent
(or even without meaning if, for example, sometype is a struct).


<<Remove the del for email>>
 
K

kal

Vu Pham said:
Dario (drinking coï¬?ee in the oï¬fceâ?¦) said:
Vu said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );


memset(ar, 0, sizeof ar);


Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(sometype) );"
works. I just want to understand how array aligned in different platforms.


I have heard of the "alignment" considerations only with regard
to structures, and not arrays.

However, it seems that structures may have padding _at the end_
to facilitate allocations of arrays of such structures. This
implies that arrays have no "holes" between its elements.

Please see: http://www.eskimo.com/~scs/C-faq/q2.13.html

But your question is valid in that does the standard REQUIRE
array elements to be sequentially allocated in memory? In
other words, can the whole array be treated as an array of
a different type whose size in bytes is the same?

I would guess "yes" but I really don't know the answer to that.
Perhaps someone with a good knowledge of the standard will post.

Thanks for the question!
 
R

Ralmin

Vu Pham said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );


It sets every every bit of every byte in the array to zero. While that
should be fine when sometype is any integer type, it is not necessarily
correct for a floating-point or pointer type. The all-bits-zero may not be a
valid representation of the value 0.0 or a null pointer.

By the way,
A * B * sizeof (sometype)
A * B * sizeof **ar
A * sizeof *ar
sizeof ar
are all equivalent.

I believe this will correctly zero out every element of ar, no matter what
type sometype is:

size_t i, j;
sometype temp = {0};
for(i = 0; i < sizeof ar / sizeof *ar; i++)
for(j = 0; j < sizeof *ar / sizeof **ar; j++)
memcpy(&ar[j], &temp, sizeof **ar);
 
P

Peter Ammon

Vu said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );

Thanks,

Vu


If you're not aware, you can write this:

sometype ar[A] = {{0}};

which will initialize the elements to zero, NULL, or whatever the analog
for sometype is.

If you were aware, carry on :)

-Peter
 
D

Dave Thompson

??
Vu Pham said:
If I have

sometype ar[A];

then does the following command clear the whole array to zero ?

memset( ar, 0, A*B*sizeof(sometype) );


It sets every every bit of every byte in the array to zero. While that
should be fine when sometype is any integer type, it is not necessarily


To be picky, it's actually guaranteed in C99 only for unsigned char
(pure binary, no sign, no padding) and in C89 only for arguably all
char flavors, but DR 263 has been accepted to guarantee it for all
integer types, so in practice yes it should be safe.
correct for a floating-point or pointer type. The all-bits-zero may not be a
valid representation of the value 0.0 or a null pointer.

By the way,
A * B * sizeof (sometype)
A * B * sizeof **ar
A * sizeof *ar
sizeof ar
are all equivalent.
Subtle point: the last two are; the first two are equivalent to each
other, but possibly not to the last two if A*B has a (common) type
narrower than (size_t and) its value i.e. overflows.
I believe this will correctly zero out every element of ar, no matter what
type sometype is:

size_t i, j;
sometype temp = {0};

Note: which is OK for a scalar or aggregate (or union) sometype,
whereas = 0 is (currently, pace Dan Pop) only good for a scalar.
for(i = 0; i < sizeof ar / sizeof *ar; i++)
for(j = 0; j < sizeof *ar / sizeof **ar; j++)
memcpy(&ar[j], &temp, sizeof **ar);


Or just ar[j] = temp.

Or several variants; sometimes I prefer to do a whole row at a time.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top