Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: I need help

Reply
Thread Tools

Re: I need help

 
 
janus
Guest
Posts: n/a
 
      05-14-2011
Another thing it is likely to print is "SIGSEGV" or "SIGBUS" or
"General Protection Fault" or something along those lines. The
anonymous array created by the string literal is not `const', but
that's just an accident of history. Trying to modify such an array
yields undefined behavior -- and indeed, plenty of compilers will
put the arrays in read-only memory. (This allows them to save space
by allocating just one array to hold both of "over" and "recover".)

I am a bit confused by the above...

Is the correct?

char* string = "NEW";
strcat(string, "Jersey");

If string is in read-only memory, could it still support things like concatenation?

Janus
 
Reply With Quote
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      05-14-2011

"janus" <> schrieb im Newsbeitrag
news:45f62c12-a439-42b7-be27-...
....
> Is the correct?
>
> char* string = "NEW";
> strcat(string, "Jersey");
>
> If string is in read-only memory, could it still support things like
> concatenation?
>
> Janus


read-only memory does not support concatenation either. And there is yet
another problem when you code:

char string[] = "NEW";
strcat(string, "Jersey");

You do not reserve space to hold the concatenated string. Here is how you
could do it:

char string[10] = "NEW";
strcat(string, "Jersey");

However strcat has the danger to run out of string size. I always code that:

char string[10] = "NEW";
strncat(string, "Jersey", sizeof(string) - strlen(string) - 1);

or with a macro:

#define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
char string[10] = "NEW";
STRNCAT(string, "Jersey");

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-14-2011
janus <> writes:
> Another thing it is likely to print is "SIGSEGV" or "SIGBUS" or
> "General Protection Fault" or something along those lines. The
> anonymous array created by the string literal is not `const', but
> that's just an accident of history. Trying to modify such an array
> yields undefined behavior -- and indeed, plenty of compilers will
> put the arrays in read-only memory. (This allows them to save space
> by allocating just one array to hold both of "over" and "recover".)
>
> I am a bit confused by the above...
>
> Is the correct?
>
> char* string = "NEW";
> strcat(string, "Jersey");
>
> If string is in read-only memory, could it still support things like
> concatenation?


No.

On my system, a program containing those two lines died with a
segmentation fault.

