你好,我正在C中进行一项任务,我需要将一个未知类型的参数传递给一个函数.
例如,假设我有以下内容:
int changeCount(void* element)
{
element.Count = element.Count++;
return 1;
}
Run Code Online (Sandbox Code Playgroud)
变量元素无效的原因是因为有三种可能性.但是,所有3都有一个名为"Count"的成员变量.
当我尝试编译我在Eclipese中编写的实际代码时,出现以下错误:
错误:请求成员'计数'不是结构或联合
我猜这种情况正在发生,因为编译器事先并不知道"元素"的类型.但是,我不明白为什么这不起作用.
感谢帮助!
我的程序需要使用void*来在动态调用情况下传输数据或对象,以便它可以引用任意类型的数据,甚至是原始类型.但是,我最近发现,在具有多个基类的类的情况下向下转换这些void*的过程失败,甚至在调用这些向下转换指针上的方法后崩溃我的程序,即使内存地址似乎是正确的.在访问"vtable"期间发生崩溃.
所以我创建了一个小测试用例,环境是Mac OS X上的gcc 4.2:
class Shape {
public:
virtual int w() = 0;
virtual int h() = 0;
};
class Square : public Shape {
public:
int l;
int w() {return l;}
int h() {return l;}
};
class Decorated {
public:
int padding;
int w() {return 2*padding;}
int h() {return 2*padding;}
};
class DecoratedSquare : public Square, public Decorated {
public:
int w() {return Square::w() + Decorated::w();}
int h() {return Square::h() + Decorated::h();}
};
#include <iostream>
template <class T> …Run Code Online (Sandbox Code Playgroud) 我有一个通用链表实现,其中包含一个包含void*to data的节点结构和一个包含对head的引用的list结构.现在这是我的问题,链表中的节点可以通过其void*保存对另一个链表的引用.当我释放包含较小列表的较大列表时,这会导致内存泄漏.所以我想知道有没有办法检查void*是否指向另一个列表所以我跟随并释放它也只是数据.
如果我在我的struct的开头添加一个关键字,我可以通过解除引用void*来检查并找出它是一个列表?
编辑:调用者不插入我的函数插入的较小列表我不希望调用者处理重定位多个列表只是他们持有指针的列表.
我正在使用哈希表,我遇到了这个函数.但是hash/sizeof(void*)是什么意思?和之后给出的评论 - 摆脱已知的0位?
// This matches when the hashtable key is a pointer.
template<class HashKey> class hash_munger<HashKey*> {
public:
static size_t MungedHash(size_t hash) {
// TODO(csilvers): consider rotating instead:
// static const int shift = (sizeof(void *) == 4) ? 2 : 3;
// return (hash << (sizeof(hash) * 8) - shift)) | (hash >> shift);
// This matters if we ever change sparse/dense_hash_* to compare
// hashes before comparing actual values. It's speedy on x86.
return hash / sizeof(void*); …Run Code Online (Sandbox Code Playgroud) C99标准的第7.18.1.4节规定:
以下类型指定无符号整数类型,其属性是任何有效指针
void都可以转换为此类型,然后转换回指针void,结果将与原始指针进行比较:
uintptr_t
这是否意味着只有void *类型可以转换为uintptr_t和返回而不改变原始指针的值?
特别是,我想知道是否需要使用以下代码uintptr_t:
int foo = 42;
void * bar = &foo;
uintptr_t baz = bar;
void * qux = baz;
int quux = *(int *)qux; /* quux == foo == 42 */
Run Code Online (Sandbox Code Playgroud)
或者,如果C99标准保证这个更简单的版本产生相同的效果:
int foo = 42;
uintptr_t bar = &foo;
int baz = *(int *)bar; /* baz == foo == 42 */
Run Code Online (Sandbox Code Playgroud)
void *在转换指针之前是否需要转换uintptr_t,反之亦然?
我正在查看https://en.cppreference.com/w/cpp/language/reinterpret_cast,我注意到它指定了我们可以始终转换为的合法类型:
byte*char*unsigned char*但我并没有看到void*在列表中。这是疏忽吗?我的用例需要一个,reinterpret_cast因为我是从int**转换为void*。我最终会从void*后面投射到镜头上int**。
嗨!
我使用了以下C宏,但在C++中它无法自动转换void*为type*.
#define MALLOC_SAFE(var, size) { \
var = malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
我知道,我可以这样做:
#define MALLOC_SAFE_CPP(var, type, size) { \
var = (type)malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
但我不想重写大部分代码,MALLOC_SAFE使用的地方.
有没有办法在没有给宏的类型的情况下这样做?也许一些MSVC 2005 #pragma/__declspec/其他?
ps:我不能使用C编译器,因为我的代码是大项目的一部分(数百个模块之一).现在它是在C++上.我知道,我可以单独构建我的代码.但这是旧代码,我只是想快速移植它.
问题是关于void*cast;)如果不可能,我只需用MACRO_SAFE_CPP替换MACRO_SAFE
谢谢!
我经历了这个问题 -
为什么结果:1 ? (int *)0 : (void *)0
与以下结果不同:
1 ? (int *)0 : (void *)1
它有何不同?它应该是0或(int*)0.
如何查看结果?
我们可以在哪里使用这种表达方式?
我确实尝试在SO上搜索这个,但我认为由于语法而不知道究竟要搜索什么,我变得有点不稳定.
我已经看到(void*)用于强制转换,通常用于函数调用.这是用来做什么的?
我们知道它void*没有关于它指向的数据的实际类型的信息。但是,从 cppreference 开始new,new[]我们知道这些运算符返回void*. 那么,如何给出:
auto x = new int{};
Run Code Online (Sandbox Code Playgroud)
知道new操作符应该返回void*,x被推导为一种类型int*,不是void*吗?
再考虑一个例子:
struct foo {
static void* operator new(std::size_t n) {
std::cout << "new called!\n";
return ::new char[n];
}
};
Run Code Online (Sandbox Code Playgroud)
让我们添加一些牛逼YPE d isplayer:
template <typename T>
struct TD;
Run Code Online (Sandbox Code Playgroud)
和测试代码:
int main() {
auto x = new foo{};
TD<decltype(x)>{};
}
Run Code Online (Sandbox Code Playgroud)
代码无法编译,错误指示decltype(x)是foo*。如果我们注释掉 的最后一行main,我们就会知道我们的void*-returning 操作符会因为new …
void-pointers ×10
c++ ×6
c ×5
casting ×4
pointers ×2
c99 ×1
gcc ×1
hash ×1
linked-list ×1
sizeof ×1
visual-c++ ×1