seg fault while deteting the dynamically allocated memory

This is a discussion on seg fault while deteting the dynamically allocated memory within the C++ Programming forums, part of the General Programming Boards category; Heres the piece of code I wrote to gather student details into a dynamically allocated array of structs. But when ...

  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,425
    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
    2,583
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    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%.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    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, 05:53 PM
  5. deleting dynamically allocated memory...
    By heljy in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2002, 11:40 AM

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