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; } |
Quote:
Code:
|
Quote:
Quote:
I can point out a few specific bugs in the code you posted: Code:
x.a=a; Code:
if(!*shared_memory) Code:
memcpy(&shared_memory,&x,sizeof(shared_data)); 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. |
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; } |
Quote:
Quote:
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. |
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. |
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. |
its okk i used the lengthy method by sprintf. thanks to everyone...
|
All times are GMT -5. The time now is 06:04 AM. |