void-pointers

Concept of void pointer in C programming

爷,独闯天下 提交于 2019-11-26 05:57:00
问题 Is it possible to dereference a void pointer without type-casting in the C programming language? Also, is there any way of generalizing a function which can receive a pointer and store it in a void pointer and by using that void pointer, can we make a generalized function? for e.g.: void abc(void *a, int b) { if(b==1) printf(\"%d\",*(int*)a); // If integer pointer is received else if(b==2) printf(\"%c\",*(char*)a); // If character pointer is received else if(b==3) printf(\"%f\",*(float*)a); /

Is it safe to delete a void pointer?

廉价感情. 提交于 2019-11-26 03:19:52
问题 Suppose I have the following code: void* my_alloc (size_t size) { return new char [size]; } void my_free (void* ptr) { delete [] ptr; } Is this safe? Or must ptr be cast to char* prior to deletion? 回答1: It depends on "safe." It will usually work because information is stored along with the pointer about the allocation itself, so the deallocator can return it to the right place. In this sense it is "safe" as long as your allocator uses internal boundary tags. (Many do.) However, as mentioned

Objective-C: difference between id and void *

此生再无相见时 提交于 2019-11-26 01:48:19
问题 What is the difference between id and void * ? 回答1: void * means "a reference to some random chunk o' memory with untyped/unknown contents" id means "a reference to some random Objective-C object of unknown class" There are additional semantic differences: Under GC Only or GC Supported modes, the compiler will emit write barriers for references of type id , but not for type void * . When declaring structures, this can be a critical difference. Declaring iVars like void *

Objective-C: difference between id and void *

烂漫一生 提交于 2019-11-25 23:30:24
What is the difference between id and void * ? bbum void * means "a reference to some random chunk o' memory with untyped/unknown contents" id means "a reference to some random Objective-C object of unknown class" There are additional semantic differences: Under GC Only or GC Supported modes, the compiler will emit write barriers for references of type id , but not for type void * . When declaring structures, this can be a critical difference. Declaring iVars like void *_superPrivateDoNotTouch; will cause premature reaping of objects if _superPrivateDoNotTouch is actually an object. Don't do

Pointer arithmetic for void pointer in C

独自空忆成欢 提交于 2019-11-25 22:02:11
问题 When a pointer to a particular type (say int , char , float , ..) is incremented, its value is increased by the size of that data type. If a void pointer which points to data of size x is incremented, how does it get to point x bytes ahead? How does the compiler know to add x to value of the pointer? 回答1: Final conclusion: arithmetic on a void* is illegal in both C and C++. GCC allows it as an extension, see Arithmetic on void- and Function-Pointers (note that this section is part of the "C

casting void** to 2D array of int - C

大城市里の小女人 提交于 2019-11-25 21:59:05
问题 i am trying to cast a void** pointer to an int** 2D array in C here is the code that i am trying to work with (with all the extraneous bits removed): \\*assume that i have a data structure called graph with some *element \"void** graph\" in it and some element \"int order\" */ void initialise_graph_data(graph_t *graph) { void **graph_data = NULL; int (*matrix)[graph->order]; size_t size = (graph->order * graph->order) * sizeof(int); graph_data = safe_malloc(size); /*safe malloc works fine*/