void-pointers

Is ((void *) -1) a valid address?

早过忘川 提交于 2019-12-17 05:04:35
问题 Verbatim from Linux' man shmat: RETURN VALUE [...] on error (void *) -1 is returned, and errno is set to indicate the cause of the error. (POSIX tells the same using a slightly different wording.) Is there any mandatory rule or definition (standard?) that (void *) -1 may not be a valid address? 回答1: To answer the question directly, no, there is no mandatory rule, definition, standard, or specification that says (void *) -1 may not be a valid address. (Of course, no rules, definitions,

void pointers: difference between C and C++

此生再无相见时 提交于 2019-12-17 05:03:41
问题 I'm trying to understand the differences between C and C++ with regards to void pointers. the following compiles in C but not C++ (all compilations done with gcc/g++ -ansi -pedantic -Wall): int* p = malloc(sizeof(int)); Because malloc returns void* , which C++ doesn't allow to assign to int* while C does allow that. However, here: void foo(void* vptr) { } int main() { int* p = (int*) malloc(sizeof(int)); foo(p); return 0; } Both C++ and C compile it with no complains. Why? K&R2 say: Any

C- Iterating over an array of structs passed through a void*

送分小仙女□ 提交于 2019-12-13 08:17:07
问题 I have a function struct Analysis reduce (int n, void* results) Where n is the number of files to be analyzed, and I'm passing an array of Analysis structs to results. The Analysis struct is defined as follows: struct Analysis { int ascii[128]; //frequency of ascii characters in the file int lineLength; //longest line in the file int lineNum; //line number of longest line char* filename; } I've cast the void * as such, struct Analysis resArray[n]; struct Analysis* ptr = results; resArray[0] =

L-value trouble when using a (void *) as a generic data container

梦想与她 提交于 2019-12-13 03:38:57
问题 Here is a structure used in a program: struct basic_block { void * aux; /* Many other fields, which are irrelevant. */ }; Now: Several instances of basic_block exist during the execution of the program. The program works in stages/passes, which are executed one after another. The aux field is meant for storing stage and basic_block specific data during the execution of a stage, and freed by the stage itself (so the next stage can reuse it). This is why it is a void * . My stage uses aux to

Pointer address span on various platforms

心不动则不痛 提交于 2019-12-13 02:45:21
问题 A common situation while coding in C is to be writing functions which return pointers. In case some error occurred within the written function during runtime, NULL may be returned to indicate an error. NULL is just the special memory address 0x0, which is never used for anything but to indicate the occurrence of a special condition. My question is, are there any other special memory addresses which never will be used for userland application data? The reason I want to know this is because it

SQLite in C/C++. sqlite3_exec: parameter set in callback function is pointing to an empty string

狂风中的少年 提交于 2019-12-12 23:50:31
问题 I'm trying to something really basic - returning the result of the SELECT statement using C/C++ interface for SQLite. My Data table has only two fields - key (varchar) and value (text). Given the key, my goal is to return the value by querying the SQLite database. I pass to *sqlite3_exec* - *select_callback* function as well as param (char *). The param is successfully set to the correct value within *select_callback*. However, after calling *sqlite3_exec* param points to an empty string

Wrapping a void * argument in Python SWIG

老子叫甜甜 提交于 2019-12-12 20:42:59
问题 I am wrapping this lib with Python SWIG that has a function looking like this: int set_option(Foo *foo, const char *name, void *value); In the lib const char *name is mapped to a type that I have access to look up: int , char * , char ** . The wrapper code generated by default accepts only a wrapped void * (naturally). What is the best way to make the wrapped method accept any Python object as argument, and do the type checking, and Python to C conversion in my own C code? My guess would be

Conversion between function pointers with void and non-void pointer parameters

自闭症网瘾萝莉.ら 提交于 2019-12-12 18:15:24
问题 I have a question related to void pointer conversions. Instead of casting between void* and non-void pointers, my question is about casting between function pointer types, one of which has void* as parameter type and another has a pointer to some particular data type. Here's the code which allows to reproduce the warning messages: #include <stdio.h> typedef void (*module_outputMessage)(void *param, const char *msg); void module_function(module_outputMessage outputFunc, void *output_param, int

Handle object in c++

谁说胖子不能爱 提交于 2019-12-12 12:32:50
问题 I've been told that a handle is a sort of "void" pointer. But what exactly does "void pointer" mean and what is its purpose. Also, what does "somehandle = GetStdHandle(STD_INPUT_HANDLE); do? 回答1: A handle in the general sense is an opaque value that uniquely identifies an object. In this context, "opaque" means that the entity distributing the handle (e.g. the window manager) knows how handles map to objects but the entities which use the handle (e.g. your code) do not. This is done so that

access violation casting to void* and back

痞子三分冷 提交于 2019-12-12 05:39:43
问题 I get an access violation reading location when I try the following. What am I doing wrong? uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(hInt); uint64_t hIntBack = *static_cast<uint64_t*>(hPoint); //get access violation here 回答1: I am guessing you meant to store the address of hInt in hPoint , not the value of hInt . uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(&hInt); // ^ addressof operator 回答2: What you do is the following: cast an integer