Abort trap : 6

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
> 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.


You don't need .close() as the file streams will be closed when the stream variables go out of scope when the function ends.
salem c, how do you know that "ziziMechant.dat" was a bit larger ?
Because you were multiplying the entire amount written to file by sizeof(int) [most likely 4x].
@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
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.