Paolo
Paolo

Reputation: 183

How are "void*" pointer declaration and "void" keyword related during memory allocation?

Since a C pointer returns a "void*", and in addition to its own address it might refers somehow to the memory size reserved by malloc(size), i ask myself if declaration of "void*" is actually something completely unrelated to the type "void" used to execute procedures and if "void*" is actually a kind of type that self contain informations like the allocated memory block size, with an "internal" structure similar to the one showed below.

//An imaginary internal implementation of "type" void* used to create pointers

struct void*
{
    char[4] address; //for 32bit systems;
    int size; //memory block size;
};

void* ptr=malloc(10); //create a pointer called ptr of 10 bytes

Upvotes: 1

Views: 141

Answers (4)

Lundin
Lundin

Reputation: 214780

void* ptr=malloc(10); creates 10 bytes of raw, uninitialised data on the heap. That data does not at this point have a type.

It does not "create a pointer called ptr of 10 bytes". The variable ptr is of sizeof(void*) bytes and allocated as "automatic storage" (likely on the stack), regardless of where you let it point.

See A program uses different regions of memory for static objects, automatic objects, and dynamically allocated objects for an explanation of where different variables are likely allocated on mainstream computers.

Upvotes: 1

No, they're not completely unrelated. void is a type that (C11 6.2.5p19)

comprises an empty set of values; it is an incomplete object type that cannot be completed.

And void * is a pointer type that points to an object of the said type.

Any object pointer type can be converted to another object pointer type with an explicit cast, given that the pointer is suitably aligned; the only conversion between pointer types that can happen without a cast in C are conversions to void * and back again; hence the void * status as the generic pointer type.

The void type in itself can be used for other tasks - for example an expression can be cast to void to signal that the value is being deliberately ignored, or used as the return value type to signal that the function does not return a value. The only semantic overload is the use of void in a prototype: int func(void) to signal that the function does not take any arguments.

Upvotes: 1

Christophe
Christophe

Reputation: 73530

The use of the keyword void may indeed have a different semantic depending on the context:

  • It can mean nothing such as in void myfunction(...) { ... } or in int my_other_function(void) {...}
  • It can mean anything such as in void *p = ...;

In C a void pointer is a pointer that can point to anything. In C++ as well, but the usage should be avoided as much as possible and std::any should be preferred.

Important remark about your imaginary implementation: a void pointer does not istself know anything about the size of the object it points to.

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234855

void has more than one use; that's all. void and void* are very different beasts. Note that void* is not a keyword, but void is.

Don't think of a void* as being your structure. It's merely a pointer that's been converted from another type by a cast. malloc returns a void* that's convertible to another pointer type by a mechanism that somewhat paradoxically makes it impossible to write malloc in portable C.

Upvotes: 2

Related Questions