开发者

C++ | void *data[1000]

Is it appropriate way to keeps the pointers in statically allocated array this way in C++?

void *data[1000];

Is the size of void* various on 32 and 64 bit mac开发者_开发知识库hines?


It's almost certainly absolutely not appropriate. Void pointers are pretty damn bad to begin with, and mix them with mutable static data, and you have something quite awful. A primitive unwrapped array of a magic number size on top of that, and I'm very glad that I don't have that in my code. Of course, if you have to, then you have to, but in the incredible majority of new code, you won't need anything like this.

Yes, the size of void* varies.


A far better choice than void* is to make every object derive from a base class and use base* instead.

A far better choice than a fixed size array is to use std::vector<base*> or a Boost pointer container.

Yes, the size of a pointer will be different between 32 and 64 bit systems - that's the whole point.


Yes, size of void* will vary depending on system bitness.

It's impossible to say whether using a fixed size array and storing void* pointers in it is appropriate without more details - it's usually not a good choice, but sometimes you really have no other options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