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'; }
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'; }
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Ž
Or if you don't want to care about the underlying datatype, let sizeof do the dirty work: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)?Code:sizeof array / sizeof *array
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.*
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:
Also, as was mentioned, you might not need to erase the array because you can do this:Code:const int size = 10; char myArray[size] = "hello"; ... ... for(int i=0; i<size; i++) { }
If you were using string types, you could do this:Code:char myArray[10] = "hello"; strcpy(myArray, "hi"); cout<<myArray<<endl;
Code:string myStr = "hello world"; myStr = "";
Last edited by 7stud; 02-22-2006 at 11:20 PM.
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.
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?Originally Posted by SlyMaelstrom
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Ž
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.*
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 04:40 PM.
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Ž
you could also use memset, or define an inline ZeroMemory
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++)Code:inline void ZeroMemory(void* pMem, size_t size) { memset(pMem, 0, size); }
"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?
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;
You're not getting it. When you declare an array like this:Would this also cause the error if for example only 2 elements of the array index have been used?
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:const int size=1000; char temp [size];
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.Code:temp[1000] = 100;
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:
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.Code:char temp[size] = {0};
A pointer is a variable that stores an address in memory, and an address in memory looks something like this:with a pointer is it ok to use sizeof (array)?
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 10:19 PM.