void-pointers

How to cast blocks to and from void *

青春壹個敷衍的年華 提交于 2019-11-29 17:16:33
问题 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

Union vs. static_cast(void*)

99封情书 提交于 2019-11-29 16:18:16
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 not the way c++ code should be designed. As I read, void pointers shouldn't be used unless there's no

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

女生的网名这么多〃 提交于 2019-11-29 14:59:17
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, sizeof(node)); } printf("Done reallocating\n"); printmem(nodes, i); // free(a); return 0; } This gives the

Regarding dereferencing 'void *' pointer

对着背影说爱祢 提交于 2019-11-29 14:55:09
I am not able to find out how to remove the above warning from the below line of code. data is is void pointer and as part of callback function will be receiving string in the data pointer. As I have typecast the void pointer but compiler still showing the warning. There are basically two warnings showing up on the below line. 1. dereferencing 'void *' pointer 2. taking address of expression of type 'void service_ind = atoi((const char*)&data[at_response.param[0].start_of_value_index]) ? TRUE:FALSE ; Below are required information void * data; AT_PARSER_RESPONSE at_response; typedef struct { /

access element of struct passed into a void* pointer

眉间皱痕 提交于 2019-11-29 11:55:54
I'm working with a binary search tree data structure to sort a series of structs with the type definitions: typedef struct { char c; int index; } data_t; typedef struct node node_t; typedef node { void *data; node_t *left; node_t *right; } The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function: static void *recursive_search_tree(node_t *root, void *key, int cmp(void*,void*)) Within the recursive_search_tree function, I want to be able to modify the code to use the index element as a

Usage of void pointers across different platforms

孤街浪徒 提交于 2019-11-29 09:22:53
I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems? int x=100; int *pi=&x; printf("value of pi is: %p",(void*)pi); Mohit Jain printf is a variadic function and must be passed arguments of the right types. The standard says %p takes void * . Implicit cast doesn't take place for variadic functions . Quoting from N1570 7.21.6.1 The fprintf function p : The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing

Is comparing two void pointers to different objects defined in C++?

牧云@^-^@ 提交于 2019-11-29 09:21:12
Inspired by this answer about dynamic cast to void* : ... bool eqdc(B* b1, B *b2) { return dynamic_cast<void*>(b1) == dynamic_cast<void*>(b2); } ... int main() { DD *dd = new DD(); D1 *d1 = dynamic_cast<D1*>(dd); D2 *d2 = dynamic_cast<D2*>(dd); ... eqdc(d1, d2) ... I am wondering if it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid , but different objects . More generally, but possibly not as relevant, is comparing ( == or != ) two values of type void* always defined, or is it required that they hold a

How to return different types from a single function

…衆ロ難τιáo~ 提交于 2019-11-29 08:59:42
I have the following c code : #include <stdio.h> #include <stdlib.h> void *func(int a) { if (a==3) { int a_int = 5; int *ptr_int = &a_int; return (void *)ptr_int; } else if (a==4) { char a_char = 'b'; char *ptr_char = &a_char; return (void *)ptr_char; } else { fprintf(stderr, "return value is NULL"); return NULL; } } int main (int argc, char *argv[]) { int *ptr_int = (int *)func(3); char *ptr_char = (char *)func(4); fprintf(stdout, "int value = %d\n", *ptr_int); fprintf(stdout, "char value = %c\n", *ptr_char); return 0; } But when I use gcc to test this code, I have the following results : int

Find out Type of C++ Void Pointer

五迷三道 提交于 2019-11-29 06:57:11
I have a small question: how do I find out what type a C++ pointer is? I often use a small function in my console programs to gather input, which looks something like this: void query(string what-to-ask, [insert datatype here] * input) I would like to create a generic form, using a void pointer, but I can't cin a void pointer, so how to I find out it's type so I can cast it? It's been a long time since the last time I coded in C++, but... Can't you use a Template ? You can't. However, one alternative is to do away with void pointers, make everything derive from a common base class and use RTTI

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

别说谁变了你拦得住时间么 提交于 2019-11-29 05:26:49
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 representing a base class of such an object. If not, the behavior is undefined. (C++ Standard 5.3.5 [expr.delete