分配和访问结构体中字符串的指针
我试图将字符串存储在结构中包含的数组中,并访问它,但我遇到了困难。该结构如下所示:
typedef struct {
void **storage;
int numStorage;
} Box;
Box 初始化如下:
b->numStorage = 1000000; // Or set more intelligently
Box *b = malloc(sizeof(Box));
// Create an array of pointers
b->storage = calloc(b->numStorage,sizeof(void *));
为了设置字符串,我使用此函数:
void SetString(Box *b, int offset, const char * key)
{
// This may seem redundant but is necessary
// I know I could do strcpy, but made the following alternate
// this isn't the issue
char * keyValue = malloc(strlen(key) + 1);
memcpy(keyValue, key, strlen(key) + 1);
// Assign keyValue to the offset pointer
b->storage[offset*sizeof(void *)] = &keyValue;
// Check if it works
char ** ptr = b->storage[offset*sizeof(void *)];
// It does
printf("Hashcode %d, data contained %s\n", offset, *ptr);
}
问题在于,当我尝试使用完全相同的偏移量再次检索它时:
// Return pointer to string
void *GetString(const Box *b, int offset, const char *key)
char ** ptr = b->storage[offset*sizeof(void *)];
if (ptr != NULL) {
printf("Data should be %s\n", *ptr);
return *ptr;
} else {
return NULL;
}
返回的指针是乱码。可能出什么问题了?
I'm trying to store a string in an array contained within a struct, and access it, but I'm having a hard time. The struct looks like this:
typedef struct {
void **storage;
int numStorage;
} Box;
Box is initialized as such:
b->numStorage = 1000000; // Or set more intelligently
Box *b = malloc(sizeof(Box));
// Create an array of pointers
b->storage = calloc(b->numStorage,sizeof(void *));
In order to set the string, I use this function:
void SetString(Box *b, int offset, const char * key)
{
// This may seem redundant but is necessary
// I know I could do strcpy, but made the following alternate
// this isn't the issue
char * keyValue = malloc(strlen(key) + 1);
memcpy(keyValue, key, strlen(key) + 1);
// Assign keyValue to the offset pointer
b->storage[offset*sizeof(void *)] = &keyValue;
// Check if it works
char ** ptr = b->storage[offset*sizeof(void *)];
// It does
printf("Hashcode %d, data contained %s\n", offset, *ptr);
}
The problem lies when I try to retrieve it again, with the exact same offset:
// Return pointer to string
void *GetString(const Box *b, int offset, const char *key)
char ** ptr = b->storage[offset*sizeof(void *)];
if (ptr != NULL) {
printf("Data should be %s\n", *ptr);
return *ptr;
} else {
return NULL;
}
The returned pointer is gibberish. What could be amiss?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
访问数组时不必指定实际的内存偏移量。只需给它索引,您就会得到正确的元素。
那么,在你的第三个代码块中:
在你的第四个代码块中:
另外,在第二个代码块中,是否已设置
b->numStorage
?You don't have to specify the actual memory offset when accessing arrays. Simply give it the index and you will get the correct element.
So, in your third code block:
And in your fourth:
Also, in the second block of code, has
b->numStorage
already been set?这将局部变量
keyValue
的地址存储在数组中。一旦函数完成,该地址就变得无效。我想你想要:然后在检索时进行相应的更改。
This stores the address of the local variable
keyValue
in the array. Once the function completes, this address becomes invalid. I think you want:and then make the corresponding change while retrieving.
这不就是:
设置storage[offset*sizeof(void*)]指向局部变量keyValue的地址吗?即函数返回后不再有效
Doesn't this:
set storage[offset*sizeof(void*)] to point to the address of the local variable keyValue? i.e. no longer valid after function returns