LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   memcpy from structure to a allocated memory (https://www.linuxquestions.org/questions/linux-newbie-8/memcpy-from-structure-to-a-allocated-memory-762805/)

hjc 10-18-2009 01:45 PM

memcpy from structure to a allocated memory
 
Hi,
I have an allocated memory called shared_memory and a struct called shared_data. i want to copy contents from struct shared_data to shared_memory using memcpy function. Then i want to print the contents from shared memory. can somebody help? in my actual code shared_memory is of size 4k but shared_data is of a few bytes. so I want to copy these few bytes into a larger memory. later i want to print the entire contents of the shared_memory.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
char * shared_memory;

typedef struct{
long int fib_sequence[10];
int sequence_size;
char a;
}shared_data;

shared_data x;
int i;

x.sequence_size=10;
x.fib_sequence[0]=10;
x.fib_sequence[1]=20;

for(i=2;i<10;i++)
x.fib_sequence[i]=0;
x.a=a;

shared_memory = (char *)malloc(sizeof(shared_data));
if(!*shared_memory)
printf("out of memory");

//want to copy through memcpy contents of struct shared_data to //shared_memory(allocated memory)
memcpy(&shared_memory,&x,sizeof(shared_data));

//i want to printf the contents of entire shared_memory
printf("%d",shared_memory);// i don't know how to print the //contents

free(shared_memory);
return 0;
}

smeezekitty 10-18-2009 02:45 PM

Quote:

Originally Posted by hjc (Post 3724002)
Hi,
I have an allocated memory called shared_memory and a struct called shared_data. i want to copy contents from struct shared_data to shared_memory using memcpy function. Then i want to print the contents from shared memory. can somebody help? in my actual code shared_memory is of size 4k but shared_data is of a few bytes. so I want to copy these few bytes into a larger memory. later i want to print the entire contents of the shared_memory.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
char * shared_memory;

typedef struct{
long int fib_sequence[10];
int sequence_size;
char a;
}shared_data;

shared_data x;
int i;

x.sequence_size=10;
x.fib_sequence[0]=10;
x.fib_sequence[1]=20;

for(i=2;i<10;i++)
x.fib_sequence[i]=0;
x.a=a;

shared_memory = (char *)malloc(sizeof(shared_data));
if(!*shared_memory)
printf("out of memory");

//want to copy through memcpy contents of struct shared_data to //shared_memory(allocated memory)
memcpy(&shared_memory,&x,sizeof(shared_data));

//i want to printf the contents of entire shared_memory
printf("%d",shared_memory);// i don't know how to print the //contents

free(shared_memory);
return 0;
}

Code:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct{
                long int fib_sequence[10];
                int sequence_size;
                char a;
        }shared_data;
shared_data_type(x) (shared_data)(((shared_data*)(&x))[0])
int main()
{
        char * shared_memory;

       

        shared_data x;
        int i;

        x.sequence_size=10;
        x.fib_sequence[0]=10;
        x.fib_sequence[1]=20;

        for(i=2;i<10;i++)
                x.fib_sequence[i]=0;
        x.a=a;

        shared_memory = (char *)malloc(sizeof(shared_data));
        if(!*shared_memory)
                printf("out of memory");

        //want to copy through memcpy contents of struct shared_data to //shared_memory(allocated memory)
        memcpy(&shared_memory,&x,sizeof(shared_data));       

        //i want to printf the contents of entire shared_memory
        printf("%d",shared_data_type(shared_memory).sequence_size);//print the sequence size
        free(shared_memory);
        return 0;
}

please use code tags next time.

johnsfine 10-18-2009 04:03 PM

Quote:

Originally Posted by smeezekitty (Post 3724041)
please use code tags next time.

Very good point. But please don't quote the entire point next time and please don't rewrite the OP's code without explaining anything nor fixing the bugs.

Quote:

