Thread: memcpy(msg->data+msg->dsize, ..)

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    Question memcpy(msg->data+msg->dsize, ..)

    memcpy function is used for copy to dst from src with size:
    Code:
    memcpy(dst, src, size)
    If msg structure was defined as follow:
    Code:
    struct msg{
           char data[100];
           int dsize;
    }
    and buf defined as follow:
    Code:
    struct buf{
           char *data;
           int len;
    }
    Now if i run below command:
    Code:
    memcpy(msg->data+msg->dsize, buf->data, buf->len)
    Does copy buf->data to msg->data? If yes, what is effect of msg->dsize?

    I saw this in a C project.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    I tried what you are coming to say.

    memcpy will copy the bytes from source to destination.

    If buf->data is copied to msg->data then msg->dsize should be zero. otherwise it will point (buf->data+msg->dsize) to some other memory location. So it may give segmentation fault.

    Try the following code to understand about memcpy.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    
    struct msg{
           char data[100];
           int dsize;
    };
    
    struct buf{
           char *data;
           int len;
    };
    
    int main()
    {
            char str1[]="hello";
            char str2[10];
            struct msg *m1;
            struct buf *b1;
            b1=(struct buf *)malloc(sizeof(struct buf));
            m1=(struct msg *)malloc(sizeof(struct msg));
            b1->data="hello";
            b1->len=strlen(b1->data);
            m1->dsize=0;
            memcpy(str2,str1,10);
            memcpy(m1->data+m1->dsize, b1->data, b1->len);
            printf("str1:%s str2:%s\n",str1,str2);
            printf("str1:%s str2:%s\n",b1->data,m1->data);
            return 0;
    }
    Last edited by sganesh; 03-03-2010 at 02:57 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    In this code it means that copying the data in the source to the destination's specific offset.
    Usually if you simply used msg->data as a destination in the memcpy function it will be copied to the beginning.
    Since array name is the address of the starting address. But we are now incrementing the pointer to some other offset using msg->dsize

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Since structure members occupies the memory in a sequential order ,
    msg->data+msg->dsize this statement defined the location which is a bound of the structure
    msg.

    so msg->data and msg->dsize has their value unedited.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Quote Originally Posted by sganesh View Post
    I tried...
    ...
    Thank you.

    if change size of msg->data from 100 to 2 and then try to copy buffer to msg, msg->dsize will be
    invalid. I don't know what msg->dsize will has, I only know it will be invalid.

    If change your code to it,
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    
    struct msg{
           char data[2];
           int dsize;
    };
    
    struct buf{
           char *data;
           int len;
    };
    
    int main()
    {
            char str1[]="asdasd";
            char str2[5];
            struct msg *m1;
            struct buf *b1;
            b1=(struct buf *)malloc(sizeof(struct buf));
            m1=(struct msg *)malloc(sizeof(struct msg));
            b1->data="asdasd";
            b1->len=strlen(b1->data);
            m1->dsize=0;
            memcpy(str2,str1,5);
            memcpy(m1->data+m1->dsize, b1->data, b1->len);
            printf("str1:%s str2:%s\n",str1,str2);
            printf("b1:%s m1:%s\n",b1->data,m1->data);
            printf("m1->dsize:%d\n",m1->dsize);
            return 0;
    }
    and test it in two way, first with:
    Code:
     memcpy(m1->data+m1->dsize, b1->data, b1->len);
    second replace it with:
    Code:
     memcpy(m1->data/*+m1->dsize*/, b1->data, b1->len);
    In both, msg->dsize will be invalid.

    My question was that what is effect of adding m1->dsize to first parameter of memcpy?

    Isn't better at first check buf->len and msg->dsize then copy it?
    Code:
    memcpy(msg->data+msg->dsize, buf->data, buf->len)
    Why using msg->dsize?

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: memcpy(msg->data+msg->dsize, ..)

    I just wrote a program for copying the strings in the structure,

    If you want to copy the whole structure you can use the size of the structure in the memcpy.
    It will copy that many number of bytes from one structure to another.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    
    struct msg{
           char data[100];
           int dsize;
    };
    
    struct buf{
           char data[100];
           int len;
    };
    
    int main()
    {
            char str1[]="hello";
            char str2[10];
            struct msg *m1;
            struct buf *b1;
            b1=(struct buf *)malloc(sizeof(struct buf));
            m1=(struct msg *)malloc(sizeof(struct msg));
            strcpy(b1->data,"hello");
            b1->len=strlen(b1->data);
    //      m1->dsize=0;
            memcpy(str2,str1,10);
            memcpy(m1->data, b1->data, sizeof(struct buf));
            printf("str1:%s str2:%s\n",str1,str2);
            printf("str1:%s size:%d str2:%s size:%d \n",b1->data,b1->len,m1->data,m1->dsize);
            return 0;
    }
    Last edited by sganesh; 03-03-2010 at 06:16 AM.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Quote Originally Posted by sganesh View Post
    I just wrote a ..
    ...
    you don't ask answer of my question. I ask why using msg->dsize in first parameter of memcpy?

    your written code only copy all one structure to another one.
    however, i thank you for it.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    Quote Originally Posted by securelinprog View Post
    memcpy function is used for copy to dst from src with size:
    Code:
    memcpy(dst, src, size)
    If msg structure was defined as follow:
    Code:
    struct msg{
           char data[100];
           int dsize;
    }
    and buf defined as follow:
    Code:
    struct buf{
           char *data;
           int len;
    }
    Now if i run below command:
    Code:
    memcpy(msg->data+msg->dsize, buf->data, buf->len)
    Does copy buf->data to msg->data? If yes, what is effect of msg->dsize?

    I saw this in a C project.
    Actually, I didn't use the msg->dsize as a first parameter of memcpy() I think you only used in your question.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Ok, right.

    I understand why using msg->dsize in the memcpy function.

    By using it, can copy all buf structure to msg->data and msg->dsize simultaneously.

    Thanks to all specially sganesh.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by securelinprog View Post
    I understand why using msg->dsize in the memcpy function.

    By using it, can copy all buf structure to msg->data and msg->dsize simultaneously.
    No. msg->dsize is used as an offset from the start of msg->data.
    Imagine having the following buffers
    Code:
    msg->data : { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ( 10 zeroes)
    buf->data : { 1, 1, 1 } (3 ones)
    And then you have this code
    Code:
    memcpy(msg->data+msg->dsize, buf->data, buf->len)
    If dsize is 0 then the result is
    msg->data : { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }

    If dsize is 4 then the result is
    msg->data : { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Quote Originally Posted by _Mike View Post
    No. msg->dsize is used as an offset from the start of msg->data.
    Imagine having the following buffers
    Code:
    msg->data : { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ( 10 zeroes)
    buf->data : { 1, 1, 1 } (3 ones)
    And then you have this code
    Code:
    memcpy(msg->data+msg->dsize, buf->data, buf->len)
    If dsize is 0 then the result is
    msg->data : { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }

    If dsize is 4 then the result is
    msg->data : { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }
    Thank you _Mike.

    Which parameter determine dsize was 4 or 0? Does size of msg->data and buf->data is effective?

    Thank you very much.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by securelinprog View Post
    Which parameter determine dsize was 4 or 0?
    dsize is whatever you set it to be in your code.. the 0 and 4 were just random numbers I used as an example.

    Does size of msg->data and buf->data is effective?
    the size of msg->data has to be at least the size of buf->data + msg->dsize or you would get a buffer overrun.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Thank you _Mike. I understood well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 02:45 PM
  2. Display text in a Dialog Box
    By Dark_Phoenix in forum Windows Programming
    Replies: 9
    Last Post: 01-02-2009, 06:30 AM
  3. Creating a Window Class
    By Dark_Phoenix in forum Windows Programming
    Replies: 2
    Last Post: 12-07-2008, 07:45 PM
  4. the handle is invalid
    By m37h0d in forum C++ Programming
    Replies: 17
    Last Post: 07-09-2008, 11:29 PM
  5. need help declaring an msg
    By kwm32 in forum Windows Programming
    Replies: 6
    Last Post: 03-24-2004, 02:49 PM

Tags for this Thread