Thread: seg fault while deteting the dynamically allocated memory

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    seg fault while deteting the dynamically allocated memory

    Heres the piece of code I wrote to gather student details into a dynamically allocated array of structs. But when I try to delete the memory, it gives me a seg fault. I used print statements to make sure that it is reaching the delete[] part and crashing there.

    Code:
    #include <iostream>
    #include <string>
    #include "students.h"
    
    using namespace std;
    
    int main(){
    
            unsigned int number_of_students = 0;
    
            //Gather student details
            cin >> number_of_students;
    
            //read student information
            students *student_details = new students[number_of_students];
            read_students(number_of_students, student_details);
    
            delete(student_details);
    
            return 0;
    }
    the students.cpp is

    Code:
    #include <iostream>
    #include <string>
    #include "students.h"
    
    using namespace std;
    
    void read_students(const unsigned int number_of_students, students *student){
    
            for(int i = 0; i < number_of_students; i++){
                    cin >> (student + i) -> student_id;
                    cin >> (student + i) -> first_name
                        >> (student + i) -> last_name;
            }
    
            cout << "finished input" << endl;
    }
    and students.h is
    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    
    struct student_list{
    
            unsigned int student_id;
            std::string first_name;
            std::string last_name;
            };
    
    typedef struct student_list students;
    
    void read_students(const unsigned int number_of_students, students *student);
    
    #endif
    Thanks.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    worked fine for me - tho i did compile it in one file - can't see any reason why you should have a problem
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am a C programer; I am working off and on at learning C++.

    There is two different deletes in C++; one with arrays one without arrays.

    Code:
    students *student_details = new students[number_of_students];
    Code:
    delete(student_details);
    I am guessing this is the correct way!

    Code:
    delete [] student_details;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    There is two different deletes in C++; one with arrays one without arrays.
    There are actually more than two forms of operator delete in C++.

    However, your conclusion is valid. The form of operator delete used to release an object (or set of objects) needs to match the form of operator new used to allocate it.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Hmm interesting. So u cannot delete an array with delete() ? I tried delete [] student_details and it worked. I am also learning c++ after learning c. So I thought all free() translate to delete() irrespective of being an element or an array.

    Thank you for your help.

    Someone above mentioned it worked for him. Is this behaviour system dependent?

    -livin
    Last edited by livin; 10-16-2012 at 12:46 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Deleting something you allocated with new[] with delete invokes undefined behavior.
    Meaning, it might work, it might not work, it might blow up your computer, blow out your monitor or just do nothing.
    You may also want to check out std::vector that handles dynamic arrays with ease.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing dynamically allocated memory
    By kkk in forum C Programming
    Replies: 4
    Last Post: 05-30-2011, 12:24 PM
  2. Replies: 2
    Last Post: 10-18-2010, 07:52 AM
  3. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  4. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 06:53 PM
  5. deleting dynamically allocated memory...
    By heljy in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2002, 11:40 AM