Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void * arithmetic

Tags:

#include<stdio.h> int main(int argc,char *argv[]) {    int i=10;    void *k;    k=&i;     k++;    printf("%p\n%p\n",&i,k);    return 0; } 

Is ++ a legal operation on void* ? Some books say that it's not but K & R doesn't say anything regarding void * arithmetic ( pg. 93,103,120,199 of K &R 2/e)

Please clarify.

PS : GCC doesn't complain at least in k++.

like image 498
Onkar Mahajan Avatar asked Oct 13 '10 11:10

Onkar Mahajan


People also ask

Why can't I perform arithmetic on void * pointer?

Since void is an incomplete type, it is not an object type. Therefore it is not a valid operand to an addition operation. Therefore you cannot perform pointer arithmetic on a void pointer.

What does void * func () mean?

Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.

What is void * p in C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

Can you free a void * in C?

yes it is safe. Show activity on this post. In one case, malloc and free match, and in the other the allocated memory is returned. There are also secondary resources, and the constructor and destructor match.


2 Answers

It is a GCC extension.

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

If you add the -pedantic flag it will produce the warning:

warning: wrong type argument to increment

If you want to abide to the standard, cast the pointer to a char*:

k = 1 + (char*)k; 

The standard specifies one cannot perform addition (k+1) on void*, because:

  1. Pointer arithmetic is done by treating k as the pointer to the first element (#0) of an array of void (C99 §6.5.6/7), and k+1 will return element #1 in this "array" (§6.5.6/8).

  2. For this to make sense, we need to consider an array of void. The relevant info for void is (§6.2.5/19)

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

  3. However, the definition of array requires the element type cannot be incomplete (§6.2.5/20, footnote 36)

    Since object types do not include incomplete types, an array of incomplete type cannot be constructed.

Hence k+1 cannot be a valid expression.

like image 194
kennytm Avatar answered Oct 17 '22 23:10

kennytm


No, arithmetic on void* is not covered by the standard. Use char* for this.

like image 22
Jens Gustedt Avatar answered Oct 18 '22 01:10

Jens Gustedt