指针之间的数据

发布于 2024-11-05 04:09:34 字数 773 浏览 1 评论 0原文

如果执行以下操作,将复制指针中的实际数据还是指针本身?

void nav_runGpsSystem(GPSLocation *dest)
{   
    GPSLocation *destination = malloc(sizeof(GPSLocation));
    destination = dest;

其中 GPSLocation 如下

typedef struct
{
    double latitude;
    double longitude;
}
GPSLocation;

问题是正在创建的 GPSLocation(指针)是在使用它的子系统外部创建的,但它仍在同一板上(运行 ubuntu 上网本版本的 pandaboard)。我不希望指针丢失数据以某种方式损坏,所以我想复制第一个指针指向的数据,以便创建它的系统可以在需要时释放指针。

编辑

阅读答案后很明显我必须使用取消引用 *destination = *dest

那么如果我想将数据传递给线程呢

void *startgpswatchdog(void *ptr)
{
    GPSLocation *destination;
    destination = (GPSLocation *) ptr;

如果我现在将数据复制到系统中,是否需要在 pthread 中再次复制数据或仅仅传递指针就足够了,因为它已经被分配并且线程共享相同的内存空间?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚笙 2024-11-12 04:09:34

您将目标分配给一个新的内存块,但随后指向远离该块的位置(内存泄漏 - 您无法取回该内存)并指向现有的目标对象。 dest 和destination 是指向同一个对象的两个指针。
如果要复制对象,则需要取消引用:*destination = *dest;
但是,您需要以某种方式跟踪目标 ptr,以便稍后可以释放分配的内存 - 从您的示例来看,不清楚您打算如何做到这一点。
那么您是否暗示您要以多个线程访问该对象? - 这里要小心,您需要使用互斥锁或临界区来保护对象,以避免并发访问。

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.

折戟 2024-11-12 04:09:34

您正在复制指针本身。

destination = dest;

所以现在目的地将指向目的地所指向的内容。

You are copying the pointer itself.

destination = dest;

So now destination will point to what dest points to.

墨落成白 2024-11-12 04:09:34

它不会按预期工作,它只会分配指针,并且您将丢失malloced内存。

您需要取消引用指针:

void nav_runGpsSystem(GPSLocation *dest)
{   
    GPSLocation *destination = malloc(sizeof(GPSLocation));
    *destination = *dest;
}

这应该足够了。

编辑
这很好,因为线程共享相同的内存空间。不过,请确保每个线程都有原始结构的不同副本。

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.

你与清晨阳光 2024-11-12 04:09:34

如果您将定义和初始化分成两行,可能会更容易“看到”它不起作用

void nav_runGpsSystem(GPSLocation *dest) {
    GPSLocation *destination;
    destination = malloc(sizeof(GPSLocation));
    destination = dest; /* overwrite malloc return value and leak memory */

您的编辑没问题(除了多余的转换)。

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

烙印 2024-11-12 04:09:34

destination = dest;复制数据,而是导致 destination 指向与 dest 相同的数据代码>.内存中仍然只有一份数据副本,只是现在 dest destination 都指向它。

您需要做的是取消引用两个指针:

*destination = *dest;

这实际上是说,“获取 dest 指向的数据并将其复制到 destination 引用的位置”。

编辑:回答问题编辑:您的线程都引用内存中的相同数据是完全可以的,因此除非您有特殊原因为每个线程制作副本,否则什么你有就好了。不过,您确实需要注意同步问题 - 如果两个线程尝试同时更改共享数据会发生什么?如果这是一个问题,您必须研究诸如 互斥体信号量

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文