Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   memcpy() and uint8_t (http://www.go4expert.com/forums/memcpy-uint8t-t20691/)

katty 18Jan2010 15:11

memcpy() and uint8_t
 
Hello folks..

In the following code, I m trying to copy struct element to array...


Code:

#include<iostream>
#include<stdint.h>
#include<string>

using namespace std;
struct test
{
        struct header
        {
                int a;
                int b;
        };
        uint8_t c;
        uint8_t d;
        uint8_t e;
        uint32_t f;
};
struct test2
{
        uint8_t n;
        uint8_t m;
        uint8_t p;
};


int main()
{
        test headd;
        test2 data;
        uint8_t* d;
        int x, y,i;

        x= sizeof(test);
        y= sizeof(test::header);
        i= sizeof(data);

        x=x+y+i;
        d = new uint8_t[x+10];
        cout<<"x ="<<x<<endl;

/** Assign **/
        headd.c = 4;
        headd.d = 5;

        data.n=8;
        data.m=7;
        data.p=9;

/** copy 2 elements of struct 'headd' and struct 'data' on to memory block pointed by 'd' **/

        memcpy(d,&headd.c,8);
        cout<<"d="<<*d<<endl;
        cout<<"d2="<<*(d+1)<<endl;

        memcpy(d+8,&data, 8);
        cout<<"d="<<*d<<endl;
        cout<<"d2="<<*(d+2)<<endl;

        return 0;
}

OUTPUT:
x =19
d=
d2=
d=
d2=


I ve replaced uint8_t and uint32_t by datatype 'int' and it works well... i can display contents of 'd'
but not for uint8_t and uint32_t .
Please suggest me how can i check whether data successfully copied to destination?


Thank You

Gene Poole 18Jan2010 19:36

Re: memcpy() and uint8_t
 
I'm not sure what you are doing, but it might be a packing issue. By default, MS compilers line-up data in structure to an 8 byte boundary (makes it easier on the memory controller). You can override this behavior with the #pragma pack option:

Code:


#pragma pack(push,1)
struct test
{
        struct header
        {
                int a;
                int b;
        };
        uint8_t c;
        uint8_t d;
        uint8_t e;
        uint32_t f;
};
struct test2
{
        uint8_t n;
        uint8_t m;
        uint8_t p;
};
#pragma pack(pop)


katty 19Jan2010 12:16

Re: memcpy() and uint8_t
 
I want to copy struct test (except struct header) and struct test2 on to array pointed by 'd'

I 'd put printf() to display *d instead of cout... its working

Thanks for early reply


Quote:

Originally Posted by Gene Poole (Post 63091)
I'm not sure what you are doing, but it might be a packing issue. By default, MS compilers line-up data in structure to an 8 byte boundary (makes it easier on the memory controller). You can override this behavior with the #pragma pack option:

Code:


#pragma pack(push,1)
struct test
{
        struct header
        {
                int a;
                int b;
        };
        uint8_t c;
        uint8_t d;
        uint8_t e;
        uint32_t f;
};
struct test2
{
        uint8_t n;
        uint8_t m;
        uint8_t p;
};
#pragma pack(pop)




All times are GMT +5.5. The time now is 07:35.