473,385 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array alignment

If I have

sometype ar[A][b];

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

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

Thanks,

Vu

Nov 14 '05 #1
8 2745
Vu Pham wrote:
If I have

sometype ar[A][b];

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

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


memset(ar, 0, sizeof ar);
Nov 14 '05 #2

"Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <da***@despammed.com> wrote in
message news:ca**********@carabinieri.cs.interbusiness.it. ..
Vu Pham wrote:
If I have

sometype ar[A][b];

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

Nov 14 '05 #3
On Wed, 9 Jun 2004 11:38:15 -0500, "Vu Pham" <vu@sivell.com> wrote:
If I have

sometype ar[A][b];

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>>
Nov 14 '05 #4
kal
"Vu Pham" <vu@sivell.com> wrote in message news:<2i************@uni-berlin.de>...
"Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <da***@despammed.com> wrote in
message news:ca**********@carabinieri.cs.interbusiness.it. ..
Vu Pham wrote:
If I have

sometype ar[A][b];

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!
Nov 14 '05 #5
"Vu Pham" <vu@sivell.com> wrote:
If I have

sometype ar[A][b];

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[i][j], &temp, sizeof **ar);

--
Simon.
Nov 14 '05 #6
Thanks everyboy.

"Vu Pham" <vu@sivell.com> wrote in message
news:2i************@uni-berlin.de...
If I have

sometype ar[A][b];

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

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

Thanks,

Vu


Nov 14 '05 #7
Vu Pham wrote:
If I have

sometype ar[A][b];

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][b] = {{0}};

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

If you were aware, carry on :)

-Peter
Nov 14 '05 #8
??
On Thu, 10 Jun 2004 10:03:33 GMT, "Ralmin" <ne**@ralminNOSPAM.cc>
wrote:
"Vu Pham" <vu@sivell.com> wrote:
If I have

sometype ar[A][b];

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[i][j], &temp, sizeof **ar);


Or just ar[i][j] = temp.

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

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Lidström | last post by:
Hello, I want to have a class that contains only a collection of another class. For example: public __gc class Alignment { public: Alignment(); ... };
12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
31
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.