Does anyone know the best way to clear an array of any info? I can't figure out how to do it.
Does anyone know the best way to clear an array of any info? I can't figure out how to do it.
Depends on what you mean by 'clear'. If you want to simply zero out the binary representation of a data structure you can use memset. But often, this isn't adequate for more complex classes, in which case you can call the destructor, initialize it with a default-constructed object, or, in some cases, call the approprate member function of the object that serves the purpose of 'clearing' the object. Perhaps you can give an example of what kind of data structure you're dealing with (ie: the code in question)?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Define "clear an array of any info"? Do you mean storing marker values (like, 0, or -1, or NULL) or whatever in every element?
At first blush, this sounds like a program-logic thing (either using a marker value, or just keeping track of "there are x values in this array", or just using a dynamic thing like a vector) more than something syntactic.
What do you mean by clear? You mean remove all of its elements? That would depend on how this array is allocated.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Wow. Triple-simultaneous response.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
By clear I mean clear to the point as if no data was ever in there and not simply fill the array with zeros.
That still isn't enough information. Post some code, please.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
There must always be data in an array. There is no way to have "no data ever in there". Even when you have an uninitialized array, there's still data there, you just can't trust it.
As Sebastiani requested, post whatever (small portion of) code is compelling you to ask this question, and there will be answers abound.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
The code is a little long so I'll break it down a bit so that it makes sense.
Code:#include <iostream> #include <fstream> using namespace std; int g = 0; int main () { Main: char File [100]; cout <<"Enter filename and extention:\n"; cin.getline (File, 100); // does whatever the program is meant to // And here is where I need to clear "File" so that it doesnt carry on should I select 1. cout << "Would you like to continue?\nPress 1 for Yes\nPress 2 for No:\n"; cin >> g; if ( g == 1) {goto Main;} else { return EXIT_SUCCESS;};
In that case, 'File' is never used after that point so you don't need to clear it, really. But if you just have some compulsion to do so, you could
1) set the first character to 0
2) use memset
3) use std::fill, or otherwise cycle through the array and set each element to the default-constructed value of char (ie: 0).
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
It doesn't really make sense. Having an array uninitialised essentially means that any read-only access of it gives undefined behaviour.
I take it you want the array File to be uninitialised every time through the loop.
One way is to use a function;
Essentially, the array File is only guaranteed to exist within the scope of do_stuff().Code:#include <iostream> #include <fstream> using namespace std; void do_stuff() { char File [100]; cout <<"Enter filename and extention:\n"; cin.getline (File, 100); // does whatever the program is meant to } int g = 0; int main () { Main: do_stuff(); cout << "Would you like to continue?\nPress 1 for Yes\nPress 2 for No:\n"; cin >> g; if ( g == 1) {goto Main;} else { return EXIT_SUCCESS;}; }
However, I suspect what you really want is for the data that was read in to be cleared, so the data is no longer physically in memory. In that case, simply overwrite the array File with some suitable pre-defined pattern (eg all zeros).
Last edited by grumpy; 06-13-2009 at 05:37 PM.
Since it's a string, the easiest thing to do is set the first element to nul:
If security is a concern (which it probably isn't since this string isn't used for storing passwords), you need to set all the elements to '\0' using std::fill().Code:File[0] = '\0';
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Could be well about missing calls to cin.ignore when mixing input with >> and getline (or failed cin generally) which makes him think array being "full" is the reason why input doesn't succeed the way he expects?...
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.