我收到了这条消息:
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) 我有以下代码;
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)
为什么我会得到那个错误,我认为一个空白是"无类型"所以它可以指向任何东西,或任何东西可以指向它?
我正在研究这个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
#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)
我的问题来自上面的代码..
当我们将正常变量强制转换为void*或任何指针变量时会发生什么?
我们必须将变量的地址传递给函数,因为在函数定义参数中是指针变量.但是这段代码传递了正常变量..
在linux pthread编程中遵循这种格式......我是一个入门级的C程序员.我在linux gcc编译器中编译这个程序..
我需要在源代码中找到所有这些地方,其中隐式转换任何类型的指针void *或停止这些隐式转换的方法.
例如:
int * 至 void *char * 至 void *Base * 至 void *是否有任何gcc警告或错误标志,它会检测指针被隐式转换为的所有这些行void *?
我很困惑为什么我的编译器在以下情况下抛出错误:
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中使用自己的八叉树.树将包含几十亿个对象,因此内存效率是关键.为了实现这一点,我目前使用一个带有标志和联合的结构,但我认为它不干净并且它浪费了内部节点的空间,因为我只需要一个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指针的多种可能性?
提前致谢!
当我从主可执行文件中调用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.谁能看到我哪里出错了?
以下作品:
void* p = reinterpret_cast<void*>(0x1000);
Run Code Online (Sandbox Code Playgroud)
但是看起来“不正确/不安全”,例如。0x1000是一个int,甚至不是uintptr_t,我可以解决这个问题,但是有没有更好/更安全的投射方法?