linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Amit Dang" <amit_dang@intersolutions.stpn.soft.net>
To: "Huber,
	George K RDECOM CERDEC STCD SRI" <George.K.Huber@us.army.mil>,
	linux-c-programming@vger.kernel.org
Subject: Re: Into the Void
Date: Fri, 28 Jan 2005 10:06:06 +0530	[thread overview]
Message-ID: <007c01c504f2$e79e86d0$9736a8c0@ispl091> (raw)
In-Reply-To: D2AA47A6FB2C1A48AF0526440C0F245CAA3633@monm207.nae.ds.army.mil

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" <George.K.Huber@us.army.mil>
To: "'Scott'" <drmemory@3rivers.net>; <linux-c-programming@vger.kernel.org>
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 majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


  reply	other threads:[~2005-01-28  4:36 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-27 17:58 Into the Void Huber, George K RDECOM CERDEC STCD SRI
2005-01-28  4:36 ` Amit Dang [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-02-02 18:17 Huber, George K RDECOM CERDEC STCD SRI
2005-02-01 15:32 Huber, George K RDECOM CERDEC STCD SRI
2005-02-02 12:15 ` Glynn Clements
2005-01-26 16:02 Huber, George K RDECOM CERDEC STCD SRI
2005-01-26 13:45 Jeff.Fellin
2005-01-26 16:11 ` Scott
2005-01-27  4:02   ` Ron Michael Khu
2005-01-27 17:01     ` Scott
2005-01-26  0:09 Scott
2005-01-26  2:18 ` Ron Michael Khu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='007c01c504f2$e79e86d0$9736a8c0@ispl091' \
    --to=amit_dang@intersolutions.stpn.soft.net \
    --cc=George.K.Huber@us.army.mil \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).