Hi George, I was just going through the code. In second code, line ((char*)memcpy((void*)*in_buf, out_buf, 4))[4] = '\0'; why does (void *)*in_buf returns NULL. I tried typecasting different type of pointers and its always NULL.
Regards, Amit Dang ----- Original Message ----- From: "Huber, George K RDECOM CERDEC STCD SRI" <[EMAIL PROTECTED]> To: "'Scott'" <[EMAIL PROTECTED]>; <[email protected]> Sent: Thursday, January 27, 2005 11:28 PM Subject: RE: Into the Void > Scott wrote: > > >Thanks for your help; I actually managed to get the program to compile > >with no errors, but now am getting a segmentation fault which I can't > >track down, so I'll probably be back to plague you folks some more! > > Even without seeing your code, I probably can guess what is happening. > Consider the following sample program (with no error checking) > > ====================================================================== > #include <stdio.h> > #include <stdlib.h> > #include <string.h> > > #define BUF_LEN 6 > > int main() > { > char* in_buf = malloc(BUF_LEN*sizeof(char)); > char* out_buf = malloc(BUF_LEN*sizeof(char)); > > strcpy(out_buf, "Test"); > > ((char*)memcpy((void*)in_buf, out_buf, 4))[4] = '\0'; > > printf("%s\n", in_buf); > > return 0; > } > ====================================================================== > > The above program compiles cleanly and produces the expected output. > > However, if the above program is changed to this: > > ====================================================================== > #include <stdio.h> > #include <stdlib.h> > #include <string.h> > > #define BUF_LEN 6 > > int main() > { > char* in_buf = malloc(BUF_LEN*sizeof(char)); > char* out_buf = malloc(BUF_LEN*sizeof(char)); > > strcpy(out_buf, "Test"); > > ((char*)memcpy((void*)*in_buf, out_buf, 4))[4] = '\0'; > > printf("%s\n", in_buf); > > return 0; > } > ====================================================================== > > we get a single warning (warning: cast to pointer from integer of > different size) and the program segfaults when it is run. > > Make sure you are not dereferencing the the first parametere to memcpy. > > Also, if the above does not help, try compiling with debugging support > turned on (i.e. gcc -g ...) and then run the program under gdb. Using the > gdb `where' command will show you what line you are seg-faulting on. > > Finally, I find it useful to alway compile using the flags "-ansi -pedantic > -Wall" (or "-std=c99 -pedantic -Wall) and treat any warning as errors. I > know it can be a pain-in-the-arse, but I find that it does minimize run-time > errors. > > George > - > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in > the body of a message to [EMAIL PROTECTED] > More majordomo info at http://vger.kernel.org/majordomo-info.html - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html
