Jessie
Jessie

Reputation: 1

Segmentation fault (core dumped) 4

I keep getting the error Segmentation fault(core dumped) after I use Ctrl+D to stop the input of my program. I have been googling this error and trying to figure out what is causing it, but I can't seem to figure it out. I have tried googling this problem and looked through other questions/answers on this website, but I still can't figure out why I am getting the error. From my research, I figured out that a segmentation fault error is cause by trying to access memory that I don't have permission to access. I hope this is right.

I am fairly new to C++ and would appreciate any help you guys can give me.

#include <iostream>
using namespace std;

#include "video.h"

int main() {

    const int MAX = 100;
    Video *video[MAX];  // up to 100 videos

    for(int l = 0; l < MAX; l++)
    {
        video[l] = NULL;
    }

    string title;
    string url;
    string desc;
    string sorting;
    float length;
    int rate;

    cout << "What sorting method would you like to use?" << endl;
    getline(cin, sorting);
    cout << "Enter the title, the URL, a comment, the length, and a rating for each video" << endl;

    while(getline(cin, title))
    {
        getline(cin, url);
        getline(cin, desc);
        cin >> length;
        cin >> rate;
        cin.ignore();
        for(int k=0; k < MAX; k++)
        {
            video[k] = new Video(title, url, desc, length, rate);
        }
    }


    video[MAX]->print();  // prints the new Video object

    delete[] video[MAX];

    return 0;
}

Upvotes: 0

Views: 160

Answers (3)

kfsone
kfsone

Reputation: 24269

delete[] video[MAX];

tries to array-delete an element beyond the end of video.

You either meant:

delete video[MAX - 1];

or you mean't

delete[] video;

Similarly, you might want to review

video[MAX]->print();  // prints the new Video object

this tries to print the MAX+1th video rather than the last video or the entire list of videos.

You should also review this loop:

    for(int k=0; k < MAX; k++)
    {
        video[k] = new Video(title, url, desc, length, rate);
    }

Upvotes: 0

Danra
Danra

Reputation: 9946

A couple of problems in your code:

1) Incorrect usage of delete[].

First of all, delete[] should be used only to delete dynamically allocated arrays. Yours isn't one.

Second of all, even if video were a dynamically allocated array, the correct way to delete it would be

delete[] video;

The correct way to delete the videos in your code would be to iterate over the array and delete each video:

for(int k=0; k < MAX; k++)
{
    delete video[k];
}

2) Incorrect indexing of the last element of the array.

video[MAX]->print();

Should be

video[MAX-1]->print();

The first element is at index zero, the second one is at index 1... so the last (MAX) element is at index MAX-1.

3) Finally, while not a cause for a segmentation fault, you're probably not trying to fill the entire array with the same video every time you read a line. However, that's what the code does :)

Upvotes: 1

Anedar
Anedar

Reputation: 4275

Besides the incorrect usage of delete that Danra already pointed out, your array

Video *video[MAX];

has no element

video[MAX];

the last element is

video[MAX-1];

because C++ starts counting with 0 (as you did correctly in your for-loop). So

video[MAX]->print(); 

will fail.

Upvotes: 0

Related Questions