473,699 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(some type) );

Thanks,

Vu

Nov 14 '05 #1
8 2764
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(some type) );


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

"Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <da***@despamme d.com> wrote in
message news:ca******** **@carabinieri. cs.interbusines s.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(some type) );


memset(ar, 0, sizeof ar);


Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(some type) );"
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.co m> 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(some type) );

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.co m> wrote in message news:<2i******* *****@uni-berlin.de>...
"Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <da***@despamme d.com> wrote in
message news:ca******** **@carabinieri. cs.interbusines s.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(some type) );


memset(ar, 0, sizeof ar);


Thanks, but what I asked is if "memset( ar, 0, A*B*sizeof(some type) );"
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.co m> 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(some type) );


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.co m> 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(some type) );

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(some type) );

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**@ralminNOS PAM.cc>
wrote:
"Vu Pham" <vu@sivell.co m> 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(some type) );
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.ne t
Nov 14 '05 #9

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

Similar topics

3
2555
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
2236
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 increments itself with the size of the array each time it is incremented with 1. like
31
5005
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 a 4-byte integer followed by a stream of variable
10
6686
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
4425
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 but I don't want to. Arrays cannot be assigned in C, but structs can, so I coded the following: #include <stdlib.h>
20
3514
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 *)a2; // now a1=a2, a1=a2, etc.
6
5181
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 = malloc(n*sizeof(r)) and (to a degree) by the surrounding error checking.
1
2921
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 my program the rates array does not get the information. I think my problem is where I am actuall calling the array index, but I am not sure how to do this. Here is my code: /* * MortFrame.java * * Created on February 24, 2008, 7:28 PM */...
5
3788
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
152
9833
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 = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
8628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.