LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-18-2009, 02:45 PM   #1
hjc
LQ Newbie
 
Registered: Oct 2009
Posts: 8

Rep: Reputation: 0
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;
}
 
Old 10-18-2009, 03:45 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,330

Rep: Reputation: 227Reputation: 227Reputation: 227
Quote:
Originally Posted by hjc View Post
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.
 
Old 10-18-2009, 05:03 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by smeezekitty View Post
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 View Post
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.
 
Old 10-18-2009, 05:59 PM   #4
hjc
LQ Newbie
 
Registered: Oct 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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;
}

Last edited by hjc; 10-18-2009 at 06:23 PM.
 
Old 10-18-2009, 10:07 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by hjc View Post
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.
 
Old 10-19-2009, 01:05 AM   #6
hjc
LQ Newbie
 
Registered: Oct 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 10-19-2009, 01:16 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 10-19-2009, 04:19 AM   #8
hjc
LQ Newbie
 
Registered: Oct 2009
Posts: 8

Original Poster
Rep: Reputation: 0
its okk i used the lengthy method by sprintf. thanks to everyone...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
releasing memory allocated to a process aloktiagi Linux - Newbie 1 08-25-2009 07:56 AM
[SOLVED] Memory leak: How risky not to free allocated memory. kaz2100 Linux - General 1 12-24-2008 01:00 AM
confirmaion for the allocated memory presence chakka.lokesh Programming 2 08-14-2008 12:46 AM
Need help: Seg fault, Memcpy, and dynamically allocated arrays benobi Programming 3 06-09-2005 11:58 PM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 03:56 AM


All times are GMT -5. The time now is 02:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration