void-pointers

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

会有一股神秘感。 提交于 2019-11-26 20:37:25
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? 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, standards, or specifications about memory addresses are mandatory. I see people walking down the street every day

What does “typedef void (*Something)()” mean

核能气质少年 提交于 2019-11-26 18:03:27
问题 I am trying to understand what this means, the code I am looking at has in .h typedef void (*MCB)(); static MCB m_process; in .C MCB Modes::m_process = NULL; And sometimes when I do m_process(); I get segmentations fault, it's probably because the memory was freed, how can I debug when it gets freed? I hope my questions are clear. 回答1: It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly

What is a void pointer in C++? [duplicate]

删除回忆录丶 提交于 2019-11-26 17:57:14
问题 Possible Duplicate: What is a void pointer and what is a null pointer? I often see code which resembles something like the following: void * foo(int bar); What does this mean? Does it mean that it can return anything ? Is this similar to dynamic or object in C#? 回答1: A void* does not mean anything. It is a pointer, but the type that it points to is not known. It's not that it can return "anything". A function that returns a void* generally is doing one of the following: It is dealing in

C isn't that hard: void ( *( *f[] ) () ) ()

北城余情 提交于 2019-11-26 14:58:05
问题 I just saw a picture today and think I'd appreciate explanations. So here is the picture: I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture: So this "reading spirally" is something valid? Is this how C compilers parse? It'd be great if there are simpler explanations for this weird code. Apart from all, can these kind of codes be useful? If so, where and when? There is a question

Concept of void pointer in C programming

孤街浪徒 提交于 2019-11-26 14:50:48
Is it possible to dereference a void pointer without type-casting in the C programming language? Also, is there any way of generalizing a function which can receive a pointer and store it in a void pointer and by using that void pointer, can we make a generalized function? for e.g.: void abc(void *a, int b) { if(b==1) printf("%d",*(int*)a); // If integer pointer is received else if(b==2) printf("%c",*(char*)a); // If character pointer is received else if(b==3) printf("%f",*(float*)a); // If float pointer is received } I want to make this function generic without using if-else statements - is

multiple inheritance: unexpected result after cast from void * to 2nd base class

久未见 提交于 2019-11-26 14:33:58
问题 My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting these void* in case of classes with multiple base classes fails and even crashes my program after invoking methods on these down casted pointers even if the memory addresses seem to be correct. The crash happens during access to "vtable". So I have

How to cast an integer to void pointer?

流过昼夜 提交于 2019-11-26 12:04:12
问题 While working with Threads in C, I\'m facing the warning \"warning: cast to pointer from integer of different size\" The code is as follows #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> void *print(void *id) { int a=10; printf(\"My thread id is %ld\\n\",pthread_self()); printf(\"Thread %d is executing\\n\",id); return (void *) 42; } int main() { pthread_t th[5]; int t; int i; int status; void *ret; for(i=0;i<5;i++) { status=pthread_create(&th[i],NULL,print,

dynamic_cast from “void *”

女生的网名这么多〃 提交于 2019-11-26 11:28:32
问题 According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please clarify the issue. 回答1: dynamic_cast works only on polymorphic types, i.e. classes containing virtual functions. In gcc you can dynamic_cast to void* but not from : struct S { virtual ~S() {} }; int main() { S* p = new S(); void* v = dynamic_cast<void*>(p); S* p1 = dynamic_cast<S*>(v); // gives an

casting via void* instead of using reinterpret_cast

三世轮回 提交于 2019-11-26 09:23:35
问题 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

error: cast from &#39;void*&#39; to &#39;int&#39; loses precision

ⅰ亾dé卋堺 提交于 2019-11-26 08:02:45
问题 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? 回答1: 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. 回答2: Again,