如何在 C 中存储指向已分配内存区域的偏移位置的指针?

发布于 2024-12-17 14:31:16 字数 1596 浏览 1 评论 0原文

我正在尝试用我得到的库制作一个非常简单的哈希表。我尝试创建的行包含 C 字符串或 int 作为键。值可以有 4 种不同类型。如果它是结构,我尝试将指针存储在分配的内存区域中。然而,在我的世界中,这并不遵循与非指针字段相同的逻辑,我得到:

error C2106: '=' : left operand must be l-value when I try to store pointer as value:

这是我知道这非常基本的代码

static void MakeRow(UtiHashtable_t *Hashtable, void *Key, void *Value, void *Row)
{
    int     KeySize, ValueSize;

    KeySize = sizeof(char) * Hashtable->KeyStringLength;
    ValueSize = GetValueSize(Hashtable);
    Row = malloc(KeySize + ValueSize);

    if (Hashtable->KeyType ==  MY_HASHTABLE_TYPE_STRING)  
        MyStrcpy((char *)Row, (char *)Key, Hashtable->KeyStringLength); 
    else if (Hashtable->KeyType ==  MY_HASHTABLE_TYPE_INT)
        ((int *)Row)[0] =  *(int *)Key;
    else
        MyAssert(0);

    switch (Hashtable->ValueType)
    {
    case MY_HASHTABLE_TYPE_STRING:
        MyStrcpy((char *)Row + KeySize, (char *)Value, Hashtable->ValueStringLength); 
        break;
    case MY_HASHTABLE_TYPE_INT: 
        *(int *)((char *)Row + KeySize) =  *(int *)Value;
        break;
    case MY_HASHTABLE_TYPE_DOUBLE: 
        *(double *)((char *)Row + KeySize) =  *(double *)Value;
        break;
    case MY_HASHTABLE_TYPE_STRUCT: 
        (int *)((char *)Row + KeySize) = (int *)Value;
        break;
    default:
        MyAssert(0);
    }
}

,并且会得到很少的反对票,但除了答案之外,我想解释为什么这会带走 *不让它成为指针。

编译器允许:

(int *)Value = (int *)Key;

那为什么不呢:

(int *)((char *)Row + KeySize) = (int *)Value;

谢谢& BR-马蒂

I'm trying to make a very simple Hashtable with libraries I got. The row that I try to make contains either C-string or int as key. Value can be of 4 different types. If it's struct I try to store a pointer in malloced memory area. However in my world this does not follow the same logic as non-pointer fields and I get:

error C2106: '=' : left operand must be l-value when I try to store pointer as value:

Here's the code

static void MakeRow(UtiHashtable_t *Hashtable, void *Key, void *Value, void *Row)
{
    int     KeySize, ValueSize;

    KeySize = sizeof(char) * Hashtable->KeyStringLength;
    ValueSize = GetValueSize(Hashtable);
    Row = malloc(KeySize + ValueSize);

    if (Hashtable->KeyType ==  MY_HASHTABLE_TYPE_STRING)  
        MyStrcpy((char *)Row, (char *)Key, Hashtable->KeyStringLength); 
    else if (Hashtable->KeyType ==  MY_HASHTABLE_TYPE_INT)
        ((int *)Row)[0] =  *(int *)Key;
    else
        MyAssert(0);

    switch (Hashtable->ValueType)
    {
    case MY_HASHTABLE_TYPE_STRING:
        MyStrcpy((char *)Row + KeySize, (char *)Value, Hashtable->ValueStringLength); 
        break;
    case MY_HASHTABLE_TYPE_INT: 
        *(int *)((char *)Row + KeySize) =  *(int *)Value;
        break;
    case MY_HASHTABLE_TYPE_DOUBLE: 
        *(double *)((char *)Row + KeySize) =  *(double *)Value;
        break;
    case MY_HASHTABLE_TYPE_STRUCT: 
        (int *)((char *)Row + KeySize) = (int *)Value;
        break;
    default:
        MyAssert(0);
    }
}

I know this is really basic and will get few down votes but besides the answer I would like to have explanation why this taking * away does not make it pointer.

The compiler allows:

(int *)Value = (int *)Key;

so why not:

(int *)((char *)Row + KeySize) = (int *)Value;

Thanks & BR -Matti

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

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

发布评论

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

评论(2

沫尐诺 2024-12-24 14:31:16

不推荐对左值使用强制转换。

此外,由于对齐问题,某些系统不允许将 int 或指针放置在任意内存位置。

您应该做的是将指针值 memcpy 到您想要的位置。
当您想使用指针时,您需要将其memcpy返回到指针变量。

Using casts on an lvalue is deprecated.

Also, some systems don't allow ints or pointers to be placed in arbitrary memory locations due to alignment issues.

What you should do is to memcpy the pointer value to where you want it.
And when you want to use the pointer you need to memcpy it back to a pointer variable.

娇纵 2024-12-24 14:31:16

您省略了左侧的解引用 *,因此该赋值不再有意义。

You left off the dereferencing * on the left side, so the assignment no longer makes any sense.

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