void-pointers

Why is `boost::any` better than `void*`?

∥☆過路亽.° 提交于 2019-12-02 20:51:12
What inherent advantages do boost::any and boost::any_cast offer over using void* and dynamic_cast ? The advantage is that boost::any is way more type-safe than void* . E.g. int i = 5; void* p = &i; static_cast<double*>(p); //Compiler doesn't complain. Undefined Behavior. boost::any a; a = i; boost::any_cast<double>(a); //throws, which is good As to your comment, you cannot dynamic_cast from a void* . You can dynamic_cast only from pointers and references to class types which have at least one virtual function (aka polymorphic types) boost::any calls destructors: { boost::any x = std::string(

In C++, I'm getting a message “error: 'void*' is not a pointer-to-object type”

余生长醉 提交于 2019-12-02 20:17:19
问题 Here's my code: #include <iostream> using namespace std; int main() { void *x; int arr[10]; x = arr; *x = 23; //This is where I get the error } As you can see, the code is very simple. It just creates a void pointer x which points to the memory address of the array 'arr' and puts the integer 23 into that memory address. But when I compile it, I get the error message "'void*' is not a pointer-to-object type". When I use an 'int' pointer instead of a void pointer and then compile it, I don't

Why is it impossible to have a reference-to-void?

青春壹個敷衍的年華 提交于 2019-12-02 17:02:15
Why is it impossible to have a reference to void? The only thing I found in the C++ Standard is this line, at 8.3.2.1 A declarator that specifies the type "reference to cv void" is ill-formed. Why is it that way? Why can't I write a "generic" function that accept a void& ? Just to be clear, I have no useful application in mind where using a reference-to-void could be better than using templates, but I'm just curious about the rationale for forbidding this construct. To clarify a little, I understand that using a reference-to-void "as is" would be as meaningless as dereferencing a pointer-to

freeing a void pointer

蓝咒 提交于 2019-12-02 16:50:07
问题 How to free a void pointer. struct vStruct { void *vPtr; struct vStruct *next; }; struct vStruct sObj; struct vStruct *sObjNew = sObj; delete sObjNew->vPtr; -----------> Is this correct way to delete void pointer delete sObjNew; Showing error Operator 'delete', applied to void* argument having has undefined behavior, and most likely does not invoke the object's destructor. 回答1: You should not delete a void pointer. delete works for specific types (such that compiler knows, which destructor

freeing a void pointer

倾然丶 夕夏残阳落幕 提交于 2019-12-02 09:44:21
How to free a void pointer. struct vStruct { void *vPtr; struct vStruct *next; }; struct vStruct sObj; struct vStruct *sObjNew = sObj; delete sObjNew->vPtr; -----------> Is this correct way to delete void pointer delete sObjNew; Showing error Operator 'delete', applied to void* argument having has undefined behavior, and most likely does not invoke the object's destructor. You should not delete a void pointer. delete works for specific types (such that compiler knows, which destructor should be called - as stated in error message). If you want to hold unspecified type in your structure, you

In C++, I'm getting a message “error: 'void*' is not a pointer-to-object type”

天涯浪子 提交于 2019-12-02 08:13:42
Here's my code: #include <iostream> using namespace std; int main() { void *x; int arr[10]; x = arr; *x = 23; //This is where I get the error } As you can see, the code is very simple. It just creates a void pointer x which points to the memory address of the array 'arr' and puts the integer 23 into that memory address. But when I compile it, I get the error message "'void*' is not a pointer-to-object type". When I use an 'int' pointer instead of a void pointer and then compile it, I don't get any errors or warnings. I wanna know why I get this error. Thank you. As the compiler message says,

How to use void* as a single variable holder? (Ex. void* raw=SomeClass() )

…衆ロ難τιáo~ 提交于 2019-12-02 07:57:17
I am trying to make void* to hold a value (to avoid default constructor calling). I want to:- copy K to void* e.g. K k1; --> void* raw=k1; copy void* to K e.g. void* raw; --> K k2=raw; try not to break destructor and causes memory leak don't use any dynamic allocation (heap, performance reason) Here is what I tried:- class K{ public: std::string yes="yes" ; }; int main() { //objective: k1->raw->k2 , then delete "raw" void* raw[sizeof(K)]; //<--- try to avoid heap allocation K k1; static_cast<K>( raw)=k1; //<--- compile fail K k2= static_cast<K>( raw); std::cout<<k2.yes; //test static_cast<K&>

print bits of a void pointer

旧城冷巷雨未停 提交于 2019-12-02 06:57:47
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. 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 indeterminate value). You can't - reading those bytes before initializing their contents leads to undefined

Bubble sort universal implementation in C

那年仲夏 提交于 2019-12-01 21:41:59
I'm trying to make an universal bubble sort function. It allow to user to write its own compare and swap function. I implemented a swap and compare function for int type, but when I run the code for next array: {3, 5, 8, 9, 1, 2, 4, 7, 6, 0} , I get: 0 0 84214528 2312 1 2 4 7 6 0. Why this is happening? #include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 int compInt(void *a, void *b) // FUNCTION FOR COMPARE INT { if (*(int*)(a) > *(int*)(b)) { return false; } // IF FIRST INT > SECOND INT (WRONG ORDER) RETURN FALSE return true; // RIGHT ORDER -> RETURN TRUE } I think the

Return nothing or return void— What exactly does C# do at the end of a void Function() call?

懵懂的女人 提交于 2019-12-01 21:32:15
Consider the following C# function: void DoWork() { ... } The C# documentation states: When used as the return type for a method, void specifies that the method does not return a value . That seems straight-forward enough and, in most cases, that suits as a fair definition. However, for many lower-level languages (i.e. C) "void" has a slightly different meaning. Specifically, all blocks of code must return something . Therefore, void is a representation of the null pointer which is a representation of "nothing". In such a case, you do not need to have a return statement within your code