void-pointers

Usage of void pointers across different platforms

微笑、不失礼 提交于 2019-11-28 02:47:12
问题 I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems? int x=100; int *pi=&x; printf("value of pi is: %p",(void*)pi); 回答1: printf is a variadic function and must be passed arguments of the right types. The standard says %p takes void * . Implicit cast doesn't take place for variadic functions. Quoting from N1570 7.21.6.1 The fprintf function p : The

Is comparing two void pointers to different objects defined in C++?

一世执手 提交于 2019-11-28 02:43:00
问题 Inspired by this answer about dynamic cast to void* : ... bool eqdc(B* b1, B *b2) { return dynamic_cast<void*>(b1) == dynamic_cast<void*>(b2); } ... int main() { DD *dd = new DD(); D1 *d1 = dynamic_cast<D1*>(dd); D2 *d2 = dynamic_cast<D2*>(dd); ... eqdc(d1, d2) ... I am wondering if it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid , but different objects . More generally, but possibly not as relevant,

How to return different types from a single function

这一生的挚爱 提交于 2019-11-28 02:21:43
问题 I have the following c code : #include <stdio.h> #include <stdlib.h> void *func(int a) { if (a==3) { int a_int = 5; int *ptr_int = &a_int; return (void *)ptr_int; } else if (a==4) { char a_char = 'b'; char *ptr_char = &a_char; return (void *)ptr_char; } else { fprintf(stderr, "return value is NULL"); return NULL; } } int main (int argc, char *argv[]) { int *ptr_int = (int *)func(3); char *ptr_char = (char *)func(4); fprintf(stdout, "int value = %d\n", *ptr_int); fprintf(stdout, "char value =

Find out Type of C++ Void Pointer

你说的曾经没有我的故事 提交于 2019-11-28 00:10:44
问题 I have a small question: how do I find out what type a C++ pointer is? I often use a small function in my console programs to gather input, which looks something like this: void query(string what-to-ask, [insert datatype here] * input) I would like to create a generic form, using a void pointer, but I can't cin a void pointer, so how to I find out it's type so I can cast it? 回答1: It's been a long time since the last time I coded in C++, but... Can't you use a Template? 回答2: You can't. However

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

主宰稳场 提交于 2019-11-27 22:49:05
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? You are dereferencing the void * before casting it to a concrete type. You need to do it the other way around: arguments vars = *(arguments *)

Determine Number of Frames in a Core Audio AudioBuffer

妖精的绣舞 提交于 2019-11-27 16:51:39
问题 I am trying to access the raw data for an audio file on the iPhone/iPad. I have the following code which is a basic start down the path I need. However I am stumped at what to do once I have an AudioBuffer. AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:urlAsset error:nil]; AVAssetReaderTrackOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[urlAsset tracks] objectAtIndex:0] outputSettings:nil]; [assetReader addOutput:assetReaderOutput];

Warning: cast to/from pointer from/to integer of different size

戏子无情 提交于 2019-11-27 13:04:00
问题 I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation. I compile using: gcc test.c -o test -pthread with GCC 4.8.1. And I get the warning test.c: In function ‘main’: test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] pthread_create(&(tid[i]), &attr, runner, (void *) i); ^ test.c: In function ‘runner’: test.c:54:22: warning: cast from pointer to integer of different size [-Wpointer

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

依然范特西╮ 提交于 2019-11-27 11:40:10
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. It defines a pointer-to-function type. The functions return void, and the argument list is unspecified because the question is (currently, but possibly erroneously) tagged C; if it were tagged C++, then the function would take no arguments at all. To make it a

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

若如初见. 提交于 2019-11-27 10:25:31
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#? 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 unformatted memory. This is what operator new and malloc return: a pointer to a block of memory of a certain size

how to use void ** pointer correctly?

家住魔仙堡 提交于 2019-11-27 10:22:26
问题 I am trying to use a double void pointer but I am a little bit confused about the usage. I have a struct that contains a void ** array. struct Thing{ void ** array; }; struct Thing * c = malloc (sizeof(struct Thing)); c->array = malloc( 10 * sizeof(void *) ); So If I want to assign a different object to each pointer and try to retrieve the value // Option 1 *(c->array + index) = (void *) some_object_ptr; // Option 2 c->array[index] = (void *) some_object_ptr; then, I have another function