Thread: using delete on a structure pointer

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

    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;
    }
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    86
    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.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Maybe gcc's cin.getline() has a problem with char arrays ?
    What makes you think so?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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' .

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,949
    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
    86
    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.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

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, 02:15 AM
  3. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 11: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