需要帮助解决 C 代码中的分段错误

发布于 2024-10-02 20:16:29 字数 1807 浏览 3 评论 0原文

我将数据保存在缓冲区中,

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 技术交流群。

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

发布评论

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

评论(6

守护在此方 2024-10-09 20:16:29

这三件事不应改变事情,但会让我们(和您)更容易理解和修复您的代码。

  • 变量和类型永远不要使用相同的名称(buf 与 buf),并且最好避免仅类型不同的标识符。一个常见的习惯用法是将类型大写。 (Bufbuf
  • 很好地格式化和缩进代码
  • 使用 typedef struct 进行结构定义

这是一个示例:

typedef struct
{
  unsigned short a[2];
  unsigned short b[2];
} Buffer;

Buffer *buf = malloc(sizeof(Buffer) * MAX_FILE_SIZE);

至于导致段错误的原因,它是很难说。我想查看 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.

  • NEVER have the same name for variables and types (buf vs. buf), and preferably avoid identifiers that differ only in type. A common idiom is to capitalize types. (Buf vs buf)
  • Format and indent your code nicely
  • Use typedef struct for struct definitions

Here's an example:

typedef struct
{
  unsigned short a[2];
  unsigned short b[2];
} Buffer;

Buffer *buf = malloc(sizeof(Buffer) * MAX_FILE_SIZE);

As to what's causing the segfault, it's difficult to say. I'd like to see the #define for MAX_FILE_SIZE, and more context around where the crash is happening. Your malloc looks fine now, but you're not checking to see if it succeeds...

摘星┃星的人 2024-10-09 20:16:29
  • 您从不检查 fopen 的返回值(3),当无法打开文件时返回NULL
  • sizeof( struct buf ) 告诉您结构体的大小,而 sizeof( buf ) 则为您提供 buf 变量(即指针)的大小,它是 4 或 8 字节,具体取决于平台。
  • You never check the return value of fopen(3), which returns NULL when it cannot open the file.
  • sizeof( struct buf ) tells you size of the structure, while sizeof( buf ) gives you size of the buf variable, i.e. of a pointer, which is 4 or 8 bytes depending on the platform.
眼前雾蒙蒙 2024-10-09 20:16:29

使用 sizeof(struct buf) 代替。

Use sizeof(struct buf) instead.

紫南 2024-10-09 20:16:29

首先要做的事情:在调试器下运行它。哪一行导致了段错误?

另外,在普通 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.

要走干脆点 2024-10-09 20:16:29

作为一般经验法则,始终使用适当的错误处理代码(即 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.

陌路黄昏 2024-10-09 20:16:29

尝试将 fwrite 更改为:

fwrite(buf, sizeof(struct buf), MAX_FILE_SIZE, fp);

您分配了 struct buf *buf 来为 MAX_FILE_SIZE 数量的 struct buf 数据分配内存。

If 语句也可能是一个问题;但是,我需要知道它周围有什么代码 - 这是在循环中完成的吗?

try changing the fwrite to:

fwrite(buf, sizeof(struct buf), MAX_FILE_SIZE, fp);

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.

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