开发者

help needed with segmentation error in c code

开发者 https://www.devze.com 2023-01-25 15:16 出处:网络
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)

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, "w开发者_如何学编程b");
                     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


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...


  • 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.


Use sizeof(struct buf) instead.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消