void-pointers

void* to Object^ in C++/CLI

时光怂恿深爱的人放手 提交于 2020-01-13 13:49:52
问题 I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#. Some of the native C++ functions have a return type of void*. I am not sure how to handle this when I pass back the value to my calling code. For instance: if a C# app calls my dll wrapper, what do I return from the native call: void* start(ThreadFunc,void *, unsigned *); I am currently attempting to box the return in a generic System::Object^ with no luck.

print bits of a void pointer

寵の児 提交于 2020-01-11 11:19:31
问题 If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated? For example: void * p; p = malloc(24); printf("0x%x\n", (int *)p); I would like the above to print the 24 bits that I just allocated. 回答1: size_t size = 24; void *p = malloc(size); for (int i = 0; i < size; i++) { printf("%02x", ((unsigned char *) p) [i]); } Of course it invokes undefined behavior (the value of an object allocated by malloc has an

print bits of a void pointer

﹥>﹥吖頭↗ 提交于 2020-01-11 11:18:15
问题 If I create a void pointer, and malloc a section of memory to that void pointer, how can I print out the individual bits that I just allocated? For example: void * p; p = malloc(24); printf("0x%x\n", (int *)p); I would like the above to print the 24 bits that I just allocated. 回答1: size_t size = 24; void *p = malloc(size); for (int i = 0; i < size; i++) { printf("%02x", ((unsigned char *) p) [i]); } Of course it invokes undefined behavior (the value of an object allocated by malloc has an

Do I need to initiallize(set to 0) memory after calling realloc?

六月ゝ 毕业季﹏ 提交于 2020-01-10 05:17:08
问题 I need to implement a simple dynamic array of pointers to a typedef'ed pointer. Using realloc each time it's requested by the user, the array size will grow by sizeof(pointer). So what I have is this: #include <stdio.h> #include <string.h> #include <stdlib.h> typedef void* node; void printmem(node* a, int c) { int i; for (i = 0; i < c; ++i) { printf("%d\t\t%p\n", i, a[i]); } } int main(void) { node* nodes = NULL; int i = 0, count = 20; for (i = 0; i < count; ++i) { nodes = realloc(nodes,

Generic List in C using void pointer

一个人想着一个人 提交于 2020-01-05 08:24:13
问题 I'm trying to create a generic list that will allow any type to be entered. However, I am having problems with the comparism in the is_element_of function (since I am making use of void pointers). Any help? typedef struct Item{ void* data; } Item; typedef struct Node{ Item Item; struct Node* next; struct Node* previous; } Node; typedef Node* List; bool is_element_of(Item Item, List *pointertolist) { bool isinlist = false; Node *scan = *pointertolist; while (scan->next != NULL) { if ((scan-

Is it safe to cast a pointer to void* then compare to NULL

社会主义新天地 提交于 2020-01-05 04:59:14
问题 i am working on a homework and since our constraints are really strict i need to check for NULL pointers everywhere if i want 100%. So i made a little inlined function which checks pointers for NULL : static inline void exit_on_null(void* ptr, const char* msg) { if ( ! ptr ) { printf("%s\n", msg); exit(1); } } Now i asked myself is it safe to do so? From the standard i know it is save to cast a pointer to void* and back and receive the original pointer. Does that give that the void* version

Container of Different Functors

馋奶兔 提交于 2020-01-03 17:23:32
问题 I am trying to figure out a way to have a container of functors so that I can pass a value into the functors and have it be modified however I am having trouble allowing the functors to not be restricted in what types they can be passed and the amount of arguments they can take. My actual use for this is that I have a series of functors which all change a 3D vector in some way based on the input. By being able to store these functors in a container I can manipulate the order in which they are

warning: pointer of type ‘void *’ used in arithmetic

你说的曾经没有我的故事 提交于 2020-01-02 00:53:15
问题 I am writing and reading registers from a memory map, like this: //READ return *((volatile uint32_t *) ( map + offset )); //WRITE *((volatile uint32_t *) ( map + offset )) = value; However the compiler gives me warnings like this: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith] How can I change my code to remove the warnings? I am using C++ and Linux. 回答1: Since void* is a pointer to an unknown type you can't do pointer arithmetic on it, as the compiler wouldn't know

(void*) casting- what is this used for?

余生长醉 提交于 2020-01-01 03:20:07
问题 I did try searching for this on SO but I think due to the syntax and not knowing exactly what to search I became a little unstuck. I have seen (void*) being used to cast, usually to function calls. What is this used for? 回答1: void* , usually referred to as a void pointer , is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful

Malloc and Void Pointers

℡╲_俬逩灬. 提交于 2019-12-30 04:45:10
问题 I'm studying this malloc function and I could use some help: static void *malloc(int size) { void *p; if (size < 0) error("Malloc error"); if (!malloc_ptr) malloc_ptr = free_mem_ptr; malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */ p = (void *)malloc_ptr; malloc_ptr += size; if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr) error("Out of memory"); malloc_count++; return p; } I know that the malloc func allocates memory space for any type, if there is enough memory, but the lines i don't