Resetting an array

This is a discussion on Resetting an array within the C Programming forums, part of the General Programming Boards category; Hi, I initialized the values of the folowing arrays: char userInput[MAXSIZE]; char* cmdv[20]; during the execution of my program and ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Resetting an array

    Hi,

    I initialized the values of the folowing arrays:
    char userInput[MAXSIZE];
    char* cmdv[20];
    during the execution of my program and I want to reset them so that they can be reused as new.
    What's the best way to do this?

    Thanks,

    Canadian

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Depends on what you mean by reset.
    You can use a loop to set all elements to a new value, or you can use ZeroMemory or memset to fill the arrays with a specific value (or 0 in case of ZeroMemory).

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > char * cmdv[20];
    I'd use a loop setting each pointer to NULL, that way if the pointers pointed to any DMA'd memory you could free it.

    > char userInput[MAXSIZE];
    memset(userInput, 0, sizeof(userInput));
    Last edited by zacs7; 11-12-2007 at 02:17 AM.

  4. #4
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by Canadian0469 View Post
    Hi,

    I initialized the values of the folowing arrays:
    char userInput[MAXSIZE];
    char* cmdv[20];
    during the execution of my program and I want to reset them so that they can be reused as new.
    What's the best way to do this?

    Thanks,

    Canadian
    I dont know if we ever need it..
    but if it is needed.. I generally put a nulchar in the first pointer..
    Code:
    userinput[0]='\0';
    length of this string is 0 so its reset
    C's Motto: who cares what it means? I just compile it!!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    For strings, yes, but if you copy other data, then there's no 0 or NULL when the data ends which might be a good thing. It's always safer to zero the whole buffer.

  6. #6
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by Elysia View Post
    For strings, yes, but if you copy other data, then there's no 0 or NULL when the data ends which might be a good thing. It's always safer to zero the whole buffer.
    I have my own stupid reason for these clearing buffer..
    when we allocate memory to a variable.. and dont initialize it to anything it will be garbage value..
    This is something like the same situation, so you treat it as garbage value even if it is exactly not a garbage.. (garbage as it is not useful now, not garbage as its you who generated in your code..)
    anyway.. the method for bookkeeping the array sizes and limits is only by keeping a separate variable, and strings are an exception.. if you want to reset your array.. just tell the bookkeeper that it is reset, and do nothing..

    in fact it really shouldnt matter if you put 0, or NULL or anything.. anyway you wont visit that part of the array by keeping track of what is of our interest by some other variable or a field in the array itself.. (linked lists are an exception, as their condition is to have a null pointer in the end of the list)
    C's Motto: who cares what it means? I just compile it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21