-1

I am always tempted to refer to void * as dynamic typing for C & C++, usually as a throwaway joke.

I could not find a wikipedia entry or a dictionary entry for Dynamic Type. Perhaps the term is non-sensical?


Questions:

  1. Is void * a dynamic type?
  2. Is QVariant a dynamic type?

Thanks.

prapin
  • 6,395
  • 5
  • 26
  • 44
Anon
  • 2,267
  • 3
  • 34
  • 51
  • 1
    Related: [Representing dynamic typing in C](https://stackoverflow.com/questions/1485505/representing-dynamic-typing-in-c) – Weather Vane Jan 23 '23 at 11:27
  • 3
    C++ does not have dynamic types. The type of everything is fixed at compile time. `void *` can be used for a technique called _"type erasure"_ where a portion of the code has no knowledge of the type of the object (it obviously can't make use of the object as it doesn't know what type it is). – Richard Critten Jan 23 '23 at 11:30
  • 2
    As for "Is void* a dynamic type?", that question doesn't make any sense if you can't answer "what is a dynamic type"? As far as I know, no such thing exists in C or C++. Probably why no wikipedia page exists either. There is dynamic type _checking_ however, is that what you are asking about? – Lundin Jan 23 '23 at 11:38
  • 1
    re `QVariant` - _"...The QVariant class acts like a union for the most common Qt data types...."_ - it's `union` wrapper for a few fixed Qt types. So not dynamic at all. The types `QVariant` can hold are fixed: (a) by design of Qt and (b) at compile time. See also [`std::variant`](https://en.cppreference.com/w/cpp/utility/variant). ps the QVariant link in the question is broken. – Richard Critten Jan 23 '23 at 11:57
  • @RichardCritten For it to be dynamic, does it have to be able to become literally any type, or is it allowed to be constrained to a select few? – Anon Jan 23 '23 at 12:04
  • @Anon you should define _"dynamic"_ first so we can answer. I have assumed my definition is correct (with regard to C++) and answered accordingly. If you think there is a better/different definition __for you__ I think you should state what it is so we can answer the correct question. – Richard Critten Jan 23 '23 at 12:07
  • @RichardCritten I am only concerned regarding a formal definition, of which I have yet to find any. I guess I should not be speculating in lieu of that, but if you have a citation, I would gladly accept an answer containing one. – Anon Jan 23 '23 at 12:40

1 Answers1

0

Note this answer assumes dynamic type conversion as definition

void* is not a dynamic type, it is a pointer without a type, that doesn't make it a dynamic type because it's type does not automagically adapt to the situations as in Javascript.

For example such code wouldn't work at all:

void* test = malloc(20);
test[0] = 'E'; /* dereferencing void pointer */
puts(test);

Void pointers has to be casted by the programmer and in a sense that what would "dynamic" correspond to is not dynamic.

Some of C Compilers may cast void into other types automatically at compile time but that wouldn't make it dynamic.

Technically speaking similar thing can be done with char* too and you can convert it into a int and that wouldn't make it dynamic.

char* e = malloc(sizeof(int));
memset(e, 0, sizeof(int));
int coolint = *(int*)(e);
printf("%d", coolint);

It's better to think void* like an any type, anything stored in your computer's memory is represented by bytes and by themselves they do not correspond to any type they are just numbers make up the data.

Main usage area of void* in C is to emulate polymorphism.