This is the mail archive of the
mailing list for the glibc project.
Re: problem with malloc?
- To: glibc-linux at ricardo dot ecn dot wfu dot edu
- Subject: Re: problem with malloc?
- From: fheitka at attglobal dot net
- Date: Fri, 28 Apr 2000 17:42:52 -0400
- Cc: Joerg Mueller-Tolk <joerg dot mueller-tolk at retarus dot de>
- Reply-To: glibc-linux at ricardo dot ecn dot wfu dot edu
In <3903F90B.AF3D32B7@retarus.de>, on 04/24/00
at 09:34 AM, Joerg Mueller-Tolk <email@example.com> said:
>> > len = strnlen( buffer );
>> > target = ( char * )malloc( (len+1)*sizeof( char ) );
>> > strncpy( target, buffer, len );
>I don't understand two things:
>First, is strnlen() just a typo? If not, I understand it as to return the
Oops. Yes that is a typo. Should be strlen()
>length, if it is less or equal to a maximum and to return the maximum
>otherwise, but there would be the maximum parameter missing. In the
>following I suppose it is meant to be strlen(buffer).
>Second, doesn't the strncpy() make more comparisons than needed. I do the
>job nearly always this way:
>tmpInt = strlen(buffer) + 1;
>target = new char[tmpInt]; // target = malloc(tmpInt);
>memcpy(target, buffer, tmpInt);