[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: problem with strncpy



On Thu, Mar 20, 2003 at 02:09:04PM +0100, Markus Bernhardt wrote:
> void table_set_param(Table *table, char *name, int count, long index) {
>   int i;
>   for(i = 0; i < TABLE_NAME_LENGTH; i++) {
>     table->name[i] = '\0';
>   }

    Wouldn't:

    memset(table->name, 0, TABLE_NAME_LENGTH);

    be nicer?  Possibly not as fast, I suppose...

>   strncpy(table->name, name, TABLE_NAME_LENGTH-1);

    Are you certain that table->name and name are *always* independent
variables?  Note from the strncpy man page:

       The
       strings  may not overlap, and the destination string dest must be
       large enough to receive the copy.

> Breakpoint 3, table_set_param (table=0x60000ffffffedec0, name=0x4000000000022808 "TSTICHWORT", count=2, index=6917546619827052592) at suchmaschine/table.c:21
> 21        strncpy(table->name, name, TABLE_NAME_LENGTH-1);
> (gdb) print name
> $1 = 0x4000000000022808 "TSTICHWORT"
> (gdb) print table->name
> $6 = '\0' <repeats 254 times>

We need to know what the hex value of the table->name pointer is at this
point...

Tim

-- 
Dr Tim Cutts
Informatics Systems Group
Wellcome Trust Sanger Institute, Hinxton, Cambridge, CB10 1SA, UK



Reply to: