标签: void-pointers

相当于C++的window.setTimeout()

在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++ function execution void-pointers

7
推荐指数
2
解决办法
1万
查看次数

C-> C++在#define中自动将void指针转换为C++中的Type指针,如果没有给出类型(C风格)[MSVS]

嗨!

我使用了以下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

谢谢!

c c++ casting void-pointers visual-c++

7
推荐指数
3
解决办法
4631
查看次数

确定核心音频AudioBuffer中的帧数

我试图在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)

core-audio pcm void-pointers avfoundation ios

7
推荐指数
1
解决办法
6353
查看次数

从(void*)类型中取消引用结构

我正在尝试使用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)

c void-pointers

7
推荐指数
1
解决办法
6396
查看次数

将块转换为void*以进行动态类方法解析

+(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

implementation casting dynamic objective-c void-pointers

7
推荐指数
1
解决办法
1503
查看次数

C#和void指针

我正在编写我的第一个C#应用程序,但幸运的是,我必须使用void指针(使用返回句柄的DLL).根据我的阅读,有几个选择:

  1. 不安全的代码,例如参见http://www.c-sharpcorner.com/UploadFile/gregory_popek/WritingUnsafeCode11102005040251AM/WritingUnsafeCode.aspx

  2. 在C#程序中写入我的函数declerations IntPtr而不是void*.例如:public static extern char SupportsWhatever(IntPtr h);

  3. 使用ref,例如:public static extern char SupportsWhatever(ref h);

还应该注意,我需要在DLL和C#应用程序之间来回传递信息.

不安全的选项是第一个出现在谷歌上的选项,但出于某些原因,在我的所有功能都感觉不对之前编写关键词unsafe.

有什么建议?

c# dll marshalling void-pointers

7
推荐指数
1
解决办法
3996
查看次数

const void*提供什么价值超过void*?

在C++中,const void *对于函数的参数类型使用a是否有任何价值void *?由于a void *是不透明的,除了用户之外是否有任何修改的风险reinterpret_cast,在这种情况下,他们同样可以const_cast在a上进行修改const void *,从而真正购买任何东西吗?我问,因为我正在使用一个实用程序模板类来提供共享指针,它提供了一个专门化void以避免void &问题,但没有提供专业化const void,因此我想知道这只是一个疏忽还是永远不需要它?

c++ const void-pointers const-cast reinterpret-cast

7
推荐指数
1
解决办法
9105
查看次数

三元运算符和类型转换中的混淆

我经历了这个问题 -

为什么结果:1 ? (int *)0 : (void *)0
与以下结果不同: 1 ? (int *)0 : (void *)1

它有何不同?它应该是0(int*)0.
如何查看结果?
我们可以在哪里使用这种表达方式?

c ternary-operator type-conversion void-pointers

7
推荐指数
1
解决办法
834
查看次数

(void*)cast-这是用来做什么的?

我确实尝试在SO上搜索这个,但我认为由于语法而不知道究竟要搜索什么,我变得有点不稳定.

我已经看到(void*)用于强制转换,通常用于函数调用.这是用来做什么的?

c++ casting void-pointers

7
推荐指数
1
解决办法
1万
查看次数

是否允许从TYPE*转换为unsigned char*?

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一样.

c void-pointers opaque-pointers

7
推荐指数
1
解决办法
570
查看次数