Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #4061 > unrolled thread

Re: I need help

Started byjanus <emekamicro@gmail.com>
First post2011-05-16 03:52 -0700
Last post2011-05-17 09:05 +1200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Re: I need help janus <emekamicro@gmail.com> - 2011-05-16 03:52 -0700
    Re: I need help "Heinrich Wolf" <invalid@invalid.invalid> - 2011-05-16 13:14 +0200
    Re: I need help pete <pfiland@mindspring.com> - 2011-05-16 07:17 -0400
    Re: I need help James Kuyper <jameskuyper@verizon.net> - 2011-05-16 07:27 -0400
    Re: I need help Ian Collins <ian-news@hotmail.com> - 2011-05-17 09:05 +1200

#4061 — Re: I need help

Fromjanus <emekamicro@gmail.com>
Date2011-05-16 03:52 -0700
SubjectRe: I need help
Message-ID<dceaf88c-7dca-487c-881a-64dcd49c76c3@glegroupsg2000goo.googlegroups.com>
> -- 
> 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))

On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
> "Ian Collins" <ian-...@hotmail.com> schrieb im Newsbeitrag 
> news:93bdoaFlpiU2@mid.individual.net...
> > 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));
>   }

I do not see any difference between the two
Janus

[toc] | [next] | [standalone]


#4063

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-05-16 13:14 +0200
Message-ID<iqr0vi$efk$1@news.m-online.net>
In reply to#4061
"janus" <emekamicro@gmail.com> schrieb im Newsbeitrag 
news:dceaf88c-7dca-487c-881a-64dcd49c76c3@glegroupsg2000goo.googlegroups.com...
...
> void a_procedure(char *a)
char * is a pointer and not an array. On a 32 bit machine sizeof(a) is 
normally 4 (4 bytes, 8 bit each)
...
>>   char string[10] = "NEW";

sizeof(string) is 10 here.

...

> I do not see any difference between the two
> Janus 

[toc] | [prev] | [next] | [standalone]


#4064

Frompete <pfiland@mindspring.com>
Date2011-05-16 07:17 -0400
Message-ID<4DD107C1.2723@mindspring.com>
In reply to#4061
janus wrote:
> 
> > --
> > 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))
> 
> On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
> > "Ian Collins" <ian-...@hotmail.com> schrieb im Newsbeitrag
> > news:93bdoaFlpiU2@mid.individual.net...
> > > 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));
> >   }
> 
> I do not see any difference between the two

Try harder.

-- 
pete

[toc] | [prev] | [next] | [standalone]


#4066

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-05-16 07:27 -0400
Message-ID<iqr1nl$96e$1@dont-email.me>
In reply to#4061
On 05/16/2011 06:52 AM, janus wrote:
...
> On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
>> "Ian Collins" <ian-...@hotmail.com> schrieb im Newsbeitrag 
>> news:93bdoaFlpiU2@mid.individual.net...
>>> 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));
>>   }
> 
> I do not see any difference between the two
> Janus

In the first version of a_procedure, STRNCAT(a, "Jersey") expands to

	strncat(a, "Jersey", sizeof(a) - strlen(a) - 1)

In that expression, sizeof(a) gives the size of the variable 'a', which
is a pointer to char. Therefore, sizeof(a) will be equal to
sizeof(char*). There's a pretty good chance that this will be either 4
or 8, depending upon what platform you're compiling for. It's unlikely
to be 10, though that's not an impossible value.

In the second version, sizeof(string) gives the size of the variable
"string", which is an array 10 char elements, so it is guaranteed to be
10. It's unlikely that this will be the same as sizeof(a); but even if
it is, that's only a coincidence.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#4120

FromIan Collins <ian-news@hotmail.com>
Date2011-05-17 09:05 +1200
Message-ID<93dhs6FlpiU8@mid.individual.net>
In reply to#4061
On 05/16/11 10:52 PM, janus wrote:

Please stop starting new threads!  Drop that awful new google interface.

-- 
Ian Collins

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web