-1

this doesnt works for me:

char buff[11];
char* msg_ptr;
msg_ptr = buff;
uint8_t id; 
uint32_t msg_length;
char msg[] = "hallo";
id = 77;
msg_length = 5;

memcpy(buff, &id, sizeof(uint8_t));
memcpy(buff+1, &msg_length, sizeof(uint32_t));
strcpy(buff+5, msg);


printf("id :%d\n",*(uint8_t*)msg_ptr);
msg_ptr++;

printf("msg_length: %d\n", *msg_ptr);

msg_ptr++;
printf("msg: %s\n", msg_ptr);

Id and msg_lentgh are shown on konsole, but after that i get a segmentation fault. I am new to C please be as detailied as possible. Regards

user1324258
  • 561
  • 2
  • 8
  • 25
  • 1
    This isn't your actual code (for a start, you have `msg_ptr` and `msg_prt`). Please post your **actual** code. – Oliver Charlesworth May 17 '12 at 15:13
  • You still have a typo in there. – Mat May 17 '12 at 15:27
  • Hi, typos are not the problem, they are easy to clear. I want to know my thinking error. I am new to stackoverflow and in the top left corner i noticed that someone commented that my buffer is to small. But it should not be: id(1 byte) + msg_length(4 byte) + msg(5 byte) = 10 byte. – user1324258 May 17 '12 at 15:35
  • @user1324258: Typos **are** a problem. If you don't show your **actual** code, then we can't be sure that we're addressing the **actual** problem when we answer. – Oliver Charlesworth May 18 '12 at 13:13

1 Answers1

0
msg_ptr = msg+1

That's your problem. You should do *msg_ptr++* to go to next position (which is *msg_len*). Instead you change the pointer to point the 'a' within the msg string.

2 more bugs in the code:

  • You don't take into account the terminating '\0' of the string. You have 1 + 4 + 5 + 1, the last one being the terminating '\0'. Also, when working with strings use strncpy, which handles copying the string terminator. memcpy will copy exactly as much as you ask it to. You are corrupting your stack.

  • On the last line you must print the string as a a number. Use %s.

Here's what Wikipedia has to say about C strings and stack overflows while you are at it.

dtatulea
  • 181
  • 3
  • Hi, thanks for the detailed reply. I updated my code. Now the segmentation fault is gone, but msg is null. – user1324258 May 17 '12 at 15:45
  • You didn't update the last msg_ptr correctly. It should be msg_ptr += sizeof(uint32_t) (aka 4). – dtatulea May 17 '12 at 15:49
  • When i do this, i get a segmentation fault again. – user1324258 May 17 '12 at 15:53
  • *buff* doesn't have the right size and you are not printing the string correctly (hint: dereferencing string pointer) – dtatulea May 17 '12 at 15:57
  • Okey size of buff should be 11. I do dereferencing the string pointer. Could you please tell me exactly whats wrong? – user1324258 May 17 '12 at 16:06
  • Ok it works. Why it doesnt works when using (int32_t)msg_ptr++ instaed of msg_ptr+=sizeof(uint32_t) – user1324258 May 17 '12 at 16:15
  • Because you must do ((int32_t *)msg_ptr)++. In your case you were converting the value to int32 and adding one. When converting it to a pointer to an int32, the compiler will do the magic of adding the sizeof(int32). If you had ((int16_t *)msg_ptr)++ it would to the magic of adding 2 to the value of the pointer. – dtatulea May 17 '12 at 16:24
  • [this](http://en.wikipedia.org/wiki/Pointer_(computer_programming)#Typed_pointers_and_casting) should help explain better. – dtatulea May 17 '12 at 16:26
  • Sorry i made a scribal again, i should be (int32_t*)msg_ptr++ instead of (int32_t)msg_ptr++. So i have to put (int32_t*)msg_ptr into brackets before ++. That was the mistake. Thank you very much!! – user1324258 May 17 '12 at 16:33