void-pointers

Converting a void* to a std::string

余生长醉 提交于 2019-11-29 05:15:57
问题 After perusing the web and messing around myself, I can't seem to convert a void*'s target (which is a string) to a std::string. I've tried using sprintf(buffer, "%p", *((int *)point)); as recommended by this page to get to a C string, but to no avail. And sadly, yes, I have to use a void*, as that's what SDL uses in their USEREVENT struct. The code I'm using to fill the Userevent, for those interested, is: std::string filename = "ResumeButton.png"; SDL_Event button_press; button_press.type =

Determine Number of Frames in a Core Audio AudioBuffer

前提是你 提交于 2019-11-29 02:24:09
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]; [assetReader startReading]; CMSampleBufferRef ref; NSArray *outputs = assetReader.outputs;

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

烂漫一生 提交于 2019-11-28 20:22:49
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-to-int-cast] int threadnumber = (int) param; ^ This error comes for the following code: #include

How to make generic function using void * in c?

不想你离开。 提交于 2019-11-28 18:23:50
I have an incr function to increment the value by 1 I want to make it generic,because I don't want to make different functions for the same functionality. Suppose I want to increment int , float , char by 1 void incr(void *vp) { (*vp)++; } But the problem I know is Dereferencing a void pointer is undefined behaviour . Sometimes It may give error : Invalid use of void expression . My main funciton is : int main() { int i=5; float f=5.6f; char c='a'; incr(&i); incr(&f); incr(&c); return 0; } The problem is how to solve this ? Is there a way to solve it in C only or will I have to define incr()

Test for void pointer in C++ before deleting

空扰寡人 提交于 2019-11-28 13:57:17
I have an array in C++: Player ** playerArray; which is initialized in the constructor of the class it is in. In the destructor I have: delete playerArray; except when testing the program through Valgrind it says that there are some calls to delete to a void pointer: operator delete(void*) I want to test whether the playerArray is a void pointer before calling delete to avoid this error. Does anyone know how to do this? Perhaps you meant delete [] playerArray . You need the [] if the pointer is an array, not a single instance. Here's how operator delete is defined. void operator delete(void*)

Byte precision pointer arithmetic in C when sizeof(char) != 1

久未见 提交于 2019-11-28 13:05:40
How can one portably perform pointer arithmetic with single byte precision? Keep in mind that: char is not 1 byte on all platforms sizeof(void) == 1 is only available as an extension in GCC While some platforms may have pointer deref pointer alignment restrictions, arithmetic may still require a finer granularity than the size of the smallest fundamental POD type Your assumption is flawed - sizeof(char) is defined to be 1 everywhere. From the C99 standard (TC3) , in section 6.5.3.4 ("The sizeof operator"): (paragraph 2) The sizeof operator yields the size (in bytes) of its operand, which may

can void* be used to store function pointers? [duplicate]

妖精的绣舞 提交于 2019-11-28 11:58:48
This question already has an answer here: Why are function pointers and data pointers incompatible in C/C++? 14 answers void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? int send(); void* p = send; Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable? No it may not. According to the C Standard (6.3.2.3 Pointers) 1 A pointer to void may be converted to or from a pointer to any object type . A pointer to any object type may be converted to a pointer

How void pointer arithmetic is happening in GCC

流过昼夜 提交于 2019-11-28 11:46:15
int main() { int a; void *p; p = &a; printf("%ld\n",(long)p); p = p+1; printf("%ld\n",(long)p); } In this program, p+1 is just incrementing the value of p by 1. I know void pointer arithmetic is not possible in C , so GCC is doing it implicitly. And if yes, then is it taking it as char pointer . Also, why dereferencing is not possible for void pointer, if it is implicitly doing pointer arithmetic. C does not allow pointer arithmetic with void * pointer type. GNU C allows it by considering the size of void is 1 . From 6.23 Arithmetic on void- and Function-Pointers : In GNU C, addition and

C: Extrapolating type from void pointer

房东的猫 提交于 2019-11-28 09:58:29
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? 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 some of those bits to tell you what the bits are, there's no consistent way to identify it. Sure you can, and

Regarding dereferencing 'void *' pointer

柔情痞子 提交于 2019-11-28 09:20:46
问题 I am not able to find out how to remove the above warning from the below line of code. data is is void pointer and as part of callback function will be receiving string in the data pointer. As I have typecast the void pointer but compiler still showing the warning. There are basically two warnings showing up on the below line. 1. dereferencing 'void *' pointer 2. taking address of expression of type 'void service_ind = atoi((const char*)&data[at_response.param[0].start_of_value_index]) ? TRUE