访问 void * 结构
我试图在这里找到我的实现中的错误,我将一个结构存储在另一个结构中,并且似乎无法访问存储的值。我定义了两个结构体。
typedef struct {
void * data;
} Element;
typedef struct {
void **buckets;
} Storage;
在一个单独的函数中,我将 key
设置为指向 char
的指针。并将其传入存储在数据中。
void Function(const char *key, Storage *storageUnit)
{
char keyValue[strlen(key) + 1];
strcpy(keyValue, key); // To fix the discard qualifiers bit
Element data = { keyValue }; // = new struct element;
printf("Key %s\n", (char *)data.data); // This works.
*(storageUnit->buckets) = &data;
// Let's see if it got stored correctly?
Element temp = *(Element *)(storageUnit->buckets);
// This is gobbledygook
printf("Stored correctly with data %s", (char *)(temp.data));
}
我可能会错过什么?
I'm trying to find the bug in my implementation here, where I store a struct in another struct and cannot seem to access the value stored. I define two structs.
typedef struct {
void * data;
} Element;
typedef struct {
void **buckets;
} Storage;
In a separate function, I set key
to be a pointer to a char
. And pass it in to be stored in data.
void Function(const char *key, Storage *storageUnit)
{
char keyValue[strlen(key) + 1];
strcpy(keyValue, key); // To fix the discard qualifiers bit
Element data = { keyValue }; // = new struct element;
printf("Key %s\n", (char *)data.data); // This works.
*(storageUnit->buckets) = &data;
// Let's see if it got stored correctly?
Element temp = *(Element *)(storageUnit->buckets);
// This is gobbledygook
printf("Stored correctly with data %s", (char *)(temp.data));
}
What could I be missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
为
并打印语句为
编辑
不确定这是否是一个好方法,但我很确定您可以更改
为
不更改打印语句并获得相同的行为。
change
to
and print statement from
to
EDIT
Not sure if this is a good way to do it, but I am pretty sure you can change
to
Without changing the print statement and get the same behavior.
我想也许你巩固了你的帖子?某一时刻它是 storageUnit->buckets,然后是 cm->buckets。这让我认为后面的打印实际上是在函数之外,在这种情况下它会爆炸,因为 Element data 是一个局部变量,一旦 Function() 返回,它就会消失。您已将自动装置添加到容器中。
我想你想要
元素数据 = new Element();
数据.数据=键值;
I think maybe you consolidated your posting a bit? at one point it's storageUnit->buckets, then it's cm->buckets. This makes me think the latter printing is actually outside the function, in which case it will blow up because Element data is a local variable that goes away once Function() returns. You've added an automatic to your container.
I think you want
Element data = new Element();
data.data = keyValue;
char keyValue[strlen(key)];
是错误的,因为您使用 strcpy,您必须考虑 nul 终止符。将其设为char keyValue[strlen(key) + 1];
。这可能是导致最后一个 printf 出现问题的原因。*(storageUnit->buckets) = &data;
也可能是错误的。data
和keyValue
都在堆栈上分配。当您的Function
返回时,这些对象不再有效,因此将该指针存储在 storageUnit->buckets[0] 中是没有用的。您可能应该使用例如 malloc() 动态分配元素和键。char keyValue[strlen(key)];
is wrong since you use strcpy, you have to account for the nul terminator. make itchar keyValue[strlen(key) + 1];
. This is likely what's causing your issue in the last printf.*(storageUnit->buckets) = &data;
might be wrong too.data
andkeyValue
are both allocated on the stack. When yourFunction
returns, these objects are no longer valid, so storing that pointer in storageUnit->buckets[0] is useless. You should probably allocate the element and the key dynamically with e.g. malloc().