The code below is giving me the SIGSEGV error, segmentation fault at line number 66(found using debugger).According to me everything is fine but still don't know what is done wrong by me. Please someone help me out to sort the problem

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "video.h"
#include "Categories.h"

using namespace std;

string Video::selected_category(int category_num) {
    return category_array[category_num];
}

void Video::init_cate_array() {

}

int Video::total_objects_video(string filename) {
    Video temp_obj;
    int totalobjects = 0;
    int totalsize = 0;
    fstream file(filename.c_str(),ios::in);
    file.seekg(0,ios::end);
    totalsize = file.tellg();
    totalobjects = totalsize - sizeof(temp_obj);
    file.close();
    return totalobjects;
}

int Video::get_id(string category) {
    Video temp_obj;
    int id_counter;
    int totalobjects = 0;
    string filename = category+".dat";
    fstream file(filename.c_str(),ios::in);
    if(file == NULL) {
        file.close();
        return 1; /// If the file doesnot existss...
    } else {
        if(file.is_open()) {
            totalobjects = total_objects_video(filename);
            for(int i =0 ; i<totalobjects; i++) {
                file.read((char *)&temp_obj , sizeof(temp_obj));
                id_counter = temp_obj.video_id;
            }
        }
        ++id_counter;
        file.close();
        return id_counter;
    }
}

void Video::display_data() {
    cout<<"\n Video Id = "<<video_id<<endl;
    cout<<"\n Category_name = "<<category_name<<endl;
    cout<<"\n Category_short_name = "<<category_short_name<<endl;
    cout<<"\n Video name is "<<name<<endl;
    cout<<"\n Video size is "<<video_size<<endl;
    cout<<"\n Video length is "<<length;
}

//// In this category_name is passed with the function.
void Video::set_data(string category) {
    issued = false;
    video_id = get_id(category);
    category_name = category;
    category_short_name = category.substr(0,3);
    cout<<"\n Category short_name is "<<category_short_name<<endl;
    cout<<"\n The category name of the object is "<<endl;
    cout<<"\nEnter the name of the video "<<endl;
    cin>>name;
    cin.ignore();
    cout<<"\n Enter the video size in MiB "<<endl;
    cin>>video_size; /// Size must be in MB
    cout<<"\n Enter the length of video "<<endl;
    cin>>length;
}

void Video::add_video() {
        string choosen_category;
        int category_num;
        string filename;
        string *ptr1;
    //  string category_array[30];
        //init_cate_array();
        Video temp;

        Categories obj;

        obj.view_categories();

        //ptr1 = obj.store_categories();

        //// This portion will print the data read from the file and store it in
        // a local array.   
        int counter = 0;

        for(int i = 0;i < 30; i++) {
            category_array[i] = "";
        }

        fstream file("catgories.dat",ios::in);
        if(file.is_open()) {
            while(getline(file,category_name)) {
                category_array[counter] = category_name;
                cout<<"\n Data is "<<category_array[counter];
                ++counter;
            }
        }
        file.close();

        do {
            cout<<"\n Select the category number you want to add video in ? "<<endl;
            cin>>category_num;
            filename = selected_category(category_num);
            if(filename == "") {
                cout<<"\n Not a category,try again ";
            } 
        }while(filename == "");
        choosen_category = filename;
        filename = filename+".dat"; 
        cout <<"\n Filename is "<<filename;
        temp.set_data(choosen_category);
        temp.display_data();
        //// opening the filename entered by the user.
//      fstream fileOut(filename.c_str(),ios::app);
//      
//      if(fileOut.is_open()) {
//          fileOut.write((char *)&temp,sizeof(temp));
//          fileOut.close();
//          cout<<" \n\n\nVideo record added :)";
//          temp.display(); 
//      } else {
//          fileOut.close();
//          fstream fileOut(filename.c_str(),ios::out); 
//          fileOut.write((char *)&temp,sizeof(temp));
//          fileOut.close();
//          cout<<" \n\n\nVideo record added :)";
//          temp.display(); 
//      }
//      
}

Recommended Answers

All 12 Replies

A segmentation fault typically means you tried to access memory outside of your address space. The most common causes of this are a bogus index or invalid pointer.

Also note that the point at which an error manifests doesn't mean that exact line is the root cause. Sometimes the line pointed out by a debugger is wholly unrelated to the problem.

Since it appears this error can be reproduced in a debugger, step through your code and work back from the point it happens. Check your data, check your indexes, make sure that everything looks kosher memory-wise until you find a problem.

Tried everything @deceptikon, not finding any clues to get it sort as everything looks fine to me.

Actually when I use it like this way
category_name = category.c_str();
It would work but the next line is giving problem now. if I comment it out & then try it works good.

What is the type of category_name

The type of category_name is of string type. This is the header file for that

#include<string>
#ifndef VIDEO_H
#define VIDEO_H
using namespace std;
    static string category_array[30];
    class Video {
        private : string name;
                  int video_id;
                  string category_name;
                  //string cat_short_name;
                  float video_size; /// Size must be in MB
                  int length; //// Length of video must be in minutes
                  bool issued; /// This acts as flag to decide whether the the video is already issued
                  bool video_exists(string user_choice);
        public :    
                 void init_cate_array();
                 void display_data();
                 void set_data(string);
                 int get_id(string);
                 void add_video();
                 void delete_video();
                 void modify_video();
                 void view_videos();

                 int total_objects_video(string);
                 int total_size_file(string);
                 string selected_category(int);   
    }; 
