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开发者_C百科 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";
}
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.
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]
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];
}
}
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.
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.
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));
精彩评论