1

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 the pointer enough as it is already malloced and the threads share the same memory space?

1
  • You don't need the cast. void* is assignment compatible with all pointers to object.
    – pmg
    May 6, 2011 at 10:04

5 Answers 5

1

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.

2
  • Thing is, I dont have to read them from different threads. Im doing the following, taking the argument, sending the new pointer to a watchdog, watchdog creates a gps navigation thread and passes in the pointer. then only gps nav uses it. My question is more about if the struct data will be safe to pass the pointer around the threads seeing that a new pointer has been created in this system from the copied data that was pointed to by an outside system.(subsystem running on the same board).
    – some_id
    May 6, 2011 at 10:20
  • @Helium Yes the your "destination" pointer is safe in that its pointing to memory you are mananging - the freeing of the org memory pointed to by dest will not effect whats pointed to by destination.
    – Ricibob
    May 6, 2011 at 10:31
1

You are copying the pointer itself.

destination = dest;

So now destination will point to what dest points to.

0
1

It won't work as intended, it will just assign the pointer and you'll lose malloced 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.

3
  • So I can then access the same data passed in, with destination?
    – some_id
    May 6, 2011 at 9:39
  • Yes, but they will be a copy of the original data (the ones pointer by dest)
    – Simone
    May 6, 2011 at 9:40
  • Yes, if you they write to the struct. Otherways, if you don't want to copy it, you need to synchronize the access to the struct's field.
    – Simone
    May 6, 2011 at 10:07
1

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).

1
  • I find it easier because of the alignment (the malloc isn't "hiding away" to the right) and the absence of the * in the assignment (really shows what object changes value).
    – pmg
    May 6, 2011 at 9:51
1

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.

2
  • Thanks. I am aware or mutexes/semaphores, but they arent needed here. :)
    – some_id
    May 6, 2011 at 10:21
  • @Helium: cool - just thought I better throw the subject out there in case... :)
    – Mac
    May 6, 2011 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.