But of course that's not the only possible result. String literals
(more accurately, the static arrays associated with them) aren't
*necessarily* stored in read-only memory. The strcat() call will
attempt to store the 7 characters of "Jersey" (6 plus the trailing
'\0') at locations starting after the 'W' in "NEW". This might
appear to work, if that area of memory happens to be writable
and happens not to be used for anything else. On the other hand,
it could clobber some other variable, or write over some internal
information (such as the function's return address), with arbitrarily
bad results that might not even appear until later.

And the phrase "will attempt" above isn't strictly correct. Since
the behavior is undefined, strcat() could conceivably detect that it
won't be able to write the characters and do something else instead.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      05-15-2011
"Heinrich Wolf" <> writes:
> "janus" <> schrieb im Newsbeitrag
> news:45f62c12-a439-42b7-be27-...
> ...
> > Is the correct?
> >
> > char* string = "NEW";
> > strcat(string, "Jersey");
> >
> > If string is in read-only memory, could it still support things like
> > concatenation?
> >
> > Janus

>
> read-only memory does not support concatenation either. And there is
> yet another problem when you code:
>
> char string[] = "NEW";
> strcat(string, "Jersey");
>
> You do not reserve space to hold the concatenated string. Here is how
> you could do it:
>
> char string[10] = "NEW";
> strcat(string, "Jersey");
>
> However strcat has the danger to run out of string size. I always code that:
>
> char string[10] = "NEW";
> strncat(string, "Jersey", sizeof(string) - strlen(string) - 1);


Why do you want to scan accross "NEW" twice? Once in the strlen, and
once in the strncat.

Phil
--
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      05-15-2011

"Phil Carmody" <> schrieb im Newsbeitrag
news:...
....
> Why do you want to scan accross "NEW" twice? Once in the strlen, and
> once in the strncat.


I know that I could save some microseconds with

size_t l = strlen(a);
strncat(a + l, b, sizeof(a) - l - 1);

but STRNCAT has done a good job many years and is easy to read.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-15-2011
On 5/14/2011 2:15 PM, janus wrote:
> Another thing it is likely to print is "SIGSEGV" or "SIGBUS" or
> "General Protection Fault" or something along those lines. The
> anonymous array created by the string literal is not `const', but
> that's just an accident of history. Trying to modify such an array
> yields undefined behavior -- and indeed, plenty of compilers will
> put the arrays in read-only memory. (This allows them to save space
> by allocating just one array to hold both of "over" and "recover".)
>
> I am a bit confused by the above...


It is customary to mark the material you quote, making it
distinguishable from material of your own authorship. See those
greater-than signs at the left margin? Most news clients or mail
clients will insert them for you.

It is also customary to attribute the quoted material to its
author (or authors), so that readers aren't misled into thinking
its your own. See that "On...janus wrote:" line at the top?
Again, most news clients or mail clients will insert such lines
for you.

To use the medium effectively, first learn to use the medium.

> Is the correct?
>
> char* string = "NEW";
> strcat(string, "Jersey");


Undefined behavior.

> If string is in read-only memory, could it still support things like concatenation?


No.

Nor should it. Think of it this way: A string literal (when
used this way) is notionally a constant, much like 42 or 3.14. If
you start modifying constants, things will become very confusing
very rapidly:

int i = 6;
6 *= 7; /* not legal; just imagine */
int j = 6; /* does j have the value forty-two? */

Similarly with string constants: By concatenating to a string (or
by changing one or more of its existing characters), you change
the value of that string. So:

char *message = "Hello, world!";
strcpy(message, "Go to Hell!"); /* not legal; just imagine */
puts(message); /* friendly, or confrontational? */

You don't (or shouldn't) want to change a constant, and C doesn't
promise that an attempt to do so will succeed. (Doesn't promise
anything at all about what will happen, actually.)

--
Eric Sosman
d
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-15-2011
On 5/14/2011 2:29 PM, Heinrich Wolf wrote:
>[...]
> However strcat has the danger to run out of string size. I always code
> that:
>
> char string[10] = "NEW";
> strncat(string, "Jersey", sizeof(string) - strlen(string) - 1);
>
> or with a macro:
>
> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
> char string[10] = "NEW";
> STRNCAT(string, "Jersey");


If you like this sort of thing, so be it -- I'm not convinced
there's much value in using training wheels, but tastes vary.

However, you ought to mention that this STRNCAT macro relies
on the pre-existing '\0' at the end of the output array. If
string[9] were '?' instead of '\0', STRNCAT would run happily but
leave you with a non-terminated string, just waiting to cause
unpleasant surprises later.

--
Eric Sosman
d
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      05-15-2011

"Eric Sosman" <> schrieb im Newsbeitrag
news:iqog3e$27h$...
....
> However, you ought to mention that this STRNCAT macro relies
> on the pre-existing '\0' at the end of the output array. If
> string[9] were '?' instead of '\0', STRNCAT would run happily but
> leave you with a non-terminated string, just waiting to cause
> unpleasant surprises later.

....
No! strncat guarantees to append a terminating '\0' character, even if it
needs to truncate the source string. That is the difference to strncpy. You
do net need to memset(output, 0 sizeof(output)); The only prerequisite is,
that output is terminated by a single '\0' before. But that is the same with
strcat.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-15-2011
On 5/15/2011 8:30 AM, Heinrich Wolf wrote:
>
> "Eric Sosman" <> schrieb im Newsbeitrag
> news:iqog3e$27h$...
> ...
>> However, you ought to mention that this STRNCAT macro relies
>> on the pre-existing '\0' at the end of the output array. If
>> string[9] were '?' instead of '\0', STRNCAT would run happily but
>> leave you with a non-terminated string, just waiting to cause
>> unpleasant surprises later.

> ...
> No! strncat guarantees to append a terminating '\0' character, even if
> it needs to truncate the source string. That is the difference to
> strncpy. You do net need to memset(output, 0 sizeof(output)); The only
> prerequisite is, that output is terminated by a single '\0' before. But
> that is the same with strcat.


My apologies: I confused `strncat' with `strncpy'.

--
Eric Sosman
d
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      05-16-2011
"Heinrich Wolf" <> writes:

> "Phil Carmody" <> schrieb im
> Newsbeitrag news:...
> ...
> > Why do you want to scan accross "NEW" twice? Once in the strlen, and
> > once in the strncat.

>
> I know that I could save some microseconds


You knew you always knew the ratio of the length of the left
string compared to the right string? Wow, you're a lucky man.
I wish I could be so certain that arbitrary input was so
predictable.

Phil
--
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57