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
struct. This is just asking for trouble. And you see you got caught in your own trap, when you are doingsizeof(struct buf)andsizeof(buf). Tidy up your code would help you most.