尝试写入 FILE 时出现总线错误
我正在尝试使用 2D 数组在 C++ 中生成所有矩阵,但是在尝试写入超过 735 个字符时出现总线错误,我认为我在内存分配方面遇到问题,你能帮我吗?
#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";
}
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";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能还有其他错误,但对于初学者来说,您是
使用
array[1]
(第二个元素),当array
只有一个元素(
int[symbols]
类型)。未定义的行为,以及既然你在写,你肯定会破坏其他对象
在堆栈上。
calloc
看起来也有点奇怪;这是我第一次看到用
sizeof
指定的元素大小一个常数。在本例中,常量的类型为 int,并且
您正在分配给
int*
,所以您可能很幸运。但 std::vector
当然,您要在第一次写入后关闭文件,
这意味着以后的所有写入都将是空操作。
There are probably other errors, but for starters, you're
using
array[1]
(the second element), whenarray
only haveone element (of
int[symbols]
type). Undefined behavior, andsince you're writing, you're certainly corrupting other objects
on the stack.
The
calloc
looks more than a little strange as well; it's thefirst time I've seen an element size specified with
sizeof
a constant. In this case, the constant has type
int
, andyou'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:
You should be using your loop variables:
感谢大家的帮助,这真的很有帮助,在这里我发布了最终的代码,我将其减少:
Thanks guys for your help,it was really helpful, here I post the final code which I reduce it:
ptr =(int*)calloc(symbols, sizeof(symbols));
应该是
ptr =(int*)calloc(symbols, sizeof(int));
我不确定这是否直接导致您的问题。
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.
如果您只想将一堆“1”写入文件,为什么不这样做:
编辑:要首先将数据放入向量中,然后复制到文件中,您可以执行以下操作:
并将其复制到文件,你会做类似的事情:
If you just want to write a bunch of '1's to a file, why not something like:
Edit: To put the data in a vector first, then copy to the file, you could do something like this:
and to copy that to the file, you'd do something like: