void-pointers

Porting C code to C++, problem with casting void* from malloc to desired pointer

元气小坏坏 提交于 2020-06-25 21:12:55
问题 I am currently porting some C code I wrote to C++ for fun. I am struggling with a malloc() call I make in C, with h and w being constants for simplicity reasons, but later exchanged with runtime constants: double (*g2)[h][w] = malloc(h * w * sizeof(double)); In C, this is an implicit conversion of a void* , and this of course doesn't fly with C++. I already tried casting this with reinterpret_cast<double[h][w]> , but this is still an invalid cast. I was wondering, how can I make this work in

Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?

拈花ヽ惹草 提交于 2020-05-16 04:10:10
问题 I have the following code (live on Coliru): // untouchable extern library .hpp file typedef union ExternLibraryUnion { int a; float b; }ExternLibraryUnion; // my code #include <iostream> class Container{ public: Container() : m_union(NULL) {}; ~Container(){ if(m_union){ delete m_union; } } void init(){ m_union = new ExternLibraryUnion(); } ExternLibraryUnion* get_union(){ return m_union; } private: ExternLibraryUnion* m_union; }; class Master{ public: Master() : m_union(NULL) { m_container

Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?

心不动则不痛 提交于 2020-05-16 04:07:09
问题 I have the following code (live on Coliru): // untouchable extern library .hpp file typedef union ExternLibraryUnion { int a; float b; }ExternLibraryUnion; // my code #include <iostream> class Container{ public: Container() : m_union(NULL) {}; ~Container(){ if(m_union){ delete m_union; } } void init(){ m_union = new ExternLibraryUnion(); } ExternLibraryUnion* get_union(){ return m_union; } private: ExternLibraryUnion* m_union; }; class Master{ public: Master() : m_union(NULL) { m_container

Storing arbitrary objects in array in C

孤者浪人 提交于 2020-04-18 05:45:24
问题 I am new to C but am trying to wrap my head around trying to store arbitrary objects in an array. Structs, integers, chars, functions, etc. Basically something perhaps using void pointers along the lines of (pseudocode): void *array[] = malloc(10000); struct MyStruct m = malloc(sizeof(m)); int x = 10; char c[] = "Look Here"; array[0] = &m; array[1] = &x; array[2] = &c; Essentially I want to have a global array store arbitrary objects sort of like a database, and then fetch them by index

Trying to create a multi threaded C program that prints a string in reverse order

我是研究僧i 提交于 2020-03-26 04:04:29
问题 I am working on an assignment for my C coding class that requires us to create multiple threads that run different functions. As to ease my confusion, I am trying to do the program one thread at a time, but I am having some trouble. Here is my code: #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> void * print_string_in_reverse_order(void *str) { // This function is called when the new thread is created printf("%s","In funciton start_routine().

Trying to create a multi threaded C program that prints a string in reverse order

笑着哭i 提交于 2020-03-26 04:02:54
问题 I am working on an assignment for my C coding class that requires us to create multiple threads that run different functions. As to ease my confusion, I am trying to do the program one thread at a time, but I am having some trouble. Here is my code: #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> void * print_string_in_reverse_order(void *str) { // This function is called when the new thread is created printf("%s","In funciton start_routine().

Methods to convert `void *` function parmeter inconsistant from type to type

人走茶凉 提交于 2020-02-22 19:21:55
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of

Methods to convert `void *` function parmeter inconsistant from type to type

末鹿安然 提交于 2020-02-22 19:20:11
问题 Note: This question attempts to improve what I attempted to ask here, but fell short. Also, I have seen this, and this. They discuss similar concepts, but do not answer these questions. My environment is Windows 10, and for testing I used two compilers, CLANG and GCC. I am passing variables via a void * function argument, and need to convert them. I would like to get some feedback on inconsistencies I am seeing between methods for different types. The following is a stripped-down depiction of

Why do I Have to reinterpret_cast Pointer Pointers?

不羁的心 提交于 2020-01-24 10:00:06
问题 So this static_cast code is totally legal: int n = 13; void* pn = static_cast<void*>(&n); void** ppn = &pn; Yet this has to be made into a reinterpret_cast to compile: int n = 13; int* foo = &n; void** bar = static_cast<void**>(&foo); If I don't change it I get the error: error C2440: static_cast : cannot convert from int ** to void ** note: Types pointed to are unrelated; conversion requires reinterpret_cast , C-style cast or function-style cast So I take it the issue is "the types are

Why operator void*() conversion function added to the C++ stream classes?

此生再无相见时 提交于 2020-01-22 17:30:29
问题 There is a conversion function operator void*() const in C++ stream classes. so that all stream objects can be implicitly converted to void* . During the interaction with programmers on SO they suggest me to don't use void* unless you've a good reason to use it. void* is a technique of removing type safety & error checking. So, due to the existance of this conversion function following program is perfectly valid. This is a flaw in the C++ standard library. #include <iostream> int main() {