void-pointers

Using python ctypes and libc to write void pointer to binary file

可紊 提交于 2019-11-30 18:01:01
问题 I am using python ctypes and libc to interface with a vendor-provided DLL file. The purpose of the DLL file is to acquire an image from a camera. The image acquisition appears to run without error; the issue I am having is accessing the data. The image acquisition function takes a ctypes.c_void_p as an argument for the image data. simplified as follows: """ typedef struct AvailableData { void* initial_readout; int64 readout_count; } imageData; """ class AvailableData(ctypes.Structure):

Using void pointer to an array

纵然是瞬间 提交于 2019-11-30 17:11:41
问题 I was just trying to use a void pointer to an integer array ,I tried to see if i can print the array back by casting it back into int. But it is giving me some random value. Can you tell me where i am going wrong? #include<stdio.h> #include<stdlib.h> int main(){ int a[5]; int x; int j; a[0]=1; a[1]=2; a[2]=3; a[3]=4; void *arr=a; for(j=0;j<4;j++){ x = *(int *)(arr+j); printf("%d",x); } return 0; } Output is this: 133554432131072512 Why is it not pinting elements of array a[] i.e 1,2,3,4 ? 回答1

Malloc and Void Pointers

杀马特。学长 韩版系。学妹 提交于 2019-11-30 14:42:00
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 understand are: p = (void *)malloc_ptr; malloc_ptr += size; How can it point to any data type like that

Genericity vs type-safety? Using void* in C

主宰稳场 提交于 2019-11-30 12:03:02
问题 Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get stuck into C, I'm aware that I have to make a compromise and I'd like it to be the right one. Either my data structures have a void * in each node / element and I lose type safety or I have to re-write my structures and code for each type I want to

How to cast blocks to and from void *

ε祈祈猫儿з 提交于 2019-11-30 11:35:25
So, I'm trying to pass a block as an NSAlert contextInfo parameter. [myAlert beginSheetModalForWindow: theWindow modalDelegate: myAlert didEndSelector: @selector(alertDidEnd:returnCode:contextInfo:) contextInfo: (void *) aBlock]; and get it back on the other end: void (^responseBlock)() = (__bridge_transfer void (^)()) contextInfo; Which works, to an extent. Before my call to beginSheetModalForWindow:... aBlock is at 0x00007fff610e1ec0 , and in the response ( alertDidEnd:... ), contextInfo is at 0x00007fff610e1ec0 . However, when I try to call the block: responseBlock(); I get the following

Union vs. static_cast(void*)

丶灬走出姿态 提交于 2019-11-30 09:31:25
问题 I'm writing code and until now I was using structures like this: struct s{ enum Types { zero = 0, one, two }; unsigned int type; void* data; } I needed some generic structure to store data from different classes and I wanted to use it in std::vector, so that's reason why I can't use templates. What's better option: unions or void pointers? Void pointer allocates only as much space as I need, but c++ is strong typed language for some reason and casting everywhere I need to use those data is

Is it undefined behaviour to delete a null void* pointer?

怎甘沉沦 提交于 2019-11-30 07:46:31
问题 I know that delete ing a null pointer is a no-op: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. (C++ Standard 5.3.5 [expr.delete] p2 ) And also that deleting a void* pointer is undefined behaviour because the destructor can't be called as there are no objects of type void : In the first alternative ( delete object ), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object

Confusion in ternary operator and typecasting

家住魔仙堡 提交于 2019-11-30 03:59:41
问题 I gone through this question - why the result of : 1 ? (int *)0 : (void *)0 differs to the result of : 1 ? (int *)0 : (void *)1 How it is differ ? It should be 0 or (int*)0 . How to check the result ? Where we can use such type of expression ? 回答1: The only difference is in types: the first one returns an int * , the second one returns a void * . From C11 standard, §6.5.15 Conditional operator , ¶6: If both the second and third operands are pointers or one is a null pointer constant and the

Genericity vs type-safety? Using void* in C

[亡魂溺海] 提交于 2019-11-30 01:57:33
Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get stuck into C, I'm aware that I have to make a compromise and I'd like it to be the right one. Either my data structures have a void * in each node / element and I lose type safety or I have to re-write my structures and code for each type I want to use them with. The complexity of the code is an obvious factor: iterating through an array or a linked

sizeof void pointer

浪子不回头ぞ 提交于 2019-11-30 00:13:32
why is sizeof void pointer 2 ? JaredPar The size of a void* is a platform dependent value. Typically it's value is 4 or 8 bytes for 32 and 64 bit platforms respectively. If you are getting 2 as the value then your likely running on a 16 bit coding platform (or potentially have a coding error). Could you post the code you are using and some more information about your environment / operating system? Per the online C standard (n1256 draft) : 6.2.5 Types ... 27 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 39) Similarly, pointers