This is the mail archive of the glibc-linux@ricardo.ecn.wfu.edu mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: problem with malloc?


In <3903F90B.AF3D32B7@retarus.de>, on 04/24/00 
   at 09:34 AM, Joerg Mueller-Tolk <joerg.mueller-tolk@retarus.de> said:

>"tjk@tksoft.com" wrote:

>> > 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);

Fred


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]