why function return more char in string array?

L

linux.lover

hello,
I have defined kernel data structure in LINUX kernel source code
and written linked list functionalities in kernel source file. My data
structure is

struct node {
char index[8];
char str1[8];
struct node *next;
};

this linked list has 2 fields in which one used to index.
I am writing to linked list via new system call implemented
void write_ll(char *buf,char *fn)

which writes to it in kernel as with ptr is linked list object
strncpy(ptr->str1,buf,8);
strcpy(ptr->index,fn);
printk(KERN_INFO " in ptr =%s\n",ptr->str1);

if i send kernel to write to linked list field str1 "XYZDEFGH" (only
always 8 bytes are sending) which it receives same when i try to print
in kernel but when i try to read that i got results as
XYZDEFGH ��?&

I am reading linked list with code in user space
char *out;
out=(char*)malloc(8);
memset(out,'\0',8);
out=display_ll(out,index);

where code for display_ll in kernel is
strcpy(out,ptr->str1);
return out;
 
A

Allan Bruce

linux.lover said:
hello,
I have defined kernel data structure in LINUX kernel source code
and written linked list functionalities in kernel source file. My data
structure is

struct node {
char index[8];
char str1[8];
struct node *next;
};

this linked list has 2 fields in which one used to index.
I am writing to linked list via new system call implemented
void write_ll(char *buf,char *fn)

which writes to it in kernel as with ptr is linked list object
strncpy(ptr->str1,buf,8);
strcpy(ptr->index,fn);
printk(KERN_INFO " in ptr =%s\n",ptr->str1);

if i send kernel to write to linked list field str1 "XYZDEFGH"

You have defined your struct to hold 8 bytes and have stored 8 visible chars
but you are forgetting about the null terminator, "XYZDEFGH" is actually
nine char long:
{'X', 'Y', 'Z', 'D', 'E', 'F', 'G', 'H', '\0'}

You need to allow storage for the \0 at the end of strings.
Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top