在javascript中有这个甜蜜,甜蜜的功能window.setTimeout( func, 1000 ) ;,它将在1000毫秒后异步调用func.
我想在C++中做类似的事情(没有多线程),所以我把一个示例循环放在一起,如:
#include <stdio.h>
struct Callback
{
// The _time_ this function will be executed.
double execTime ;
// The function to execute after execTime has passed
void* func ;
} ;
// Sample function to execute
void go()
{
puts( "GO" ) ;
}
// Global program-wide sense of time
double time ;
int main()
{
// start the timer
time = 0 ;
// Make a … 嗨!
我使用了以下C宏,但在C++中它无法自动转换void*为type*.
#define MALLOC_SAFE(var, size) { \
var = malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
我知道,我可以这样做:
#define MALLOC_SAFE_CPP(var, type, size) { \
var = (type)malloc(size); \
if (!var) goto error; \
}
Run Code Online (Sandbox Code Playgroud)
但我不想重写大部分代码,MALLOC_SAFE使用的地方.
有没有办法在没有给宏的类型的情况下这样做?也许一些MSVC 2005 #pragma/__declspec/其他?
ps:我不能使用C编译器,因为我的代码是大项目的一部分(数百个模块之一).现在它是在C++上.我知道,我可以单独构建我的代码.但这是旧代码,我只是想快速移植它.
问题是关于void*cast;)如果不可能,我只需用MACRO_SAFE_CPP替换MACRO_SAFE
谢谢!
我试图在iPhone/iPad上访问音频文件的原始数据.我有以下代码,这是我需要的路径的基本开始.但是,一旦我有了一个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;
AVAssetReaderOutput *output = [outputs objectAtIndex:0];
int y = 0;
while (ref = [output copyNextSampleBuffer]) {
AudioBufferList audioBufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
for (y=0; y<audioBufferList.mNumberBuffers; y++) {
AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
SInt16 *frames = audioBuffer.mData;
for(int i = 0; i < 24000; i++) { // This sometimes crashes
Float32 currentFrame …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用void指针传递数据,然后将其转换为(pData*)类型.我究竟做错了什么?gcc给了我
gcc test.c错误:请求成员'filename',而不是结构或联合
typedef struct data {
char *filename;
int a;
} pData;
void mod_struct(void *data) {
printf("%s\n",(pData *)data->filename); //error on this line
}
void main() {
pData *data;
data = (pData *) malloc(sizeof(pData));
data->filename = (char *)malloc(100);
strcpy(data->filename,"testing testing");
data->a=1;
mod_struct((void *)&data);
}
Run Code Online (Sandbox Code Playgroud) +(BOOL)resolveClassMethod:(SEL)aSel {
NSString *lString = NSStringFromSelector(aSel);
if ([self validateLetterAndAccidental:lString]) {
id (^noteFactoryBLOCK)(id) = ^(id aSelf) {
return [self noteWithString:lString];
};
IMP lIMP = imp_implementationWithBlock(noteFactoryBLOCK);
...
Run Code Online (Sandbox Code Playgroud)
我在最后一行收到错误,因为noteFactoryBLOCK被强制转换为void*而ARC不允许这样做.目前有办法实现我想要的吗?我想要一个IMP,我可以在运行时传递给class_addMethod.
编辑
IMP myIMP = imp_implementationWithBlock(objc_unretainedPointer(noteFactoryBLOCK));
Run Code Online (Sandbox Code Playgroud)
这条线给我一个警告而不是错误 - Semantic Issue: Passing 'objc_objectptr_t' (aka 'const void *') to parameter of type 'void *' discards qualifiers
我正在编写我的第一个C#应用程序,但幸运的是,我必须使用void指针(使用返回句柄的DLL).根据我的阅读,有几个选择:
在C#程序中写入我的函数declerations IntPtr而不是void*.例如:public static extern char SupportsWhatever(IntPtr h);
使用ref,例如:public static extern char SupportsWhatever(ref h);
还应该注意,我需要在DLL和C#应用程序之间来回传递信息.
不安全的选项是第一个出现在谷歌上的选项,但出于某些原因,在我的所有功能都感觉不对之前编写关键词unsafe.
有什么建议?
在C++中,const void *对于函数的参数类型使用a是否有任何价值void *?由于a void *是不透明的,除了用户之外是否有任何修改的风险reinterpret_cast,在这种情况下,他们同样可以const_cast在a上进行修改const void *,从而真正购买任何东西吗?我问,因为我正在使用一个实用程序模板类来提供共享指针,它提供了一个专门化void以避免void &问题,但没有提供专业化const void,因此我想知道这只是一个疏忽还是永远不需要它?
我经历了这个问题 -
为什么结果:1 ? (int *)0 : (void *)0
与以下结果不同:
1 ? (int *)0 : (void *)1
它有何不同?它应该是0或(int*)0.
如何查看结果?
我们可以在哪里使用这种表达方式?
我确实尝试在SO上搜索这个,但我认为由于语法而不知道究竟要搜索什么,我变得有点不稳定.
我已经看到(void*)用于强制转换,通常用于函数调用.这是用来做什么的?
C99 - 特别是第6.2.6.1节第4段 - 规定允许将对象表示复制到unsigned char数组中:
struct {
int foo;
double bar;
} baz;
unsigned char bytes[sizeof baz];
// Do things with the baz structure.
memcpy(bytes, &baz, sizeof bytes);
// Do things with the bytes array.
Run Code Online (Sandbox Code Playgroud)
我的问题:我们不能通过简单的转换避免额外的内存分配和复制操作吗?例如:
struct {
int foo;
double bar;
} baz;
unsigned char *bytes = (void *)&baz;
// Do stuff with the baz structure.
// Do things with the bytes array.
Run Code Online (Sandbox Code Playgroud)
当然,需要跟踪大小,但这首先是合法的,还是属于实现定义或未定义行为的范围?
我问,因为我正在实现一个类似的算法qsort,我希望它适用于任何类型的数组,就像它qsort一样.
void-pointers ×10
c ×4
c++ ×4
casting ×3
avfoundation ×1
c# ×1
const ×1
const-cast ×1
core-audio ×1
dll ×1
dynamic ×1
execution ×1
function ×1
ios ×1
marshalling ×1
objective-c ×1
pcm ×1
visual-c++ ×1