gpt4 book ai didi

C:段错误 11 仅在终端中,不在调试器中

转载 作者:行者123 更新时间:2023-11-30 14:42:57 25 4
gpt4 key购买 nike

我正在编写一个程序,它读取一个文件,一点一点地反转它,并将结果存储在一个新文件中,而不分配大于 1kb 的 block 。当我在终端中运行它时,它创建文件但不写入文件,而是崩溃并给出段错误 11。当我尝试使用 lldb 调试它时,整个代码运行没有任何问题。我的终端遵循与 lldb 不同的分配规则吗?我该如何解决这个问题?

我已经用大文件和小文件运行了代码,但即使使用几乎空的 txt 文件,它也会崩溃。我正在使用 lldb-340.4.119 运行 osx 10.10.5

int const CHUNK_SIZE = 1024;
int chunk_index = 0;
int character;
char new_filename[] = "output";

struct Chunk {
struct Chunk *previous;
int data[(CHUNK_SIZE-sizeof(struct Chunk*))/sizeof(int)];
};

struct Chunk* memory = (struct Chunk *)malloc(sizeof(struct Chunk));
struct Chunk* temp;

FILE *fp;
fp = fopen(argv[1], "r");

// read file into memory
character = fgetc(fp);
do {
memory->data[chunk_index] = character;
chunk_index++;
if ( chunk_index*sizeof(int) > CHUNK_SIZE-sizeof(struct Chunk*)){
chunk_index = 0;
temp = (struct Chunk *)malloc(sizeof(struct Chunk));
temp->previous = memory;
memory = temp;
}
character = fgetc(fp);
}
while (character !=EOF);
chunk_index--;
fclose(fp);


// write to new file
fp = fopen(new_filename, "wb");

do {
while (chunk_index >=0) {
printf("%c", memory->data[chunk_index]);
fprintf(fp, "%c", memory->data[chunk_index]);
chunk_index--;
}

chunk_index = (CHUNK_SIZE-sizeof(struct Chunk*))/sizeof(int);
temp = memory;
memory = memory->previous;
free(temp);
} while(memory!=NULL);

最佳答案

在调试器中运行与没有调试器运行之间的差异可能是由于调试器禁用了 ASLR。当您尝试调试它时,这可能会隐藏问题。

尝试撤消此操作。它seems在 LLDB 中应该使用命令来完成

settings set target.disable-aslr false

这应该撤消 ASLR 的禁用。在 GDB 中是

set disable-randomization off

不要忘记在调试器下重新启动程序(无需重新启动调试器!)以使此设置生效。在 GDB 中是 run 命令,LLDB 中应该有类似的命令。

关于C:段错误 11 仅在终端中,不在调试器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com