void-pointers

C (gcc) warning: initialization from incompatible pointer type when calling pthread_cleanup_push()

让人想犯罪 __ 提交于 2019-12-11 05:29:12
问题 gcc version 4.3.3 under Ubuntu Linux 9.04 in case that is relevant. This is the offending code: pthread_cleanup_push(ctl_cleanup, NULL); with ctl_cleanup() defined as void* ctl_cleanup(void *arg); There are other instances where this warning pops up, in similar circumstances. The warning also appears if I call something like pthread_cleanup_push(pthread_mutex_unlock, (void *)&m); where m is of type pthread_mutex_t. The warning reads: warning: initialization from incompatible pointer type I

Boost.Python: Fill in a passed in buffer in Python

≡放荡痞女 提交于 2019-12-11 03:06:09
问题 I was wondering whether it's possible to fill in a buffer (with the following conditions) in Python and if so how? I have a buffer in C++ that I need to fill in Python. The Address of the buffer is obtained through the GetAddress method which returns a void pointer to the buffer's address. #include <boost/smart_ptr/shared_ptr.hpp> class Foo { public: Foo(const unsigned int length) { m_buffer = boost::shared_ptr< unsigned char >( new unsigned char[ length ] ); } ~Foo(){} void* GetAddress( )

Why do we have to cast a void pointer to int or something else before printing the value in the memory whose address is in the pointer?

风流意气都作罢 提交于 2019-12-10 16:26:30
问题 I wonder why is it necessary to cast a void pointer to an int * or char * before printing the contents of the address in memory, even though we tell the printf() function how to interpret the data in memory? Let's say that we have the following code: int main (void) { void* c = malloc(4); printf("%d",*c); return 0; } Why it is not possible to do this? So to be clear my question is what is the reason for this being not possible? EDIT: After all the answers and research I am still not convinced

How can deleting a void pointer do anything other than invoke the global delete operator?

馋奶兔 提交于 2019-12-10 15:19:32
问题 The C++ standard very clearly and explicitly states that using delete or delete[] on a void -pointer is undefined behavior, as quoted in this answer: This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void . However, as I understand it, delete and delete[] do just two things: Call the appropriate destructor(s) Invoke the appropriate operator delete function, typically the global one There is a single-argument operator delete (as

reinterpret_cast std::function* to and from void*

半腔热情 提交于 2019-12-10 14:16:40
问题 I'm suffering a segfault in a plugin when I call a std::function in it passed from the main executable, via converting it's address to/from void* . I can reproduce the problem in a few self-contained lines: #include <iostream> #include <functional> int main() { using func_t = std::function<const std::string& ()>; auto hn_getter = func_t{[]() { return "Hello"; }}; auto ptr = reinterpret_cast<void*>(&hn_getter); auto getter = reinterpret_cast<func_t*>(ptr); std::cout << (*getter)() << std::endl

Does C have a generic “pointer to a pointer” type?

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:16:43
问题 For example, if I wanted to write a "free" that nulled the pointer, I could write something like: void myfree(void **data) { free(*data); *data = NULL; } however, when I try to write this, I get a compiler warning (from gcc 4.6.2) saying: warning: passing argument 1 of ‘myfree’ from incompatible pointer type [enabled by default] ... note: expected ‘void **’ but argument is of type ‘char **‘ (in this case, I am freeing a char array). It seems that void* is special cased to avoid this kind of

Using and dereferencing (void**)

非 Y 不嫁゛ 提交于 2019-12-10 03:26:56
问题 I would like to pass a "polymorphic" array of pointers to a function. I can do the following without warnings: foo (void* ptr); bar() { int* x; ... foo(x); } gcc apparently automatically casts x to a (void*) , which is just dandy. However, I get a warning when I do the following: foo (void** ptr); bar() { int** x; // an array of pointers to int arrays ... foo(x); } note: expected ‘void **’ but argument is of type ‘int **’ warning: passing argument 1 of ‘foo’ from incompatible pointer type

Avoiding void pointers

穿精又带淫゛_ 提交于 2019-12-09 19:20:44
问题 I am implementing my own programming language in C++11. One of the datatypes I've designed is the Token class. It is meant to store a token read from the source file, with the content of the token, it's type, and the line at which it was encountered. A token can be either a single-character symbol, a lenghtful string, a number, or a name. So it needs to be able to store different data types, either a character for symbols, a double for numbers, and a std::string for names and strings. The way

Why is `typedef void * COMPLEX` used in this interface?

烈酒焚心 提交于 2019-12-09 13:58:26
问题 I have a program and I can't understant how it works. Here is a part of it. I don't understand the line typedef void *COMPLEX , the command this and why the struct COMPLEX_IMPL is being used. #ifndef _COMPLEX_H #define _COMPLEX_H typedef void *COMPLEX; COMPLEX NewCOMPLEX (double a, double b ); void DeleteCOMPLEX(COMPLEX this ); double GetA (COMPLEX this ); double GetB (COMPLEX this ); COMPLEX AddComplex (COMPLEX c1, COMPLEX c2, COMPLEX res); COMPLEX MultComplex (COMPLEX c1, COMPLEX c2,

When is uintptr_t preferred over intptr_t?

落花浮王杯 提交于 2019-12-09 04:39:11
问题 Given the requirement that I need to store the value of a "generic" pointer in a struct and have no interest in the pointed-at memory itself, I find it more semantically correct to store it as an intptr_t than a void* . The question is whether a uintptr_t is better suited or not, and when one is preferred over the other in general? 回答1: It is mostly a stylistic argument (an optimizing compiler would probably generate the same, or very similar, code). However, pointer compares may be a tricky