I'm writing a program that mirrors an image using Memory Mapped Files.
These are the parts of the code I find relevant for this particular error.
First I create the output file.
Code:
if ( (fpout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC,FILE_MODE)) < 0){
fprintf(stderr,"can't creat %s for writing\n", argv[2]);
exit(1);
}
Then "set" the MMF.
Code:
if( (dst = mmap(0, size, PROT_READ, MAP_FILE | MAP_SHARED, fpout, 0)) == (caddr_t) - 1){
fprintf(stderr, "mmap error for output file");
exit(1);
}
Call the function that writes the imagem header and where the error is.
Code:
if(writeImageHeader(h, dst) == -1){
printf("Could not write to output file\n");
return -1;
}
Function located on the ppmtools.c file, whose header file is included at the top of the main C file.
Code:
int writeImageHeader(header* h, char* dst){
//write header fields with newline between them
if(sprintf(dst,"%s\n%c\n%c\n%c\n",h->type,h->width,h->height,h->depth) < 0){
return -1;
}
return 0;
}
Using GDB I get:
Code:
(gdb) step
Saving header to output file
138 if(writeImageHeader(h, dst) == -1){
(gdb) step
writeImageHeader (h=0x804c170,
dst=0xb7f7a000 <Address 0xb7f7a000 out of bounds>) at ppmtools.c:150
Have googled for a similar error but can't find an answer and I have exhausted all options I could think of.
Any help is appreciated.
Thanks.