Data between pointers
If the following is being done, will the actual data in the pointer be copied or the pointer itself?
void nav_runGpsSystem(GPSLocation *dest)
{
GPSLocation *destination = malloc(sizeof(GPSLocation));
destination = dest;
Where GPSLocation is the following
typedef struct
{
double latitude;
double longitude;
}
GPSLocation;
The issue is the GPSLocation
that is being created, the pointer, is created outside of the subsystem that uses it, but its still on the same board(pandaboard running ubuntu netbook edition). I dont want data to be lost of pointers to get damaged somehow, so I want to copy the data that the first pointer was pointing to so that the system that created it can free the pointer when they want to.
EDIT
After reading the answers it is clear I must dereference using
*destination = *dest
So what if I then want to pass the data to threads
as so
void *startgpswatchdog(void *ptr)
{
GPSLocation *destination;
destination = (GPSLocation *) ptr;
If I have the data copied into my system now, is there a need to copy the data again inside a pthread or is passing just t开发者_如何学Che pointer enough as it is already malloced and the threads share the same memory space?
You are allocating destination to a new block of memory but then pointing away from that block (a memory leak - you cant get that mem back) and pointing at the existing dest object. dest and destination are two pointers pointing at same object.
If you want to copy the object you need to dereference: *destination = *dest;
But then you need to keep track of desination ptr somehow so you can later free that allocated memory - from your example its not clear how you intend to do that.
You then imply that you are going to multiple threads accessing the object? - careful here you will need to protect the object with a mutex or critical section to avoid concurent access.
You are copying the pointer itself.
destination = dest;
So now destination will point to what dest points to.
It won't work as intended, it will just assign the pointer and you'll lose malloc
ed memory.
You need to dereference pointers:
void nav_runGpsSystem(GPSLocation *dest)
{
GPSLocation *destination = malloc(sizeof(GPSLocation));
*destination = *dest;
}
that should be enough.
EDIT
It's fine since thread share the same memory space. Be sure that each thread has a different copy of the original struct, though.
If you split the definition and initialization in 2 lines, it may be easier to "see" it doesn't work
void nav_runGpsSystem(GPSLocation *dest) {
GPSLocation *destination;
destination = malloc(sizeof(GPSLocation));
destination = dest; /* overwrite malloc return value and leak memory */
Your edit is ok (except for the redundant cast).
The line destination = dest;
does not copy the data, but instead causes destination
to point to the same data as dest
. There is still only one copy of the data in memory, its just that now both dest
and destination
point to it.
What you'll need to do is dereference both pointers:
*destination = *dest;
This effectively says, "take the data pointed to by dest
and copy it to the location referenced by destination
".
EDIT: In response to question edit: it's perfectly fine for your threads to all refer to the same data in the memory, so unless you have a particular reason to make a copy for each of your threads, what you have is fine. You do need to be aware of synchronisation issues though - what happens if two threads try to change the shared data at the same time? If that's an issue, you have to look into things like mutexes, semaphores, etc.
精彩评论