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 8 2768
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);
"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
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>>
"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!
"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.
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
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
??
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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();
...
};
|
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
|
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
|
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;
};
|
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>
| |
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.
|
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.
|
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
*/...
|
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++.
|
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;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |