Discussion in "Software" started by    bhagirath    Jul 5, 2011.
Thu Jul 07 2011, 10:33 am
#11
Hi ExperimenterUK,

thank you for reply,

finally I managed it to bypass the error some how, for time being i will be giving direct indexes for the segments, which doesn't throws error, thanks to ExperimenterUK and kirangowle for their help. I managed the code as

void main()
{
    strncpy (var1,str+0,1);
    strncpy (var2,str+1,2);
    strncpy (var3,str+2,3);
    strncpy (var4,str+3,7);
    strncpy (var5,str+8,12);
    strncpy (var6,str+12,13);
   	strncpy (var7,str+13,14);
	while(1);
}
Fri Jul 08 2011, 01:04 am
#12
Hello bhagirath
You might be able to avoid errors somehow but it still won't work.
Take "strncpy (var7,str+13,14);" for example.

Here you copy 14 bytes from near the end of 'str' into var7.
In your last code var7 was only 1 byte long, so you will write one byte
into var7 and 13 bytes into memory used for something else.

Because you are copying from near the end of 'str' most of the bytes will come from
memory that could contain anything.
If 'str' is zero terminated,strncpy will fill the count with null bytes


[ Edited Sat Jul 09 2011, 02:13 am ]
Fri Jul 08 2011, 12:01 pm
#13
Hi ExperimenterUK,

With reference to "Take "strncpy (var7,str+13,14);" for example.". This function only returns 1 byte thats 13th. Here in the function parameter 13 and 14 are the indexes of characters in string, i.e. var7 would hold the character from 13th byte to 14th, as str will hold base address + 13 would make it to t13th character.

I have debugged the code and I am getting same values, One more thing you might be right about the junk values as even though my vars are delimited to few characters but by adding them to watch you may get few characters extra added to them, but when exact values are accessed there are no junk values. Kindly see the attached screen shot. Could you please explain this behavior.








Sat Jul 09 2011, 02:22 am
#14


With reference to "Take "strncpy (var7,str+13,14);" for example.". This function only returns 1 byte thats 13th. Here in the function parameter 13 and 14 are the indexes of characters in string, i.e. var7 would hold the character from 13th byte to 14th, as str will hold base address + 13 would make it to t13th character.

bhagirath



char * strncpy ( char * destination, const char * source, size_t num );

Actually strncpy returns a char *(character pointer),probably two bytes in your case.
It transfers the count in "num" from char * source to char * destination.
None of the values passed to strncpy are indexes.


Sat Jul 09 2011, 06:23 pm
#15
@ bhagirath
ru using a trail version
i has doubt
Mon Jul 11 2011, 10:48 pm
#16
dont bother about the display in string as your strings in var are not null terminated so compiler show it as a single string rather than 2 separate string. just see that variables are having correct characters.
Tue Jul 12 2011, 01:29 pm
#17
Hi all,

Ajay Bhargav, thanks, now I got it.

Majoka, Yes its trial version, what is your doubt, could you please specify that...?

ExperimenterUK, thanks for helping me.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am