Caste
Caste

Reputation: 11

Bus error when trying to write in FILE

I am trying to generate a matrix of all ones in C++, using an 2D array, however i have a BUS ERROR when trying to write more than 735 characters, I think I have problems with memory allocation, can you help me please?

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

#define symbols 800

int main ()

{

    fstream file("/Users/Caste/Documents/MAESTRIA/PROGRAMMING TEST/CAPACITY/test1.txt",ios::out);

    int *ptr;
    ptr =(int*)calloc(symbols, sizeof(symbols));
    int i,j,array[1][symbols];
    for (i=0; i<1; i++)
    {
        for (j=0; j<symbols; j++) 
            array[1][symbols]=1;
    }
    cout << "Array indicates:\n";
    for (i=0; i<1; i++) {
        for (j=0; j<symbols; j++) 

            file<<array[1][symbols];
            file.close();

        cout << "\n";
        }

Upvotes: 0

Views: 1012

Answers (6)

Jerry Coffin
Jerry Coffin

Reputation: 490128

If you just want to write a bunch of '1's to a file, why not something like:

std::ofstream file("whatever path");

std::fill_n(std::ostream_iterator<int>(file), how_many, 1);

Edit: To put the data in a vector first, then copy to the file, you could do something like this:

std::vector<int> data;

std::fill_n(std::back_inserter(data), how_many, 1);

and to copy that to the file, you'd do something like:

std::copy(data.begin(), data.end(), std::ostream_iterator<int>(file));

Upvotes: 0

Caste
Caste

Reputation: 11

Thanks guys for your help,it was really helpful, here I post the final code which I reduce it:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstring>

using namespace std;

#define symbols 1000000
//#define SNR 7

int main () 
{
    fstream file("/Users/Caste/Documents/MAESTRIA/PROGRAMMING TEST/CAPACITY/test1.txt",ios::out);
    int channel[1][symbols];
    memset((void*)channel, '\0', symbols);
    for (int i=0; i<1; i++) 
        for (int j=0; j<symbols; j++) {
            channel[i][j]=1;
        }
    for (int i=0; i<1; i++) {
        cout << endl;
        for (int j=0; j<symbols; j++) 
            //cout << channel[i][j];
            file<<channel[i][j];
        }
    }

Upvotes: 1

James Kanze
James Kanze

Reputation: 153919

There are probably other errors, but for starters, you're using array[1] (the second element), when array only have one element (of int[symbols] type). Undefined behavior, and since you're writing, you're certainly corrupting other objects on the stack.

The calloc looks more than a little strange as well; it's the first time I've seen an element size specified with sizeof a constant. In this case, the constant has type int, and you're allocating to an int*, so you might have lucked out. But std::vector<int> would seem more appropriate.

And of course, you're closing the file after the first write, which means that all later writes will be no-ops.

Upvotes: 3

Sean Johnston
Sean Johnston

Reputation: 174

Within your loops you are accessing the array via the constants used to declare it:

array[1][symbols]

You should be using your loop variables:

array[i][j]

Upvotes: 1

Jim Clay
Jim Clay

Reputation: 1003

There are many logical errors in your program, but I believe the problem that is causing the crash is that you are closing the file in your nested loop and then trying to write to it.

Upvotes: 0

Chad
Chad

Reputation: 19609

ptr =(int*)calloc(symbols, sizeof(symbols));
should be
ptr =(int*)calloc(symbols, sizeof(int));

I'm not sure that is causing your problem directly though.

Upvotes: 0

Related Questions