#endif

Now the code is working fine when I don't use the cat_short_name variable. If i am trying to assign it the value it is showing the segmentation error.

This is a typical symptom that you are trashing the memory. If removing a variable fixes a sigsev that normally means you are trying to access an array out of bounds. Are you doing any dynamic memory allocation? Do you have an array of videos somewhere in your code?

@NathanOliver ,although I am using a array but know that it can't go out of bonds because I am running the loop till the size of the array.
No there is no dynamic memory allocation in my case.
No I don't have array of videos stored but I do have array of categories storing the categories.In Video::add_video() function it is used.

In std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () ()
Program received signal SIGSEGV, Segmentation fault.
Code blocks throwing this error

if your compiler is saying the code from the STL is causing the sigsegv ignore that. 99.99% of the time it is your code that is causing the issue. Can you post the code that you have the deals with the arrays?

@NathanOliver the code which I first posted contains the use of array in add_video func.(on line 76). The arrays are used on line 98.Also one thing I haven't define constructor yet it is showing multiple declaration if I try to define it.

Modified the code but the same error still persists and getting no clues how to remove it ?
Now I have, dynamically allocated memory to the objects to which I previously not allocating. The modified code is still giving me the same error at line 54.

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include "video.h"
#include "Categories.h"

using namespace std;

string Video::selected_category(int category_num) {
    return Categories::total_categories[category_num];
}

int Video::total_objects_video(string filename) {
    Video* v1 =  new Video;
    int totalobjects = 0;
    int totalsize = 0;
    int objectsize = 0;
    cout<<"\nThe filename is "<<filename<<endl;
    fstream file1(filename.c_str(),ios::in);
    file1.seekg(0,ios::end);
    totalsize = file1.tellg();
    objectsize = sizeof(v1); 
    file1.close();
    cout<<"\nThe size of Video Object is "<<sizeof(v1);
    totalobjects = totalsize/objectsize;
    delete v1;
    return totalobjects;    
}

int Video::total_size_file(string filename) {
    int totalsize = 0;
    fstream file(filename.c_str(),ios::in);
    file.seekg(0,ios::end);
    totalsize = file.tellg();
    file.close();
    return totalsize;
}

int Video::get_id(string category) {
    string filename = category+".dat";
    int totalobjects = 0;
    totalobjects = total_objects_video(filename);
    Video* temp_obj;
    int id_counter = 0;
    fstream file(filename.c_str(),ios::in);
    if(file == NULL) {
        file.close();
        return 1; /// If the file doesnot existss...
    } else {
        if(file.is_open()) {            
            for(int i = 0 ; i<totalobjects; i++) {
                file.read((char *) &temp_obj , sizeof(temp_obj));
                id_counter = temp_obj->video_id;
            }
        }
        delete temp_obj;
        ++id_counter;
        file.close();
        return id_counter;
    }
}

void Video::display_data() {
    cout<<"\nVideo Id = "<<video_id<<endl;
    cout<<"\nCategory_name = "<<category_name<<endl;
//  cout<<"\n Category_short_name = "<<cat_short_name<<endl;
    cout<<"\nVideo name is "<<name<<endl;
    cout<<"\nVideo size is "<<video_size<<endl;
    cout<<"\nVideo length is "<<length;
}

//// In this category_name is passed with the function.
void Video::set_data(string category) {
//  string * ptr = &category;
    issued = false;
    video_id = get_id(category);
    category_name = category;
    if(category.length() <= 4 ) {
//      cat_short_name = " ";
        cat_short_name = category;
    } else {
//      cat_short_name = " ";
        cat_short_name = category_name.substr(0,2);
    }
    cout<<"\n Category short_name is "<<cat_short_name<<endl;
    cout<<"\nThe category name of the object is "<<category_name<<endl;
    cout<<"\nEnter the name of the video "<<endl;
    cin>>name;
    cin.ignore();
    cout<<"\nEnter the video size in MiB "<<endl;
    cin>>video_size; /// Size must be in MB
    cout<<"\nEnter the length of video "<<endl;
    cin>>length;
}

void Video::add_video() {
        string choosen_category;
        int category_num;
        string filename;

        Video *temp = new Video;
        Categories obj;
        obj.view_categories();
        obj.store_categories();

        do {
            cout<<"\nSelect the category number you want to add video in ? "<<endl;
            cin>>category_num;
            filename = selected_category(category_num);
            if(filename == "") {
                cout<<"\nNot a category,try again ";
            }
        }while(filename == "");
        choosen_category = filename;
        filename = filename+".dat";
        cout <<"\nFilename is "<<filename;
        temp->set_data(choosen_category);


        //// opening the filename entered by the user.
        fstream fileOut(filename.c_str(),ios::app|ios::ate|ios::out);

        //if(fileOut.is_open()) {
            cout<<"\nThe current write pointer is on "<<fileOut.tellp();
            fileOut.write((char *)&temp, sizeof(temp));
            fileOut.close();
            cout<<"\n\n\nVideo record added :)";
            temp->display_data();
            delete temp;
//      } else {
//          fileOut.close();
//          fstream fileOut(filename.c_str(),ios::out);
//          fileOut.write((char *)&temp, sizeof(temp));
//          fileOut.close();
//          cout<<"\n\n\nVideo record added :)";
//          temp.display_data();
//      }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.