Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: I need help

 
 
Phil Carmody
Guest
Posts: n/a
 
      05-16-2011
Eric Sosman <(E-Mail Removed)> writes:

> On 5/15/2011 8:30 AM, Heinrich Wolf wrote:
> >
> > "Eric Sosman" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:iqog3e$27h$(E-Mail Removed)...
> > ...
> >> 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'.


Votes for abolishing srtncat?

1 here; I trust the above is enough of a reason to come on board, and from
what Heinrich has posted, hopefully there's a 3rd vote too.

Phil
--
"At least you know where you are with Microsoft."
"True. I just wish I'd brought a paddle." -- Matthew Vernon
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2011
On 05/15/11 06:29 AM, Heinrich Wolf wrote:
>
> or with a macro:
>
> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
> char string[10] = "NEW";
> STRNCAT(string, "Jersey");


That macro would go horribly (and silently) wrong if

a) 'a' wasn't a fixed sized array, or
b) it was, but the definition wasn't visible when the macro was expanded.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-16-2011
Ian Collins <(E-Mail Removed)> writes:

> On 05/15/11 06:29 AM, Heinrich Wolf wrote:
>>
>> or with a macro:
>>
>> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
>> char string[10] = "NEW";
>> STRNCAT(string, "Jersey");

>
> That macro would go horribly (and silently) wrong if
>
> a) 'a' wasn't a fixed sized array, or


I don't think "fixed size" conveys what you want. It might be seen to
contrast with variably modified arrays, but they work just as well (or
as badly) as any other. I think you wanted to suggest that 'a' might be
a pointer rather than an array. If not, I'll add:

a1) 'a' expands not to the name of an array but to a pointer expression.

> b) it was, but the definition wasn't visible when the macro was expanded.


I can't think how this could go silently wrong, but it sounds bad! I'd
add

c) strlen(a) >= sizeof(a)

simply because it goes wrong in a way that is often unexpected.

--
Ben.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2011
On 05/16/11 02:45 PM, pete wrote:
> Ian Collins wrote:
>>
>> On 05/15/11 06:29 AM, Heinrich Wolf wrote:
>>>
>>> or with a macro:
>>>
>>> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
>>> char string[10] = "NEW";
>>> STRNCAT(string, "Jersey");

>>
>> That macro would go horribly (and silently) wrong if
>>
>> a) 'a' wasn't a fixed sized array, or
>> b) it was, but the definition wasn't visible
>> when the macro was expanded.

>
> If (a) was a fixed sized array,
> but the definition wasn't visible when the macro was expanded,
> then (a) would be an incomplete type
> and (sizeof a) would cause the generation of a diagnostic message,
> rather than go silently wrong.


Well OK, I was thinking an extern pointer declaration, but that would be
a messy mix.

--
Ian Collins
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      05-16-2011

"Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On 05/15/11 06:29 AM, Heinrich Wolf wrote:
>>
>> or with a macro:
>>
>> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
>> char string[10] = "NEW";
>> STRNCAT(string, "Jersey");

>
> That macro would go horribly (and silently) wrong if
>
> a) 'a' wasn't a fixed sized array, or
> b) it was, but the definition wasn't visible when the macro was expanded.
>
> --
> Ian Collins


You must not use the macro STRNCAT in e.g. this case:

void a_procedure(char *a)
{
STRNCAT(a, "Jersey");
}

int main(void)
{

char string[10] = "NEW";
a_procedure(string);
}

Here sizeof(a) is the size of a pointer and not the size of string. Instead
you could do it without the macro:

void a_procedure(char *a, size_t size)
{
strncat(a, "Jersey", size - strlen(a) - 1);
}

int main(void)
{

char string[10] = "NEW";
a_procedure(string, sizeof(string));
}

 
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