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

7
  • How did you defined filename? Commented Nov 18, 2010 at 19:24
  • it actually writes a file , but when i put a printf before it , the printf does not get called, strangest thing i have ever seen Commented Nov 18, 2010 at 19:28
  • @molleman - sometimes the printf() output is not displayed even when it is called because the output is buffered. Just because the text is not displayed does not mean the segfault is occuring before that line. Commented Nov 18, 2010 at 19:40
  • yeah i got that ghillis, cheers Commented Nov 18, 2010 at 19:45
  • 1
    Never, ever call a variable the same as a struct. This is just asking for trouble. And you see you got caught in your own trap, when you are doing sizeof(struct buf) and sizeof(buf). Tidy up your code would help you most. Commented Nov 18, 2010 at 20:21

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Use sizeof(struct buf) instead.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.