Originally Posted by hjc (Post 3724002)
I have an allocated memory called shared_memory and a struct called shared_data. i want to copy contents from struct shared_data to shared_memory using memcpy function. Then i want to print the contents from shared memory. can somebody help? in my actual code shared_memory is of size 4k but shared_data is of a few bytes. so I want to copy these few bytes into a larger memory. later i want to print the entire contents of the shared_memory.

All that is unclear enough that I can't tell you what to do even if I thought that was appropriate.

I can point out a few specific bugs in the code you posted:

Code:

        x.a=a;
I don't see any declaration for a so that line shouldn't compile. What did you ntend it to do?

Code:

        if(!*shared_memory)
You shouldn't have that *. malloc() returned a pointer. The out of memory case is when the pointer itself is zero, not when the memory pointed to is zero.

Code:

        memcpy(&shared_memory,&x,sizeof(shared_data));
For the same reason, you don't want that &.

You need to learn the concepts behind pointers, including why the & is required for x but incorrect for shared_memory.

Beyond that, there is plenty more wrong. But harder to explain since I don't know what you're actually trying to do.

hjc 10-18-2009 04:59 PM

Reply to johnsfine...
Thanks for noticing the differences(errors). I appreciate it.
1)i corrected the x.a='a'; my idea is to have different data types in the structure so that i can wrtie different types of data into shared_memory which has to be a character type pointer.
2) corrected the if(!shared_memory)removed the *. understood the mistake
3) in the memcpy function u said I am not supposed to use &shared_memory.
i want to copy the contents from structure variable x to the shared memory.
shared_memory is a character type of pointer. i don't know how to go abt doing this using memcpy.

Also I want to print the contents of shared_memory which may be of different types, character , integer etc..

for eg with the below piece of code i can print contents of shared_memory as "hi there"...here i used sprintf since its string... i tot memcpy was a better function to use because the structure has data of diff types.. pls give some idea..
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main()
{
char * shared_memory;

typedef struct{
long int fib_sequence[10];
int sequence_size;
char a;
}shared_data;

char hi[100]="Hi there";

shared_data x;
int i;

x.sequence_size=10;
x.fib_sequence[0]=10;
x.fib_sequence[1]=20;

for(i=2;i<10;i++)
x.fib_sequence[i]=0;
x.a='a';

shared_memory = (char *)malloc(sizeof(shared_data));
if(!shared_memory)
printf("out of memory");

sprintf(shared_memory,hi);
printf("%s",shared_memory);

free(shared_memory);
return 0;
}

johnsfine 10-18-2009 09:07 PM

Quote:

Originally Posted by hjc (Post 3724168)
3) in the memcpy function u said I am not supposed to use &shared_memory.
i want to copy the contents from structure variable x to the shared memory.
shared_memory is a character type of pointer. i don't know how to go abt doing this using memcpy.

Just remove the & that I had marked in red. Otherwise the memcpy was correct.

Quote:

Also I want to print the contents of shared_memory which may be of different types, character , integer etc..
I don't understand that because I'm trying to interpret it as something possible.

But I think maybe you mean something impossible.

Any part of memory in a computer is just a sequence of bytes. The types are not stored in the bytes of needed. A computer program needs to get the types from somewhere else in order to interpret the data.

So the part of the program that does the printing needs to know the types it should print.

In your first example, you know you copied a shared_data object into shared_memory, so you can easily cast the shared_memory pointer as (shared_data*) in the code that does the printing. But I think you are asking for something more general that isn't actually possible.

hjc 10-19-2009 12:05 AM

memcpy(shared_memory,&x,sizeof(shared_data));
Actually with the memcpy when i did a trace on the code i don't see the data copied from x to shared_memory.
Is there something wrong. I am not able to figure that.

Wim Sturkenboom 10-19-2009 12:16 AM

No solution

There is a dedicated programming section. You might want to consider to ask a moderator to move it by reporting your first post.

And can you please (as suggested by smeezekitty) put your code between [code] and [/code]. This will preserve indentations and will make it easier to read.

hjc 10-19-2009 03:19 AM

its okk i used the lengthy method by sprintf. thanks to everyone...


All times are GMT -5. The time now is 06:04 AM.