From: Phil Sutter <phil@nwl.cc>
To: "Daniel." <danielhilst@gmail.com>
Cc: "linux-c-programming@vger.kernel.org"
<linux-c-programming@vger.kernel.org>
Subject: Re: How to check if char pointer is a constant!?
Date: Fri, 8 Jul 2016 14:59:31 +0200 [thread overview]
Message-ID: <20160708125931.GQ620@orbyte.nwl.cc> (raw)
In-Reply-To: <CAF3SDA5-L--FOXe-AzaRf7QE0Qx+XAuLeWNnFN-96smSEYJ7GA@mail.gmail.com>
On Fri, Jul 08, 2016 at 09:33:03AM -0300, Daniel. wrote:
> Hi all thanks for the reply.
>
> I think I wasn't clear. Here is a working example: http://codepad.org/0SUG6IcQ
>
> The function get_pair_from_string() should I) remove trailing spcaes, II) return
> the address for key and value using it's last parameters. The problem is that
> since I modify the first parameter calling with strings constants generates a
> segmentation fault. This is because strings constans aren't modifiable. I wan't
> to have better hints when this happen than just "SEGSEGV". I think that the
> -Wwrite-strings is what I was looking for :D, thanks Phil!!!
Glad I could help.
> John, I'll be in production, but not at this time. Luckly, that
> function is file scoped
> so I don't have to bother that much. Still I like to avoid the crypt
> "SEGSEGV" error
> message as much as possible. Usually I assert for NULL pointers and give a nice
> error message but this case is trickier. I'll take Phil advice and use
> -Wwrite-strings
Calling 'SIGSEGV' an error message is a slight underestimation. Actually
your program simply crashes. The gcc warning to activate will just help
to keep your code sane, not improve run-time behaviour.
If you want to modify strings no matter whether they are constant or
not, strdup() them into a local variable and work with that. Especially
since it's just for parsing as your example shows. Just keep in mind
that you will have to free the duplicated string at some point,
otherwise you will have a memory leak (which at some point will lead to
an equally cryptic message as your segfault did).
Cheers, Phil
next prev parent reply other threads:[~2016-07-08 12:59 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-07 19:40 How to check if char pointer is a constant!? Daniel.
2016-07-07 20:26 ` Phil Sutter
2016-07-08 12:33 ` Daniel.
2016-07-08 12:59 ` Phil Sutter [this message]
2016-07-08 14:01 ` Daniel.
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=20160708125931.GQ620@orbyte.nwl.cc \
--to=phil@nwl.cc \
--cc=danielhilst@gmail.com \
--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).