Array question

This is a discussion on Array question within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am just wondering is this the correct way to clear an array? Thanks Code: for (int i ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Array question

    Hi guys I am just wondering is this the correct way to clear an array?

    Thanks


    Code:
    	for (int i =0; i < sizeof(array); i++)
    		{
    		array[i]='\0';
    	}

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Not really. Your problem is sizeof(array). That returns bytes. So if it's an integer array with 10 elements, you're now looping while i < 40. That's a segmentation error.

    Either define your size elsewhere, or do sizeof(array) / sizeof(datatype) where datatype is the datatype of the array.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Or if you don't want to care about the underlying datatype, let sizeof do the dirty work:
    Code:
    sizeof array / sizeof *array
    But for a presumed array of char, this will end up the same. The real question becomes, why do you feel the need to clear an array (of char)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Since you are using a simple array, you had to declare its size in your program, so you already know the size of the array, and you should use that in your loop. For instance:
    Code:
    const int size = 10;
    char myArray[size] = "hello";
    ...
    ...
    for(int i=0; i<size; i++)
    {
    }
    Also, as was mentioned, you might not need to erase the array because you can do this:
    Code:
    char myArray[10] = "hello";
    strcpy(myArray, "hi");
    cout<<myArray<<endl;
    If you were using string types, you could do this:
    Code:
    string myStr = "hello world";
    myStr = "";
    Last edited by 7stud; 02-22-2006 at 10:20 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    The sizeof calculation presumes that the array is actually in scope. If all you have is a pointer to your array (say in a function), then you need another way of working out it's length (say a parameter).

    Look at fgets() for a good example of how to pass array+length as parameters.

  6. #6
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by SlyMaelstrom
    Not really. Your problem is sizeof(array). That returns bytes. So if it's an integer array with 10 elements, you're now looping while i < 40. That's a segmentation error.
    .
    What is a segmentation error does that mean the loop is just taking longer or does it mean it will cause an error in the program?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    A segmentation fault is when you try to access memory outside of your program's memory. If you try to access an array with an index greater than or equal to it's size, you may get a segementation fault and your program will crash.
    Sent from my iPadŽ

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A segmentation fault is generally the result of accessing memory that you aren't supposed to. The OS takes offense to this antisocial behavior and kills the offending program.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the replies so if I implement the code as follows:

    Would this also cause the error if for example only 2 elements of the array index have been used?
    Code:
    const int size=1000;
    char temp [size];
    			
    for (int i =0; i < size; i++)
    {
    	temp[i]='\0';
    }
    Last edited by 182; 02-23-2006 at 03:40 PM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Nope, that's fine.

    Though if you're looking to do it right after you declare it, you should also know you can do this on initialization:

    Code:
    char arry[1000] = { '\0' };
    Sent from my iPadŽ

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you could also use memset, or define an inline ZeroMemory
    Code:
    inline void ZeroMemory(void* pMem, size_t size)
    {
        memset(pMem, 0, size);
    }
    better yet, just use a vector or a boost.array (which has already been accepted into the standard library for the next version of C++)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks so how would you do this when using a pointer such as the following or with a pointer is it ok to use sizeof (array)?

    const int no=10;
    char array[no];
    char *ptr = array;

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Would this also cause the error if for example only 2 elements of the array index have been used?
    You're not getting it. When you declare an array like this:
    Code:
    const int size=1000;
    char temp [size];
    You are reserving memory for 1,000 ints. So, somewhere in memory there are 1000 slots laid out side by side for you to store your ints in. All the memory is there for your array whether you use it or not. However, you cannot use the memory outside of that 1,000 int block. So, if you do something like this:
    Code:
    temp[1000] = 100;
    you will have made a serious error and what happens after that is anybody's guess--hopefully your program will crash so that you know something is wrong.

    Initializing an array means you set it equal to something in the same line that you declare it. To initialize an entire char array to all \0's, you can simply do this:
    Code:
    char temp[size] = {0};
    That will actually work for an array of any type. If you set any array equal to {0}, then the entire array will be filled with the appropriate 0 value for that type.

    with a pointer is it ok to use sizeof (array)?
    A pointer is a variable that stores an address in memory, and an address in memory looks something like this:

    567A9F20

    So when you use sizeof() on a pointer, you will get the size of that address. However, if you use sizeof() on an array name, you will get the number of bytes the whole array occupies.

    I would suggest that you forget you ever heard of sizeof() because it is most likely something you shouldn't be using.
    Last edited by 7stud; 02-23-2006 at 09:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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