Abort trap : 6

Mar 13, 2021 at 2:29am
I got an error "Abort trap: 6" on my code. I have been looking through it to find how to fix it. I think this has to do with a dynamic memory allocation to solve it? But I do not find where and how I get this error.

How to fix it? And how to spot the error comes from ? (If you have some tips, appreciate)

(Using Visual Studio Code)

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void arrayToFile(string fileName, int *array, int sizeArray)
{
    ofstream outputFile(fileName, ios::binary); // file opened in binary mode

    outputFile.write(reinterpret_cast<char *>(array), sizeArray * sizeof(int)); //.write member accepts pointer to char as first argument
                                                                                // so use reinterpret_cast when calling it

    outputFile.close();
}

void fileToArray(string fileName, int *array, int sizeArray)
{
    ifstream inputFile(fileName, ios::binary);

    inputFile.read(reinterpret_cast<char *>(array), sizeArray * sizeof(int)); //.read member accepts pointer to char as first argument
                                                                              // so use reinterpret_cast when calling it

    inputFile.close();
}

int main()
{
    const int arraySize = 5;
    int array[arraySize] = {1, 2, 3, 4, 5};
    int arrayToRead[arraySize];

    //call arrayToFile function to write into file
    cout << "Let's start to write on the file from the array\n";
    arrayToFile("ziziMechant.dat", array, sizeof(array));
    cout << "Done\n";

    //call fileToArray to read from file
    cout << "Let's read that file to the array\n";
    fileToArray("ziziMechant.dat", arrayToRead, sizeof(arrayToRead));
    cout << "Job done\n";

    //display the array we just read
    cout << "Let's display the array content\n";
    for (int i = 0; i < arraySize; i++)
    {
        cout << arrayToRead[i] << ",";
    }
    cout << "\n";

    return 0;
}
Last edited on Mar 19, 2021 at 12:34am
Mar 13, 2021 at 6:00am
> arrayToFile("ziziMechant.dat", array, sizeof(array));
sizeof() already tells you the number of bytes.

There's no need to do * sizeof(int) in the functions as well.

You might have noticed after the write that ziziMechant.dat was a bit larger than you might expect, giving you a hint that something was amiss.


Mar 13, 2021 at 10:25am
You don't need .close() as the file streams will be closed when the stream variables go out of scope when the function ends.
Mar 19, 2021 at 12:54am
salem c, how do you know that "ziziMechant.dat" was a bit larger ?
Mar 19, 2021 at 1:02am
Because you were multiplying the entire amount written to file by sizeof(int) [most likely 4x].
Mar 19, 2021 at 3:19am
@seeplus,
Just a note: I think it's good practice to use fstream::close() when I'm done with a file, just to avoid any potential errors...but then again, I'm something of a fanatic about input validation, memory management, and stuff like that ;)

max
Mar 19, 2021 at 1:55pm
The .close() calls in OP's arrayToFile and fileToArray functions are entirely redundant because the the ofstream and ifstream both go out of scope at the end of the function, which will automatically clean up the file stream resources.
Topic archived. No new replies allowed.