473,804 Members | 3,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing an array

I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running
a whole for loop?

Best Regards
Terry
Nov 13 '05 #1
28 16640

"Terry Andersen" <te**@sea.com > schrieb im Newsbeitrag
news:bk******** **@news.net.uni-c.dk...
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running a whole for loop?


memset(Buffer, 0x00, sizeof Buffer);

Robert
Nov 13 '05 #2
Terry Andersen wrote:

I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more?
Without running a whole for loop?


A loop might be best.
There's no single operation that will do it.
Nov 13 '05 #3
Robert Stankowic wrote:

"Terry Andersen" <te**@sea.com > schrieb im Newsbeitrag
news:bk******** **@news.net.uni-c.dk...
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more?

Without running a whole for loop?


memset(Buffer, 0x00, sizeof Buffer);


Not knowing the representation of ((*Buffer)0),
memset() might or might not work right.
Nov 13 '05 #4
On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <te**@sea.com > wrote:
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running
a whole for loop?
#include <string.h>

memset(Buffer, 0, sizeof Buffer);
Best Regards
Terry


--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 13 '05 #5


Terry Andersen wrote:
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running
a whole for loop?

Best Regards
Terry


If you can put it inside a structure, you could create a zero-filled
instance and assign your buffer to that, e.g. this:

#include<stdio. h>
#include<string .h>

#define BUFFSIZE 9

typedef struct {
int Buffer[BUFFSIZE];
} BUFFSTRUCT;

static const BUFFSTRUCT ZeroBuffStruct;

void prtBuf(int buf[])
{
int i;
for (i=0; i<BUFFSIZE; i++)
printf("%d",buf[i]);
printf("\n");
}

int main()
{
BUFFSTRUCT BufStruct;
int *Buffer = BufStruct.Buffe r;

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

Buffer[3] = 4;
prtBuf(Buffer);

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

return 1;
}

produces the output:

Buffer="0000000 0"
Buffer="0004000 0"
Buffer="0000000 0"

If that's non-portable or has other problems, I'm sure someone will let
us know....

Regards,

Ed.

Nov 13 '05 #6
Le*********@td. com (Lew Pitcher) writes:
On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <te**@sea.com > wrote:
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running
a whole for loop?


#include <string.h>

memset(Buffer, 0, sizeof Buffer);


That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?
Nov 13 '05 #7

"pete" <pf*****@mindsp ring.com> schrieb im Newsbeitrag
news:3F******** **@mindspring.c om...
Robert Stankowic wrote:

"Terry Andersen" <te**@sea.com > schrieb im Newsbeitrag
news:bk******** **@news.net.uni-c.dk...
I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more?

Without running a whole for loop?


memset(Buffer, 0x00, sizeof Buffer);


Not knowing the representation of ((*Buffer)0),
memset() might or might not work right.


You are ritght, of course. Somehow the 0x00 made me think of unsigned char.
Shame on me.
Robert
Nov 13 '05 #8
Thomas Pfaff wrote:
Le*********@td. com (Lew Pitcher) writes:

On Mon, 15 Sep 2003 15:26:35 +0200, "Terry Andersen" <te**@sea.com > wrote:

I have an array that I initialize to zero, like:

Buffer[300] = {0x00};

How do I in my code reset this array to all zeros ones more? Without running
a whole for loop?


#include <string.h>

memset(Buffer, 0, sizeof Buffer);

That's not guaranteed to work unless `Buffer' is of type unsigned char, is it?


It will work on any platform where the data type of buffer can be set
to zero in all bytes. If the platform has a data type where the value
of zero is not zeros in all bytes, then it won't work.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #9
pete <pf*****@mindsp ring.com> spoke thus:
Not knowing the representation of ((*Buffer)0),
memset() might or might not work right.


Why is this so? Are you saying that if Buffer were

long Buffer[300];

that memset wouldn't work, even if you did

memset( Buffer, 0, 300 * sizeof(long) );

?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #10

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

Similar topics

10
5439
by: Liz - Newbie | last post by:
Does anyone know how to clear arrays? My C# books talk about creating arrays or talk about using Clear or RemoveAt but these methods don't appear to be available for my array. I have an array of client objects called aClients if I try aClients.RemoveAt I get an error "System.Array" does not contain a definition for "RemoveAt".
65
4248
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the difference is between these and: (3) mylist =
13
2865
by: Adam Honek | last post by:
Instead of the ZeroMemory API what would one use in VB.NET to clean an array using a custom structure? The .clear isn't a member of it by default. Any suggestions? Thanks, Adam
4
1531
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
1
9442
by: Sonnich | last post by:
Hi! How do I clear an array, I mean how do I free up all items in array, so it becomes empty? I have an array which I reuse in a loop, and need to have it emptied when starting. I fill it like this $blah="whatever";
4
3333
by: setiarakesh | last post by:
I have designed a socket Server and developed asynchronous server . It is working fine with 60 Clients which are connecting to ths Server running at Machine (2 GB RAM and OS is Windows 2003 Server)having IP which is Mapped with static IP and Port in firewall . The Programme is working fine with 60 clients and rate if incoing data on the server is 5 -6 string of 1024 byte size are being saved in DataBase. When i try to connect ...
16
1674
by: Ed Bitzer | last post by:
Trying to send groups of email with program using System.Net.Mail. I do not clear MailMessage but repeatedly loop changing only the Bcc entries. Works fine if all addresses are valid. As a simple test I have attempted to send a valid address, then an invalid address which my ISP (Comcast) will reject immediately as "Not our customer". I catch the exception, display and then continue looping to send several additional valid. The balance...
3
1773
by: Martin | last post by:
Is clearing a structure the following way well defined in C89? The structure ACTION contains no floating point or pointer members. Only integral types. My thoughts concern the padding - can and should it be altered? typedef unsigned char Byte; #define MAX_ACTIONS 10 /* ... */
4
1524
by: gobblegob | last post by:
I am trying to clear the array STR but it does not clear it. This sub is in a module. Dim STR As Array 'Dim str(6) As String STR = File.ReadAllLines(Application.StartupPath & "\login\details\" & Login.CboServicePerson.Text & ".pas") ' & (STR(1) & CboText & ".pas")) Form1.txtUser.Text = STR(1) 'textline Form1.txtPosition.Text = STR(2) Form1.txtDepartment.Text = STR(3)
0
9706
marktang
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...
0
10571
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
10326
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...
1
10317
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,...
0
10075
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
6851
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();...
0
5520
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
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.