tags:

views:

167

answers:

6

I'm using a char[] of size 4 but when I use memcpy() function it stores 8 characters in it and also the character array length becomes 8. What is happing?

I don't want to use malloc ok.

char strRoh[4]={'\0'};

and then

memcpy(strRoh,Dump+22,4);

Now tell me whats wrong with this

char strIP[]="hhhhhhhh";
char strRoh[4]={'\0'};
char strTheta[4]={'\0'};
char strTimeStamp[6]={'\0'};
char strNMDump[48]={'\0'};  

is there any problem with decelerations cause when i change there order they strings also change there size now strroh is getting 10 chars

what a hell is going on with this

+3  A: 

The underlying type of the objects pointed by both the source and destination pointers are irrelevant for memcpy; The result is a binary copy of the data.

The function does not check for any terminating null character in source - it always copies exactly num bytes. My guess is you are not adding a terminating null and trying to access it as a string.

JRL
A: 

I suppose that if char has 2 bytes if you memcpy to a byte array you might be getting 8 bytes, that is 2 bytes for each char.

I am however rusty at this C/C++ things. So hopefully somebody with more experience will give you a better answer.

Mihai Lazar
+1  A: 

C does not have any kind of boundary check on its data types.

So what you are probably "seeing" when debugging the code is that it shows you 8 bytes in the array. As someone else says, you might be trying to view it as a string and do not have a terminating zero byte. This is quite normal in C, and it is one of the aspects of the language that makes it very hard to understand.

I can recommend you read a good introduction to memory and pointer handling under C, or switch to a managed language like C#, VB.NET, Java, Perl, Python etc.

Cine
A: 

The problem is you have a char array of 4 bytes and you writing full 4 bytes during memcpy without leaving any space for the terminating null character. Declare your array as 5 bytes and initialize it all to null (which you are already doing) and everything should be fine.

Jay
A: 

(sorry, misread the listing)

Yoric
+5  A: 

C strings are 0-terminated. This means that if you want to have a string of length n in C, you need n+1 chars for it:

char hello[5] = "hello";

is not a string, because hello has space for 5 chars, and it doesn't end with 0.

char hello[6] = "hello";

is a string, and has 6 characters: h, e, l, l, o, 0.

To be able to use string related functions in C, you need the terminating 0.

So, change your code to have:

char strRoh[5]={'\0'};
char strTheta[5]={'\0'};
char strTimeStamp[7]={'\0'};
char strNMDump[49]={'\0'};

Note that in C, when you do:

char hello[] = "hello";

the compiler does the counting for you, and makes hello an array of size 6 (one terminating 0):

printf("%zu\n", sizeof hello);

will print 6.

Alok
ok thanx i have done it before you ans but you got the point now i take the size 5 instead of 4 but string length is still 4 and it works for me any how problem is solved.
moon