void-pointers

Can I do arithmetic on void * pointers in C?

自闭症网瘾萝莉.ら 提交于 2019-11-27 03:53:20
问题 is this valid void *p = &X; /* some thing */ p += 12; and if so what does p now point to? I have (third party) code that does this (and compiles cleanly) and my guess is that the void * was treated as a char *. My trusty K&R is silent(ish) on the topic EDIT: My little test app runs fine on gcc 4.1.1 and treats void * as char *. But g++ barfs I know how to do it properly. I need to know if I have to clean this code base to find all the places its done. BTW gcc -pedantic throws up a warning

C: Extrapolating type from void pointer

时光怂恿深爱的人放手 提交于 2019-11-27 03:18:43
问题 Say a function takes a void pointer as an argument, like so: int func(void *p); How can we determine or guess the type of what p is pointing to? 回答1: In general, you can't. In some cases, if there is a guarantee on what p points to, you may be able to look at the contents at and after that address to find out. Generally, your function should know what it's being passed, unless it just passes it along. Data and function arguments in C are just a bunch of bits lumped together. Unless you use

Type punning with void * without breaking the strict aliasing rule in C99

风流意气都作罢 提交于 2019-11-27 00:56:21
问题 I recently came across the strict aliasing rule, but I'm having trouble understanding how to use void * to perform type punning without breaking the rule. I know this breaks the rule: int x = 0xDEADBEEF; short *y = (short *)&x; *y = 42; int z = x; And I know that I can safely use a union in C99 for type-punning: union{ int x; short y; } data; data.x = 0xDEADBEEF; data.y = 42; int z = data.x; But how do I use void * to safely perform type-punning in C99? Is the following correct: int x =

What is a void pointer and what is a null pointer?

不羁岁月 提交于 2019-11-27 00:26:39
So I was going through some interview questions and I came across one about void and null pointers , which claims: a pointer with no return type is called a null pointer. It may be any kind of datatype. This confused me thoroughly! It seems void and null could be used interchangeably according to this question, and I don't believe that to be correct. I assumed void to be a return type and null to be a value. But I am just a code-rookie and am not sure I am right. Please express your views as to what a null pointer is and a void pointer is. I am not looking for difference between null and void.

casting via void* instead of using reinterpret_cast

浪尽此生 提交于 2019-11-27 00:20:21
I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast : T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv); Instead of: T1 * p1=... T2 * p2= reinterpret_cast<T2*>(p1); However, I can't find an explanation why is this better than the direct cast. I would very appreciate if someone can give me an explanation or point me to the answer. Thanks in advance p.s. I know what is reinterpret_cast used for, but I never saw that is used in this way For types for which such cast is permitted (e.g. if T1 is a POD

C++. Error: void is not a pointer-to-object type

元气小坏坏 提交于 2019-11-26 23:13:58
问题 I have a C++ program: struct arguments { int a, b, c; arguments(): a(3), b(6), c(9) {} }; class test_class{ public: void *member_func(void *args){ arguments vars = (arguments *) (*args); //error: void is not a //pointer-to-object type std::cout << "\n" << vars.a << "\t" << vars.b << "\t" << vars.c << "\n"; } }; On compile it throws an error: error: ‘void*’ is not a pointer-to-object type Can someone explain what I am doing wrong to produce this error? 回答1: You are dereferencing the void *

Using intptr_t instead of void*?

前提是你 提交于 2019-11-26 22:33:08
Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void* ? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html ) For what I've already read: int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do pointer arithmetics on both void* and intptr_t require casts, so none gets advantage here void* means less explicit casts when storing pointers, intptr_t means less casts when storing integer values intptr_t requires C99 What

void pointer as argument [duplicate]

Deadly 提交于 2019-11-26 22:05:27
问题 This question already has an answer here: Dynamic memory access only works inside function 1 answer The following C snippet: [...] void f1(void* a){ printf("f(a) address = %p \n",a); a = (void*)(int*)malloc(sizeof(int)); printf("a address = %p \n",a); *(int*)a = 3; printf("data = %d\n",*(int*)a); } void f(void){ void* a1=NULL; printf("a1 address = %p \n",a1); f1(a1); printf("a1 address = %p \n",a1); printf("Data.a1 = %d\n",*(int*)a1); } [...] results in a1 address = (nil) f(a) address = (nil)

What does “void *(*)(void *)” mean in C++?

╄→гoц情女王★ 提交于 2019-11-26 21:53:07
问题 It's the parameter in pthread_create() . I think each part means: void * : The return value is a void pointer. (*) : It's a pointer to a function. (void *) : It takes an untyped pointer as a parameter. Is that correct? 回答1: Yes , it is the signature of a nameless function pointer that takes and returns void * . If it had a name (as in a variable) it would be: void *(*myFuncName)(void*) 来源: https://stackoverflow.com/questions/9371171/what-does-void-void-mean-in-c

error: cast from 'void*' to 'int' loses precision

微笑、不失礼 提交于 2019-11-26 21:38:51
I have a function with prototype void* myFcn(void* arg) which is used as the starting point for a pthread. I need to convert the argument to an int for later use: int x = (int)arg; The compiler (GCC version 4.2.4) returns the error: file.cpp:233: error: cast from 'void*' to 'int' loses precision What is the proper way to cast this? You can cast it to an intptr_t type. It's an int type guaranteed to be big enough to contain a pointer. Use #include <cstdint> to define it. Again, all of the answers above missed the point badly. The OP wanted to convert a pointer value to a int value, instead,