需要帮助解决 C 代码中的分段错误
我将数据保存在缓冲区中,
struct buf *bufPtr = malloc((sizeof(struct buf))*(MAX_FILE_SIZE));
然后我想将缓冲区写入大小为 (sizeof(struct buf))*(MAX_FILE_SIZE)
的文件 下面的代码将允许我打开一个新文件,用缓冲区的内容填充它,关闭文件并释放缓冲区
#define MAX_SIZE_PER_FILE 0x4000000
FILE *fp;
struct buf *bufPtr = malloc((sizeof(struct buf))*(MAX_FILE_SIZE));
k1[0]=0x0000;
k1[1]=0x0000;
while(k1[0] != 0xffff)
{
while(k1[1] != 0xffff)
{
//something different happens in the below line, but has noting to do with segmentation errors
bufPtr[i].a[1] = k[1]
//occurs on all variables of the struct
if( write_count + sizeof(struct buf) >= sizeof(struct buf)*MAX_FILE_SIZE ) {
write_count = 0;
sprintf( filename, "keys%d", file_idx );
file_idx++;
fp = fopen(filename, "wb");
printf("test1");
fwrite(bufPtr, sizeof(struct buf)*(MAX_FILE_SIZE),1,fp);
fclose(fp);
free(bufPtr);
}
write_count += sizeof(struct buf);
k1[1]++;
counter++;
}
write_count += sizeof(struct buf);
k1[1]++;
i++;
}
我在这段代码中的某个点遇到了分段错误,并且我知道 max_file_size 会更大,由于 struct buf 由两条短裤组成,
struct buf{
unsigned short a[2];
unsigned short b[2];
};
对我来说有什么想法吗?
我在通过我的 mac 运行它时遇到了这个错误,
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)
这是上面一行结构中的一个值
bufPtr[counter].a[0] = a1[0];
,发生在其他所有内容之前,但因为它在另一个循环中,所以它必须是一个我正在使用或分配的内存量有问题
i am holding data in a buffer,
struct buf *bufPtr = malloc((sizeof(struct buf))*(MAX_FILE_SIZE));
i then want to write the buffer to a file of size (sizeof(struct buf))*(MAX_FILE_SIZE)
the code below will then allow me to open a new file populate it with the contents of the buffer, close the file and free the buffer
#define MAX_SIZE_PER_FILE 0x4000000
FILE *fp;
struct buf *bufPtr = malloc((sizeof(struct buf))*(MAX_FILE_SIZE));
k1[0]=0x0000;
k1[1]=0x0000;
while(k1[0] != 0xffff)
{
while(k1[1] != 0xffff)
{
//something different happens in the below line, but has noting to do with segmentation errors
bufPtr[i].a[1] = k[1]
//occurs on all variables of the struct
if( write_count + sizeof(struct buf) >= sizeof(struct buf)*MAX_FILE_SIZE ) {
write_count = 0;
sprintf( filename, "keys%d", file_idx );
file_idx++;
fp = fopen(filename, "wb");
printf("test1");
fwrite(bufPtr, sizeof(struct buf)*(MAX_FILE_SIZE),1,fp);
fclose(fp);
free(bufPtr);
}
write_count += sizeof(struct buf);
k1[1]++;
counter++;
}
write_count += sizeof(struct buf);
k1[1]++;
i++;
}
i get a segmentation fault at a certain point in this code, and i know max_file_size will be bigger, as the struct buf consists of two shorts
struct buf{
unsigned short a[2];
unsigned short b[2];
};
any ideas for me guys
i got this error running it through my mac
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)
this was on a value within the struct
bufPtr[counter].a[0] = a1[0];
the line above , occurs before everything else but as it is in another loop, it must be a problem with the amount of memory i am using or allocating
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这三件事不应改变事情,但会让我们(和您)更容易理解和修复您的代码。
Buf
与buf
)typedef struct
进行结构定义这是一个示例:
至于导致段错误的原因,它是很难说。我想查看
MAX_FILE_SIZE
的#define
,以及有关崩溃发生位置的更多上下文。您的malloc
现在看起来不错,但您没有检查它是否成功......Three things which shouldn't change things, but will make your code MUCH easier for us (and you) to understand and fix.
Buf
vsbuf
)typedef struct
for struct definitionsHere's an example:
As to what's causing the segfault, it's difficult to say. I'd like to see the
#define
forMAX_FILE_SIZE
, and more context around where the crash is happening. Yourmalloc
looks fine now, but you're not checking to see if it succeeds...fopen 的返回值(3)
,当无法打开文件时返回NULL
。sizeof( struct buf )
告诉您结构体的大小,而sizeof( buf )
则为您提供buf
变量(即指针)的大小,它是 4 或 8 字节,具体取决于平台。fopen(3)
, which returnsNULL
when it cannot open the file.sizeof( struct buf )
tells you size of the structure, whilesizeof( buf )
gives you size of thebuf
variable, i.e. of a pointer, which is 4 or 8 bytes depending on the platform.使用 sizeof(struct buf) 代替。
Use sizeof(struct buf) instead.
首先要做的事情:在调试器下运行它。哪一行导致了段错误?
另外,在普通 32 位机器上,
sizeof(buf)
将始终为您提供 4,因为您正在获取指针的大小。First thing to do: run it under a debugger. What line causes the segfault?
Also,
sizeof(buf)
will always give you 4 on a normal 32-bit machine because you're taking the size of a pointer.作为一般经验法则,始终使用适当的错误处理代码(即 return(1))对 I/O 函数和系统调用进行错误检查。尝试在 if 语句中运行这些类型的函数,并在语句中处理日志记录/调试/退出。
As a general rule of thumb always error check I/O functions and system calls in general with appropriate error handling code, i.e. return(1). Try running those type of functions in an if statement and handle logging/debugging/exiting within the statement.
尝试将 fwrite 更改为:
您分配了 struct buf *buf 来为 MAX_FILE_SIZE 数量的 struct buf 数据分配内存。
If 语句也可能是一个问题;但是,我需要知道它周围有什么代码 - 这是在循环中完成的吗?
try changing the fwrite to:
you allocated struct buf *buf to have memory allocated for MAX_FILE_SIZE number of struct buf data.
The If statement may likely be an issue too; however, I'd need to know what code is around it - is this being done in a loop, etc.