From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Amit Dang" Subject: Re: Into the Void Date: Fri, 28 Jan 2005 10:06:06 +0530 Message-ID: <007c01c504f2$e79e86d0$9736a8c0@ispl091> References: Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Return-path: Sender: linux-c-programming-owner@vger.kernel.org List-Id: Content-Type: text/plain; charset="us-ascii" To: "Huber, George K RDECOM CERDEC STCD SRI" , linux-c-programming@vger.kernel.org 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" To: "'Scott'" ; 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 > #include > #include > > #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 > #include > #include > > #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 majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html