Thread: Question about pointers #2

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Question about pointers #2

    As promised here is my next question about pointers as strings. This question relates to how to properly free memory when deleting a pointer or exiting a program.

    Code:
    char* string = new char[8];
    strcpy(string, "letters");
    delete string;
    Will this work? No memory leaks, other problems etc...?

    Code:
    char* function (void)
    {
         char* string = new char[8];
         strcpy(string, "letters");
         return string;
    }
    
    char* astring = new char[8];
    astring = function();
    delete astring;
    Will this work? Will this delete both string and astring? Could I just set astring equal to the return of function without allocating memory first?

    One more question, what does setting a pointer equal to NULL do? I know that NULL is just a #define 0, but does it just clear the address that the pointer points to? Or is there more? Thanks again for the help

  2. #2
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    I believe that you would have to do this-> delete string[]. if you dont inlcude the [], you will just delete the first element and not the remaining ones.
    big146

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Alright, so if I use the delete [] operator instead of the delete operator everything will work? Including the function return example? What about setting the string equal to NULL?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The correct syntax is :

    Code:
    delete [] string;
    Anytime you use operator new with [] you must use delete with []. Simple as that. Setting it to NULL isn't required afterwards but it may help you detect errors if you try to use that pointer again for something else.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char* function (void)
    >{
    > char* string = new char[8];
    > strcpy(string, "letters");
    > return string;
    >}

    >char* astring = new char[8];

    This should be:
    char* astring;

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Swoopy: so you're telling me that I don't need to allocate memory if I am recieving a string from a function return? If I delete astring after recieving the data from function, will that free the memory of string as well?

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You seem to have a handle on it maxhavoc. However, swoopy's code will only work if astring is global.

    With the code you first posted, you allocated memory for astring. Then you set that pointer equal to another block of memory, so you lost that first block you allocated. If you initialize it to NULL instead, there wouldn't be any leaks at all.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    char* string = new char[8];
    strcpy(string, "letters");
    delete string;

    Will this work? No memory leaks, other problems etc...?
    Yes.

    follow with a:

    string = 0; // or NULL, but not '\0'


    char* function (void)
    {
    char* string = new char[8];
    strcpy(string, "letters");
    return string;
    }

    char* astring = new char[8];
    astring = function();
    delete astring;

    Will this work? Will this delete both string and astring? Could I just set astring equal to the return of function without allocating memory first?
    No. It is almost always bad to do things like astring = function() - who owns the memory and who is responsible for freeing it? It can get really complicated!


    One more question, what does setting a pointer equal to NULL do? I know that NULL is just a #define 0, but does it just clear the address that the pointer points to? Or is there more?
    The very first memory address of ram (0) is classified as off-limits, meaning that any application that accesses this address causes the termination of the program with an access violation! very handy for debugging - which is precisely why it is so important to set unused pointers to NULL.
    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;
    }

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by maxhavoc
    Swoopy: so you're telling me that I don't need to allocate memory if I am recieving a string from a function return? If I delete astring after recieving the data from function, will that free the memory of string as well?
    That's right. Since you allocated it in function(), there's no need to allocate it in main(). I guess I was answering your question:
    >Could I just set astring equal to the return of function without allocating memory first?

    And yes, if you delete astring after receiving data from function(), it will free the memory of string.
    Code:
    delete []astring;
    >One more question, what does setting a pointer equal to NULL do?
    It assures the pointer no longer points to memory. It's a good idea to do after delete, just in case you tried to use that pointer again by mistake.
    Code:
    delete []astring;
    astring = NULL;
    Of course if it's at the end of the program, it doesn't matter.
    Last edited by swoopy; 06-18-2004 at 10:49 PM.

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Remember that 'string' is a bad variable name to use, as it is already part of the std namepsace of the header <string>. You should use a more descriptive name too, such as:
    Code:
    char *lpchString; //Indicates that the variable is a pointer to a char
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You should use a more descriptive name too, such as: <snip godawful hungarian notation>
    The name string or str or anything similar is silly anyway. A more descriptive name can easily be found, such as buffer or line or name, so that it more readily depicts how the string is going to be used. The code makes it obvious that the simulated array is to be used as a C-style string, the name should tell readers what the string is logically going to be used for.

    >>Will this work? No memory leaks, other problems etc...?
    >Yes.
    No, it won't. Mixing new[] with delete is undefined, delete[] should be used instead.

    >The very first memory address of ram (0) is classified as off-limits
    A null pointer doesn't have to be address 0, it just has to be some value that would not be a valid pointer.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    In response to those who said my variable names were bad or misleading, I know, this was just an example I wrote to ask my question, I don't actually use names like this in my real programs, but thanks for pointing it out

    Thanks to all of you for helping me with my problem, this is a great community and I'm glad I stumbled across it, thanks all!

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    this is a great community
    Oh we're the best.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Related to the topic of better variable names, can anyone point me to a guide that explains all the prefixes used in variables like 'n' and 'p' and 'lp' and so forth? I've heard the term hungarian notation tossed around, is that what this naming scheme is called?

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've heard the term hungarian notation tossed around, is that what this naming scheme is called?
    Yes, but it isn't widely used outside of Windows programming. The disadvantages far outweigh any advantages that hungarian notation has. You can read more about it here.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM