void-pointers

Is casting from TYPE* to unsigned char* allowed?

久未见 提交于 2019-12-08 18:24:04
问题 C99 -- specifically section 6.2.6.1, paragraph 4 -- states that copying an object representation into an array of unsigned char is allowed: struct { int foo; double bar; } baz; unsigned char bytes[sizeof baz]; // Do things with the baz structure. memcpy(bytes, &baz, sizeof bytes); // Do things with the bytes array. My question: can we not avoid the extra memory allocation and copy operation by simply casting? For example: struct { int foo; double bar; } baz; unsigned char *bytes = (void *)

How to push and pop a void pointer in C

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:59:32
问题 I have this working code: #import <stdlib.h> #import <stdio.h> typedef struct myarray { int len; void* items[]; } MYARRAY; MYARRAY *collection; void mypop(void** val) { puts(collection->items[collection->len]); *val = collection->items[collection->len--]; } void mypush(void* val) { int len = collection->len++; collection->items[len] = val; puts(collection->items[len]); } int main() { puts("Start"); collection = malloc( sizeof *collection + (sizeof collection->items[0] * 1000) ); collection-

deleting a void pointer using delete operator [duplicate]

白昼怎懂夜的黑 提交于 2019-12-08 15:33:41
问题 This question already has answers here : Is it safe to delete a void pointer? (13 answers) freeing a void pointer (3 answers) Closed 6 years ago . whats wrong with the code. What it should be. As it is throwing an error. Operator 'delete', applied to void* argument. int i; void *ptr = &i; delete ptr; 回答1: whats wrong with the code. Everything except int i; The second line attempts to convert an integer to a pointer. In special circumstances, you could force that past the compiler with

casting from void** to int

守給你的承諾、 提交于 2019-12-08 15:18:48
问题 I have a dynamic 2D array stored in a void** pointer, and I am just wondering how I am supposed to cast/dereference the values so that they can be printed? Here is an example of what I am trying to do: /* Assume that I have a data structure called graph with some * element "void** graph" in it and some element "int order" */ void foo(graph_t *graph) { int **matrix; /*safe malloc works fine, it uses calloc to initialise it all to zeroes*/ matrix = safe_malloc(graph->order * sizeof(int*)); for

Bubble Sort using pointers to function

戏子无情 提交于 2019-12-08 14:03:21
问题 I'm trying to implement a bubble sort in c by using pointers to function but does not work. Can anyone help me? Here is the code: #include <stdio.h> #include <stdlib.h> void bubbleSort(void** base, size_t length, int (*compar)(const void*, const void*)); int main(int argc, char* argv[]) { int cmp(const void*, const void*); int vet[] = {1, 2, 5, 7, 6, 1, 3, 2, 9, 15, 14, 20}; bubbleSort((void**) &vet, sizeof(vet)/sizeof(vet[0]), cmp); int i; for (i = 0; i < sizeof(vet)/sizeof(vet[0]); i++) {

How to get an item from a void pointer to array when I know each element's size?

≡放荡痞女 提交于 2019-12-07 20:33:26
问题 It comes when I want to write my own quicksort for educational purpose. This is what I got: qsort(void* array, int count, int size, int(*compare)(const void*, const void*)); And I have size of each element in array, and pointer to the first element in array. How can I get each individual element in that array? 回答1: If size was generated with the sizeof operator, it is a multiple of sizeof(char) (which is 1 by definition). So cast the void* into a char* , and move size "characters" at a time.

Why is void* considered unsafe in C++? [duplicate]

天涯浪子 提交于 2019-12-07 16:13:46
问题 This question already has answers here : What is type safety and what are the “type safe” alternatives? [duplicate] (10 answers) Closed last year . I'm reading Bjarne Stroustrup C++ FAQ site. Where I saw following line. avoid void* (keep them inside low-level functions and data structures if you really need them and present type safe interfaces, usually templates, to your users) Why is void* considered unsafe in C++? 回答1: Why is void* considered unsafe in C++? Because void* represents a

When to use a void pointer over a char pointer?

早过忘川 提交于 2019-12-07 10:03:34
问题 In which situation should we prefer a void pointer over a char pointer or vice-versa? As a matter of fact both can be type cast to any of the data types. 回答1: A void pointer is a pointer to "any type", and it needs to be converted to a pointer to an actual type before it may be dereferenced. A pointer to char is a pointer to char , that happens to have the property that you could also access (parts of) other types through it. You should use void * when the meaning is intended to be "any type"

Dynamic Array of Void Pointers

ぐ巨炮叔叔 提交于 2019-12-07 09:39:47
问题 I'm trying to create a dynamic set abstract data type, based on a dynamic array. However, I get a compiler warning and an error when I try to add the data to the array, which are: warning: dereferencing 'void *' pointer [enabled by default] error: invalid use of void expression My code is as follows, I've marked the problematic line with a comment struct SET { //general dynamic array void *data; int elements; //number of elements int allocated; // size of array }; struct SET create() { /

Is void* necessary apart from memory allocation related stuff

随声附和 提交于 2019-12-07 05:48:35
问题 Is void* necessary apart from memory allocation related stuff in C++? Can you give me an example? 回答1: Logging memory addresses If you want to output a pointer using iostreams (e.g. for logging) then going via void* is the only way of ensuring operator<< hasn't been overloaded in some crazy way. #include <iostream> struct foo { }; std::ostream& operator<<(std::ostream& out, foo*) { return out<<"it's a trap!"; } int main() { foo bar; foo *ptr = &bar; std::cout << ptr << std::endl; std::cout <<