using delete on a structure pointer

This is a discussion on using delete on a structure pointer within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! I finished up my C programming book back in December, then took a break. I'm back and studying ...

  1. #1
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    82

    using delete on a structure pointer

    Hi everyone! I finished up my C programming book back in December, then took a break. I'm back and studying C++ now, and this is my first question.

    I completed the exercise successfully, in terms of using "new" for a structure pointer, and all data displayed as it should after user input. What I'm wondering is, just for my own edification, I placed identical display code after I used "delete" to free the memory, and while the string member seems to be emptied out, both of the float members still retain the input data. Is this okay, or am I not using the delete keyword properly with a structure?

    Thanks!

    Code:
    // Do programming exercise 4, but use new to allocate a structure
    // instead of declaring a structure variable. Also, have the program
    // request the pizza diameter before it requests the pizza company name.
    
    #include <iostream>
    using namespace std;
    
    struct Pizza
    {
       char company[30];
       float diameter;
       float weight;
    };
    
    int main()
    {
       const int arraysize = 30;
    
       Pizza * pie = new Pizza;
    
       cout << "\nWhat is the diameter of your pizza in inches? _\b";
       cin >> pie->diameter;
       cin.get();
    
       cout << "\nWhat restaurant did your pizza come from? _\b";
       cin.getline(pie->company, arraysize);
    
       cout << "\nWhat is the weight of your pizza in ounces? _\b";
       cin >> pie->weight;
    
       cout << "\nYour pizza came from " << pie->company << ", it's "
          << pie->diameter << " inches in diameter, and\n"
          << "it weighs " << pie->weight << " ounces." << endl;
    
       delete pie;
    
       cout << "\nAfter freeing up used memory:\n";
       cout << "\nYour pizza came from " << pie->company << ", it's "
          << pie->diameter << " inches in diameter, and\n"
          << "it weighs " << pie->weight << " ounces." << endl;
    
       return 0;
    }
    Steering Wheel: gcc in Code::Blocks 10.05
    Batmobile: Linux Mint 14, running kernel 3.5 and KDE 4.9.2
    Engine: AMD FX-8350 Vishera 8-core
    Dashboard: NVIDIA GTX 570 HD
    Rusty old trailer for hauling games: Windows XP SP3 (in VirtualBox)
    ORV for composing music: Windows 7 Pro 64bit (in VirtualBox)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by spongefreddie
    What I'm wondering is, just for my own edification, I placed identical display code after I used "delete" to free the memory, and while the string member seems to be emptied out, both of the float members still retain the input data. Is this okay, or am I not using the delete keyword properly with a structure?
    It (as in the delete) is okay. Of course, the actual printing after the delete is not okay.

    Note that in practice we would not use dynamic memory allocation the way it is used in your example because just creating the object as a local variable would have sufficed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    82
    I figured that trying to print structure members after the memory was freed was a no-no, since doesn't the status of the pointed-to location become undefined at that point? I was just peeking under the hood, lol.

    Anyway, thank you laserlight for the thumbs up on my use of delete, and the advice regarding the proper use of dynamic memory allocation.
    Steering Wheel: gcc in Code::Blocks 10.05
    Batmobile: Linux Mint 14, running kernel 3.5 and KDE 4.9.2
    Engine: AMD FX-8350 Vishera 8-core
    Dashboard: NVIDIA GTX 570 HD
    Rusty old trailer for hauling games: Windows XP SP3 (in VirtualBox)
    ORV for composing music: Windows 7 Pro 64bit (in VirtualBox)

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    The behavior is indeed undefined. That doesn't mean that by luck the values still happen to be in memory. All that goes on after delete is that the memory is no longer reserved, it doesn't get reset. Take a look at this thread. There was a lot of information detailed about this subject there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Undefined really describes it.
    I made the company a std::string
    and
    used this form of: getline(cin,company);

    and it appropriately gives a segfault...(compiled with gcc-4.6 in fedora 15) .

    Maybe gcc's cin.getline() has a problem with char arrays ?
    [edit: ~was quite foolish of me to say that.!..]
    Last edited by manasij7479; 07-05-2011 at 11:40 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by manasij7479
    Maybe gcc's cin.getline() has a problem with char arrays ?
    What makes you think so?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    When I use getline(cin,company) ; //company being a std::string ...a segfault occurs.

    but in the original code, I get the same output as observed by 'spongefreddie' .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by manasij7479
    When I use getline(cin,company) ; //company being a std::string ...a segfault occurs, admonishing me for using freed up memory.

    but in the original code, I get the same output as observed by 'spongefreddie' .
    Well, you're right to say that you should not get the output observed by spongefreddie. However, you should not get a segfault either. Rather, a flight simulator should be launched for you to play

    Hint: undefined behaviour

    EDIT:
    My guess is that if you were to re-arrange the members of the struct, you may find that it is operator>> instead of cin.getline that is broken
    Last edited by laserlight; 07-05-2011 at 11:30 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    ... Rather, a flight simulator should be launched for you to play
    At a probability factor of 8,767,108 : 1 against...........
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    82
    Quote Originally Posted by AndrewHunter View Post
    The behavior is indeed undefined. That doesn't mean that by luck the values still happen to be in memory. All that goes on after delete is that the memory is no longer reserved, it doesn't get reset. Take a look at this thread. There was a lot of information detailed about this subject there.
    Thank you for that clarification. I was mistakenly thinking that the memory's data would be reset with *different* garbage values, for some crazy reason.
    Steering Wheel: gcc in Code::Blocks 10.05
    Batmobile: Linux Mint 14, running kernel 3.5 and KDE 4.9.2
    Engine: AMD FX-8350 Vishera 8-core
    Dashboard: NVIDIA GTX 570 HD
    Rusty old trailer for hauling games: Windows XP SP3 (in VirtualBox)
    ORV for composing music: Windows 7 Pro 64bit (in VirtualBox)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  2. Delete records in Structure
    By Myrren in forum C Programming
    Replies: 6
    Last Post: 11-19-2010, 01:15 AM
  3. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 10:33 AM
  4. Delete pointer
    By Chris_thf in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2010, 12:22 PM
  5. Replies: 9
    Last Post: 06-13-2009, 02:31 AM

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