Thread: Whats the best way to clear an array?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    11

    Whats the best way to clear an array?

    Does anyone know the best way to clear an array of any info? I can't figure out how to do it.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Wow. Triple-simultaneous response.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    By clear I mean clear to the point as if no data was ever in there and not simply fill the array with zeros.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    11
    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;};

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hitsuin View Post
    The code is a little long so I'll break it down a bit so that it makes sense.
    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;

    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;};
    }
    Essentially, the array File is only guaranteed to exist within the scope of do_stuff().

    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since it's a string, the easiest thing to do is set the first element to nul:
    Code:
    File[0] = '\0';
    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().
    "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

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Hitsuin View Post
    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 what way does that program (assuming the missing closing brace is added) not do exactly what you want already? The answer to this is rather important

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10: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, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM