【问题标题】:Typedef struct pointers correctly pointing to typedef struct (C)Typedef struct 指针正确指向 typedef struct (C)
【发布时间】:2015-10-17 23:37:24
【问题描述】:

我正在尝试将 malloc 的 ptr 保存在全局 ptr 中,以便可以在另一个函数中使用它。理想情况下,我会在全球范围内建立一个更智能的数据结构,但现在我只是想让全球 ptr 工作。

在我的 lwp.h 文件中,我有以下定义:

typedef struct threadinfo_st *thread;
typedef struct threadinfo_st {
   tid_t         foo1
   unsigned long foo2
   size_t        foo3
   rfile         foo4
   thread        foo5
   thread        foo6
   thread        foo7
   thread        foo8
} context;

使用这个线程结构,我的 lwp.c 文件中有两个函数。在第一个函数中,我构造了一个 malloc 线程,然后将数据复制到全局 ptr。然后在第二个函数中,我尝试取消引用全局 ptr 以接收我最初创建的线程。为了确认我这样做是正确的,我在每一步都打印出 ptr 地址。不幸的是,我似乎无法恢复原来的地址,因此我在第二个函数中的所有数据都被转移了

static thread headThread = NULL;

void create(){
    thread newThread = (thread) malloc( sizeof(thread));
    assert(newThread != NULL)
    // Assign junk to newThread
    printf("newThread is at: %p, headThread is at: %p\n", &newThread, &headThread);
    headThread = newThread;
}

void start(){
    thread newThread = headThread;
    printf("newThread is at: %p, headThread is at: %p\n", &newThread, &headThread);
}

在 main 中调用 create() 然后 start() 打印出来:

 newThread is at: 0x7ffdf085c5e0, headThread is at: 0x601890
 newThread is at: 0x7ffdf085c5d8, headThread is at: 0x601890

导致我在 start() 函数的 newThread 中的所有数据都被转移。

我还尝试了以下方法:

static thread *headThread = NULL;

void create(){
    thread newThread = (thread) malloc( sizeof(thread));
    assert(newThread != NULL)
    // Assign junk to newThread
    printf("newThread is at: %p, headThread is at: %p\n", &newThread, &headThread);
    headThread = &newThread;
}

void start(){
    thread newThread = headThread;
    printf("newThread is at: %p, headThread is at: %p\n", &newThread, &headThread);
}

打印出来:

newThread is at: 0x7ffff6294130, headThread is at: 0x601890
newThread is at: 0x7ffff6294128, headThread is at: 0x601890

有谁知道我在这种情况下到底做错了什么? 感谢您的帮助!

【问题讨论】:

  • thread newThread = (thread) malloc( sizeof(thread)) 没有意义。您正在尝试分配一个结构,而不是一个指针,所以 sizeof(thread) 是错误的。更好:thread newThread = malloc(sizeof *newThread); 最好:不要将指针隐藏在 typedef 后面。
  • 谢谢,我正在处理提供给我的用于多线程分配的头文件。我将把这个告诉我的教授,为什么他选择以这种方式构建它。感谢您的帮助!
  • 请不要从您的问题中删除内容。你的问题仍然存在,否则答案没有意义。您的问题的旧版本仍然在网站上可见,只是从默认视图中隐藏。如果您不小心发布了机密数据,您可以将其删除,但首先您需要以现有答案仍然有意义的方式编辑您的问题,然后标记您的问题并要求旧版本已被删除 - 但请记住,它们已被 Google 索引。不过,我在您的原始问题中看不到任何机密内容。

标签: c multithreading pointers struct


【解决方案1】:

int 两个函数 start() 和 create() 你正在打印的地址:

全局变量 headThread 是 &headThread=0x601890 并保持不变,因为它每次都相同(全局)

但是变量 newThread 是在本地(在堆栈中)声明给这两个函数的每一个。

start() 中的newThread 不是create() 中的,它们在内存(堆栈)中有不同的地址0x7ffff6294130 不是0x7ffff6294128。

要获得分配的 ptr,请执行以下操作:

printf("newThread is at: %p, headThread is at: %p\n", newThread, headThread);

那么这是正确的:

static thread headThread = NULL;

不是这个:

static thread *headThread = NULL;

最终正确的 malloc 是这样的:

thread newThread = malloc( sizeof(threadinfo_st)); //better use calloc

【讨论】:

  • 使用 start 函数中的 newThread,每当我尝试访问任何字段时,都会出现堆栈溢出错误。我该如何解决这个问题?
  • 您必须提供完整的代码。堆栈溢出让我们认为有一个递归函数会产生错误。但只有提供完整的代码才能确定
【解决方案2】:

正如 melpomene 正确指出的那样,您正在分配指向上下文(称为线程)的指针,而不是上下文本身。

然后,在这两个函数中,您都在打印存储这些指针的地址(而不是指针本身)。因此,对于静态对象(headThread)它们是相同的,对于自动对象(newThread,每次重新分配在堆栈上)它们是不同的。 这里似乎有什么问题?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    相关资源
    最近更新 更多