简体   繁体   English

将int与void结合*

[英]Combining an int with a void*

I need to combine an int with a void*. 我需要将int与void *结合使用。 I was thinking of doing it like : 我正在考虑这样做:

long int lint = ((int)integer<<(sizeof(void*)*8)) | ((void*)ptr);

but according to my previous message , a long int won't suffice. 但是根据我之前的消息 ,长整型是不够的。

Can anyone suggest how to do this? 谁能建议该怎么做?

PS. PS。 Some people will ask why on earth I might want to do this. 有人会问为什么我可能要这样做。 Well, I am developing a generic events system (dispatch/listen) that caters for countless different cases. 好吧,我正在开发一个通用事件系统(调度/侦听),该系统可以满足无数不同的情况。 I will post code when it's ready... 准备就绪后,我将发布代码...

The only answer is to use struct 唯一的答案是使用struct

Don't try to be smarter than compiler. 不要试图比编译器更聪明。 Don't perform premature optimization. 不要执行过早的优化。 Write simplest and clearest code and only after you see it too slow, perform low-level optimization 编写最简单,最清晰的代码,只有在看到它太慢之后,才执行低级优化

Struct are better here because: 结构在这里更好,因为:

  1. It is portable 轻便
  2. It safe 很安全
  3. It is c++ ideomatic. 这是C ++意识形态的。
  4. It is faster . 它更快

Let me bust your myths about comparing speed of long long and struct. 让我破坏您关于比较long long和struct的速度的神话。 As you know, all ways to optimize your code starts with profiling. 如您所知,优化代码的所有方法都从分析开始。 Lets make simple program and measure comparing speed of vectors of long long and struct S : 让我们编写简单的程序并比较long longstruct S的向量的速度:

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

struct S
{
    unsigned int a;
    void* b;

    bool operator==(const S& other)  const
    {
        return a == other.a && b == other.b;
    }
};

template <typename Iterator>
int count_eq(Iterator begin, Iterator end)
{
    int result = 0;
    for (Iterator i  = begin; i != end; ++i) {
        for (Iterator j  = i + 1; j != end; ++j) {
            result += *i == *j;
        }
    }
    return result;
}

template <typename Iterator>
void mesure(Iterator begin, Iterator end)
{
    long long t0 = GetTickCount();
    int res = count_eq(begin, end);
    long long t1 = GetTickCount();
    std::cout << "result: " << res <<"; Time: "<<(t1-t0)<<"\n";
}

int main()
{
    const unsigned int Size = 20000;
    std::vector<unsigned long long> l;
    for (int i = 0; i < Size; i++) {
        l.push_back(i% (Size/10));
    }

    std::vector<S> s;
    for (int j = 0; j < Size; j++) {
        S el;
        el.a = j% (Size/10);
        el.b = 0;
        s.push_back(el);
    }

    mesure(l.begin(), l.end());
    mesure(s.begin(), s.end()); 
}

Lets check results: 让我们检查结果:

>g++ src.cpp -O3
>a
result: 90000; Time: 327
result: 90000; Time: 188

Yes, struct with custom operator == 1.5 times faster. 是的,使用自定义operator == struct快1.5倍。

Hmm shouldn't it be something like: 嗯不应该是这样的:

long long res = ((long long)integer<<(sizeof(void*))) | ((long long)ptr);

Thit will still only work for 32 bit pointers and will not work for 64 bit. Thit仍仅适用于32位指针,不适用于64位。 There is no built-in type that will fit such arithmetics. 没有适合此类算术的内置类型。

you should use something like uint_least64_t , available from stdint.h , of course it cannot hold pointers from 64 bits systems, for that you would need to use a struct as many people have mentioned in the comments. 您应该使用类似stdint.h uint_least64_t这样的东西,当然它不能保存来自64位系统的指针,因为您将需要使用注释中提到的许多人所使用的结构。

Though the way you are using the pointers seems weird, it looks like you don't even need to use void* for what you are doing, nevermind the fact that you could to the same thing with polymorphism (for the event system you described). 尽管您使用指针的方式看起来很怪异,但看起来您甚至不需要在执行操作时使用void* ,也不要忘记可以通过多态对同一事物进行处理(对于您所描述的事件系统) 。

If you want a data structure that can store either an int or a void pointer, use a union . 如果您想要一个可以存储int或void指针的数据结构,请使用union A union will be the size of the largest value it contains. 联合将是其包含的最大值的大小。 You can do something like this: 您可以执行以下操作:

typedef union {
    void *data,
    int *value
} myUnion_t;

myUnion_t storage;
int num = 10;

// if you want to store a void pointer
storage.data = &num;

// if you want to store an int
storage.value = num;

Keep in mind a union is not a struct and should only store one value at a time. 请记住,联合不是一个结构,并且一次只能存储一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM