标签: void-pointers

C问题:单个取消引用void**双间接指针

我收到了这条消息:

expected 'void **' but argument is of type 'char **'
Run Code Online (Sandbox Code Playgroud)

当我尝试编译类似于此的东西:

void myfree( void **v )
{
    if( !v || !*v )
        return;

    free( *v );
    *v = NULL;

    return;
}
Run Code Online (Sandbox Code Playgroud)



我在堆栈溢出后阅读这个问题后发现了我认为的解决方案:
在处理双重间接时避免不兼容的指针警告 - Stack Overflow

所以我调整为这样的事情:

#include <stdio.h>
#include <stdlib.h>

void myfree( void *x )
{
    void **v = x;

    if( !v || !*v )
        return;

    free( *v );
    *v = NULL;

    return;
}

int main( int argc, char *argv[] )
{
    char *test;

    if( ( test = …
Run Code Online (Sandbox Code Playgroud)

c type-conversion void void-pointers compiler-warnings

6
推荐指数
1
解决办法
2004
查看次数

从'void*'到'unsigned char*'的转换无效

我有以下代码;

void* buffer = operator new(100);
unsigned char* etherhead = buffer;
Run Code Online (Sandbox Code Playgroud)

我在尝试编译时遇到以下错误;

error: invalid conversion from ‘void*’ to ‘unsigned char*’
Run Code Online (Sandbox Code Playgroud)

为什么我会得到那个错误,我认为一个空白是"无类型"所以它可以指向任何东西,或任何东西可以指向它?

c++ void-pointers unsigned-char

6
推荐指数
2
解决办法
2万
查看次数

Malloc和Void Pointers

我正在研究这个malloc函数,我可以使用一些帮助:

static void *malloc(int size)
  {
        void *p;

        if (size < 0)
                 error("Malloc error");
        if (!malloc_ptr)
                 malloc_ptr = free_mem_ptr;

        malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */

        p = (void *)malloc_ptr;
        malloc_ptr += size;

        if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
                 error("Out of memory");

        malloc_count++;
        return p;
 }
Run Code Online (Sandbox Code Playgroud)

我知道malloc func为任何类型分配内存空间,如果有足够的内存,但我不理解的行是:

p = (void *)malloc_ptr;
malloc_ptr += size;
Run Code Online (Sandbox Code Playgroud)

它如何指向任何类似的数据类型?我只是无法理解void指针或其位置.

注意:malloc_ptr是unsigned long

c malloc void-pointers

6
推荐指数
1
解决办法
2万
查看次数

类型转换整数为void*

#include <stdio.h>
void pass(void* );
int main()
{
    int x;
    x = 10;
    pass((void*)x);
    return 0;
}
void pass(void* x)
{
   int y = (int)x;
   printf("%d\n", y);
}


output: 10
Run Code Online (Sandbox Code Playgroud)

我的问题来自上面的代码..

  1. 当我们将正常变量强制转换为void*或任何指针变量时会发生什么?

  2. 我们必须将变量的地址传递给函数,因为在函数定义参数中是指针变量.但是这段代码传递了正常变量..

在linux pthread编程中遵循这种格式......我是一个入门级的C程序员.我在linux gcc编译器中编译这个程序..

c linux pointers pthreads void-pointers

6
推荐指数
1
解决办法
2万
查看次数

有没有办法阻止隐式指针转换为void*

我需要在源代码中找到所有这些地方,其中隐式转换任何类型的指针void *或停止这些隐式转换的方法.

例如:

  1. int *void *
  2. char *void *
  3. Base *void *

是否有任何gcc警告或错误标志,它会检测指针被隐式转换为的所有这些行void *

c c++ pointers void-pointers implicit-conversion

6
推荐指数
1
解决办法
545
查看次数

C - 表达式必须是可修改的左值

我很困惑为什么我的编译器在以下情况下抛出错误:

void funcExample (void * p_Buf, uint16_t len) 
{
    uint16_t i;

    for (i = 0; i < len; i++) {
        otherFunc (((uint8_t *)p_Buf)++); //error = expression must be a modifiable lvalue
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我在传递给otherFunc之前进行转换,那很好,因为增加非void指针没有问题:

void funcExample (void * p_Buf, uint16_t len) 
{
    uint16_t i;
    uint8_t * p_Buf_8bit;

    p_Buf_8bit = (uint8_t *) p_Buf;   

    for (i = 0; i < len; i++) {
        otherFunc (p_Buf_8bit++); 
    }
}
Run Code Online (Sandbox Code Playgroud)

一旦施放,无法将void指针递增?我错过了一些基本的东西吗?

c pointers void-pointers

6
推荐指数
1
解决办法
3310
查看次数

基于两个可能的结构的未知void指针的访问类型标志?

我目前在C中使用自己的八叉树.树将包含几十亿个对象,因此内存效率是关键.为了实现这一点,我目前使用一个带有标志和联合的结构,但我认为它不干净并且它浪费了内部节点的空间,因为我只需要一个8位标志但是为64位保留了内存指数.我的代码目前如下:

typedef struct _OctreeNode
{
    uint64_t location_code;
    union
    {
        uint8_t child_exists;
        uint64_t object_index;
    } data;
    uint8_t type;
} OctreeNode;
Run Code Online (Sandbox Code Playgroud)

我想把它分成两个不同的结构.一个叶节点和一个内节点.如下:

typedef struct _OctreeInnerNode
{
    uint64_t location_code;
    uint8_t child_exists;
    uint8_t type;
} OctreeInnerNode;

typedef struct _OctreeLeafNode
{
    uint64_t location_code;
    uint64_t object_index;
    uint8_t type;
} OctreeLeafNode;
Run Code Online (Sandbox Code Playgroud)

现在问题出现在我的无序地图上,基于位置代码的哈希值.它使用void指针,因此存储两个不同的结构不是问题.我知道有可能将标志作为第一个元素并取消引用指向flag数据类型的指针来派生类型,如下所示:

typedef struct _OctreeLeafNode
{
    uint8_t type;
    uint64_t location_code;
    uint64_t object_index;
} OctreeLeafNode;

void
func(void* node)
{
    uint8_t type = *(uint8_t*)node;
    if (type == LEAF_NODE) {
        OctreeLeafNode* leaf_node = (OctreeLeafNode*)node;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更清洁的方式.或者这不推荐?我怎么能处理结构和void指针的多种可能性?

提前致谢!

c void-pointers octree

6
推荐指数
1
解决办法
65
查看次数

从void**转换为char**有多危险

所以我们知道标准不会强制指针大小相等.(这里这里)(而不是谈论函数指针)

我想知道实际上这可能是一个问题.我们知道void *可以容纳任何东西,所以如果指针大小不同,那将是最大的尺寸.鉴于此,指定void **一个char **意味着麻烦.

我的问题是假设void *char *具有相同的大小是多么危险?实际上有一个架构,这不是真的吗?

此外,16位dos不是我想听到的!;)

c pointers void-pointers

5
推荐指数
1
解决办法
296
查看次数

reinterpret_cast std :: function*来往于void*

当我从主可执行文件中调用std :: function时,通过将其地址转换为/从中,我在插件中遇到段错误void*.我可以在几个自包含的行中重现这个问题:

#include <iostream>
#include <functional>

int main()
{
    using func_t = std::function<const std::string& ()>;

    auto hn_getter = func_t{[]() {
        return "Hello";
    }};

    auto ptr = reinterpret_cast<void*>(&hn_getter);

    auto getter = reinterpret_cast<func_t*>(ptr);
    std::cout << (*getter)() << std::endl;   // Bang!

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

即使我正在使用原始类型,它仍然是segfaulting.谁能看到我哪里出错了?

c++ void-pointers reinterpret-cast std-function

5
推荐指数
1
解决办法
663
查看次数

用非零值初始化void指针的正确(或最安全)方法?

以下作品:

void* p = reinterpret_cast<void*>(0x1000);
Run Code Online (Sandbox Code Playgroud)

但是看起来“不正确/不安全”,例如。0x1000是一个int,甚至不是uintptr_t,我可以解决这个问题,但是有没有更好/更安全的投射方法?

c++ void void-pointers c++17

5
推荐指数
1
解决办法
176
查看次数