Clearing an array

T

Terry Andersen

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
 
R

Robert Stankowic

Terry Andersen said:
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
 
P

pete

Terry said:
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.
 
L

Lew Pitcher

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')
 
P

pete

Robert said:
Terry Andersen said:
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.
 
E

Ed Morton

Terry said:
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);
printf("\n");
}

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

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

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

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

return 1;
}

produces the output:

Buffer="00000000"
Buffer="00040000"
Buffer="00000000"

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

Regards,

Ed.
 
T

Thomas Pfaff

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?
 
R

Robert Stankowic

pete said:
Robert said:
Terry Andersen said:
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
 
T

Thomas Matthews

Thomas said:
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.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Christopher Benson-Manica

pete said:
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) );

?
 
L

Lew Pitcher

[email protected] (Lew Pitcher) said:
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?

You are correct, of course.

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

(Opinions expressed are my own, not my employers')
 
J

Joe Wright

Ed said:
Terry said:
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);
printf("\n");
}

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

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

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

BufStruct = ZeroBuffStruct;
prtBuf(Buffer);

return 1;
}

produces the output:

Buffer="00000000"
Buffer="00040000"
Buffer="00000000"

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

Regards,

Ed.


You're crazy! I love the way your mind works. Really! How about this..

#include <stdio.h>

#define SA 10

typedef struct arr {
int arr[SA];
} arr;

arr st_arr;


int main(void) {
int array[SA] = { 0 }; /* initialize array */
printf("%5d\n", array[0]); /* show that array[0] == 0 */
array[0] = SA; /* change it to something else */
printf("%5d\n", array[0]); /* show the change */
printf("%5d\n", st_arr.arr[0]); /* show that the struct was 0 */
printf("%5lu%5lu\n", sizeof array, sizeof st_arr);
*((arr *)array) = st_arr; /* assign the struct to the local array */
printf("%5d\n", array[0]); /* show that it worked */
return 0;
}
C makes arrays difficult. Their expression results in a pointer to its
first element, they cannot be passed by value. Shucks. But what of a
struct consisting of an array? You can do anything with the struct that
you can with the array. What I have not realized until today is, you can
assign the 'value' of a struct (the whole thing) to a local array of the
same size (carefully), as above.

*((arr *)array) = st_arr; /* assign the struct to the local array */

This works (I think) because array in this context is pointer. Casting
it to a struct pointer and dereferencing it creates perfect lvalue
target for the assignment of a struct.

Have I been asleep all these years? Have I made a new discovery? Thanks
Ed.
 
T

Thomas Pfaff

Joe Wright said:
C makes arrays difficult. Their expression results in a pointer to its
first element, they cannot be passed by value.

Well since the value of an array is a pointer to its initial element,
I'd say arrays are indeed being passed by value ;-)
 
M

Martin Dickopp

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

As you correctly point out elsewhere in this thread, it is guaranteed to
work for all integer types according to Defect Report #263. :)

Martin
 
E

Emmanuel Delahaye

Terry Andersen said:
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?

The portable way for any type is:

static mytype_s const z[300] = {0};
memcpy(Buffer, z, sizeof Buffer); /* <string.h> */

If the array is of type unsigned char, you can use:

memset (Buffer, 0, sizeof Buffer);

With other types, the result is undefined.
 
E

Emmanuel Delahaye

pete said:
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) );

This is nuts. You want 'sizeof Buffer'. BTW, it's still wrong, because
nothing in the standard says that all-bits-to-0 is the correct representation
of 0.0 (or -0.0, +0.0, who knows...)
 
D

Default User

Martin said:
As you correctly point out elsewhere in this thread, it is guaranteed to
work for all integer types according to Defect Report #263. :)


How do you arrive at the conclusion that a Defect Report guarantees
anything of the sort?




Brian Rodenborn
 
G

Glen Herrmannsfeldt

Emmanuel Delahaye said:
pete said:
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) );

This is nuts. You want 'sizeof Buffer'. BTW, it's still wrong, because
nothing in the standard says that all-bits-to-0 is the correct representation
of 0.0 (or -0.0, +0.0, who knows...)

Nothing in the standard, but computer designers tend to like it that way.

Does anyone know of a machine where it isn't true? (comp.arch added.)

-- glen
 

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

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top