426,178 Members | 2,144 Online
Need help? Post your question and get tips & solutions from a community of 426,178 IT Pros & Developers. It's quick & easy.

# Clearing an array

 P: n/a 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 Replies

 P: n/a "Terry Andersen" 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

 P: n/a 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

 P: n/a Robert Stankowic wrote: "Terry Andersen" 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

 P: n/a On Mon, 15 Sep 2003 15:26:35 +0200, "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 runninga whole for loop? #include memset(Buffer, 0, sizeof Buffer); Best RegardsTerry -- Lew Pitcher IT Consultant, Enterprise Technology Solutions Toronto Dominion Bank Financial Group (Opinions expressed are my own, not my employers') Nov 13 '05 #5

 P: n/a 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 #include #define BUFFSIZE 9 typedef struct { int Buffer[BUFFSIZE]; } BUFFSTRUCT; static const BUFFSTRUCT ZeroBuffStruct; void prtBuf(int buf[]) { int i; for (i=0; i

 P: n/a Le*********@td.com (Lew Pitcher) writes: On Mon, 15 Sep 2003 15:26:35 +0200, "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 runninga whole for loop? #include memset(Buffer, 0, sizeof Buffer); That's not guaranteed to work unless `Buffer' is of type unsigned char, is it? Nov 13 '05 #7

 P: n/a "pete" schrieb im Newsbeitrag news:3F**********@mindspring.com... Robert Stankowic wrote: "Terry Andersen" 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

 P: n/a Thomas Pfaff wrote: Le*********@td.com (Lew Pitcher) writes:On Mon, 15 Sep 2003 15:26:35 +0200, "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 runninga whole for loop? #include 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 Nov 13 '05 #9

 P: n/a pete 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)cyberspace.org | Nov 13 '05 #10

 P: n/a Christopher Benson-Manica writes: pete 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) ); See `Defect Report #263': http://wwwold.dkuug.dk/JTC1/SC22/WG1...ocs/dr_263.htm Nov 13 '05 #11

 P: n/a On 15 Sep 2003 17:13:24 +0200, Thomas Pfaff wrote: Le*********@td.com (Lew Pitcher) writes: On Mon, 15 Sep 2003 15:26:35 +0200, "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? #include 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') Nov 13 '05 #12

 P: n/a Ed Morton wrote: 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 #include #define BUFFSIZE 9 typedef struct { int Buffer[BUFFSIZE]; } BUFFSTRUCT; static const BUFFSTRUCT ZeroBuffStruct; void prtBuf(int buf[]) { int i; for (i=0; i #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. -- Joe Wright mailto:jo********@earthlink.net "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Nov 13 '05 #13

 P: n/a Joe Wright writes: 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 ;-) Nov 13 '05 #14

 P: n/a Thomas Pfaff writes: Le*********@td.com (Lew Pitcher) writes: #include memset(Buffer, 0, sizeof Buffer); 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 Nov 13 '05 #15

 P: n/a In 'comp.lang.c', "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? The portable way for any type is: static mytype_s const z[300] = {0}; memcpy(Buffer, z, sizeof Buffer); /* */ If the array is of type unsigned char, you can use: memset (Buffer, 0, sizeof Buffer); With other types, the result is undefined. -- -ed- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #16

 P: n/a In 'comp.lang.c', Christopher Benson-Manica wrote: pete 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) ); 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...) -- -ed- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #17

 P: n/a Martin Dickopp wrote: 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 Nov 13 '05 #18

 P: n/a "Emmanuel Delahaye" wrote in message news:Xn***************************@130.133.1.4... In 'comp.lang.c', Christopher Benson-Manica wrote: pete 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) ); 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 Nov 13 '05 #19

 P: n/a Default User writes: Martin Dickopp wrote: 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? It has been repeatedly claimed in comp.std.c that the Committee's response to a Defect Report is authoritative, for example in <3D**************@wizard.net> or . Is this not the case? Martin Nov 13 '05 #20

 P: n/a On 16 Sep 2003 01:40:30 +0200, Martin Dickopp wrote in comp.lang.c: Default User writes: Martin Dickopp wrote: 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? It has been repeatedly claimed in comp.std.c that the Committee's response to a Defect Report is authoritative, for example in <3D**************@wizard.net> or . Is this not the case? Martin But this is not an authoritative response, it is a _suggestion_ by one member for a TC. It has obviously not been approved, or it would not be merely a suggestion. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq Nov 13 '05 #21

 P: n/a On 15 Sep 2003 20:04:37 GMT, Emmanuel Delahaye wrote in comp.lang.c: In 'comp.lang.c', "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? The portable way for any type is: static mytype_s const z[300] = {0}; memcpy(Buffer, z, sizeof Buffer); /* */ If the array is of type unsigned char, you can use: memset (Buffer, 0, sizeof Buffer); With other types, the result is undefined. No, how many times do I need to repeat this. It is not undefined for any type of char (signed, unsigned, or plain). It is not undefined for any exact-width types defined in . In fact, it is not only not undefined but guaranteed to produce all 0 _values_ for any signed or unsigned integer type provided only that the unsigned type does not have any padding bits. Even if the corresponding signed type does have padding bits. The reason that the standard guarantees this is left as an exercise to the reader (or the reader can look up the three or so other posts I have made about within the past week alone). And any platform that has padding bits in its unsigned integer types is actually not worth worrying about anymore. Unless somebody can name one that is still in production AND has a C99 implementation. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq Nov 13 '05 #22

 P: n/a Glen Herrmannsfeldt wrote: "Emmanuel Delahaye" wrote in message .... snip ... 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.) The well known DeathStation 3000. -- Replies should be to the newsgroup Chuck Falconer, on vacation. Nov 13 '05 #23

 P: n/a Jack Klein wrote: On 16 Sep 2003 01:40:30 +0200, Martin Dickopp wrote in comp.lang.c: It has been repeatedly claimed in comp.std.c that the Committee's response to a Defect Report is authoritative, for example in <3D**************@wizard.net> or . Is this not the case? But this is not an authoritative response, it is a _suggestion_ by one member for a TC. It has obviously not been approved, or it would not be merely a suggestion. I'm confused on this one, because they have it marked Closed, with no indication of anything added to a TC. Brian Rodenborn Nov 13 '05 #24

 P: n/a Thomas Pfaff writes: Joe Wright writes: 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 ;-) No, the value of an array consists of the values of its elements. The name of an array object, in most expression contexts, is implicitly converted to a pointer to the the array's first element; that conversion results in a distinct value. -- Keith Thompson (The_Other_Keith) ks*@cts.com San Diego Supercomputer Center <*> Schroedinger does Shakespeare: "To be *and* not to be" Nov 13 '05 #25

 P: n/a In 'comp.lang.c', LibraryUser wrote: > 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.) The well known DeathStation 3000. You meant DS 9000 ! -- -ed- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #26

 P: n/a "Emmanuel Delahaye" wrote in message news:Xn***************************@130.133.1.4... In 'comp.lang.c', LibraryUser wrote: > 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.) The well known DeathStation 3000. You meant DS 9000 ! So, what is the floating point representation for +0.0 on the DS9000? How many different sizes are available? -- glen Nov 13 '05 #27

 P: n/a Glen Herrmannsfeldt wrote: "Emmanuel Delahaye" wrote in message LibraryUser wrote:> > 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.) The well known DeathStation 3000. You meant DS 9000 ! So, what is the floating point representation for +0.0 on the DS9000? How many different sizes are available? That comes under the heading of trade secrets / proprietary information / national security etc. If I told you these things I would have to kill you. However the DS9000 can be trusted to adhere to the C89 and C99 standards, i.e. it will never emit nasal demons, cause explosions, etc. on correct coding and input data. -- Replies should be to the newsgroup Chuck Falconer, on vacation. Nov 13 '05 #28

 P: n/a On Mon, 15 Sep 2003 17:20:41 GMT, Joe Wright wrote: typedef struct arr { int arr[SA]; } arr; arr st_arr; int array[SA]; 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. I think technically this is illegal (UB) because you are accessing an object using a type (lvalue) other than its correct (here, declared) type or character, or one of the minor variations allowed in 6.5p7; at least I think that the fifth exception there is intended only to allow *sub*objects of an aggregate to be accessed by (as part of) an access to that aggregate, as is clearly necessary, not to allow a subobject to be accessed *as* the aggregate, but that could be clearer. Theoretically it could fail because the struct 'st_arr' may contain padding after the only element arr, while 'array' must not, and struct assignment may (and usually does) copy padding. In practice I think the chance of this is about equal to that of winning the lottery while being hit by a meteorite. It could also screw up aggressive type-aware optimization, leading to use of stale cached values, although on current systems I know of int[10] seems a rather poor candidate for caching. So, yes it probably does work. But it's much cleaner to just define your objects of the struct type, arr, in the first place, and assign or pass or return them; you can still easily use the array member. - David.Thompson1 at worldnet.att.net Nov 13 '05 #29

### This discussion thread is closed

Replies have been disabled for this discussion.