尝试写入 FILE 时出现总线错误

发布于 2024-11-05 10:30:51 字数 761 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鱼窥荷 2024-11-12 10:30:51

可能还有其他错误,但对于初学者来说,您是
使用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), 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.

美胚控场 2024-11-12 10:30:51

在循环中,您可以通过用于声明数组的常量来访问数组:

array[1][symbols]

您应该使用循环变量:

array[i][j]

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]
誰認得朕 2024-11-12 10:30:51

感谢大家的帮助,这真的很有帮助,在这里我发布了最终的代码,我将其减少:

#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];
        }
    }

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];
        }
    }
残疾 2024-11-12 10:30:51

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.

无敌元气妹 2024-11-12 10:30:51

您的程序中有许多逻辑错误,但我相信导致崩溃的问题是您正在关闭嵌套循环中的文件,然后尝试写入它。

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.

风流物 2024-11-12 10:30:51

如果您只想将一堆“1”写入文件,为什么不这样做:

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

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

编辑:要首先将数据放入向量中,然后复制到文件中,您可以执行以下操作:

std::vector<int> data;

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

并将其复制到文件,你会做类似的事情:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文