align_alloc返回赋值警告 [英] aligned_alloc return assignment warning

查看:229
本文介绍了align_alloc返回赋值警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在清理警告,发现以下错误:

I am cleaning up warnings and found the following error:

warning: assignment makes pointer from integer without a cast buf = aligned_alloc(ALIGN_VALUE,BUF_SZ);

此调用本质上是函数的顶部:

This call is at the very top of the function, essentially:

char* buf;
buf = aligned_alloc(ALIGN_VALUE,BUF_SZ);

据我了解,aligned_alloc返回一个空*.如果将返回值从aligned_alloc强制转换为(char *),我得到:

It is my understanding that aligned_alloc returns a void *. If cast the return from aligned_alloc to a (char *) I get:

warning: cast to pointer from integer of different size [-Wint-to-pointer-ast] buf = (char*)aligned_alloc(ALIGN_VALUE,BUF_SZ);

似乎唯一可以解决的问题是

The only thing that seems to fix it is

buf = (char*)(uintptr_t)aligned_alloc(ALIGN_VALUE,BUF_SZ);

我确保我包含stdlib.h以避免在另一篇文章中引用隐式声明.我认为强制转换为char指针应该已经解决了这个问题.据我了解,我不明白为什么当void *和uintptr_t等效时,强制转换为uintptr_t才能解决该问题.

I have made sure that I am including stdlib.h to avoid implicit declarations referred to in another post. I assumed the cast to char pointer should have resolved this. I am not understanding why the cast to uintptr_t resolves it when void* and uintptr_t are equivalent as far as I understand.

以下是文件结构的示例

#include <syslog.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
#include <sys/ioctl.h>
#include <sys/mman.h> // mmap
#include <sys/time.h>
#include <unistd.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <pthread.h>

void* ax_read_thread(void* arg)
{
    fprintf(stderr, "read thread started\n");
    ax_priv* priv = (ax_priv*)arg;
    char* buf;
    uint32_t count = 0;

    size_t len, transferred = 0;
    buf = (char*)(uintptr_t)aligned_alloc(ALIGN_VALUE,BUF_SZ);

    if (buf == NULL){
        fprintf(stderr, "Aligned alloc failed\n");
        pthread_exit(NULL);
    }
    while(1){
    //do things
    }
}

感谢您提供的所有帮助.我现在看到警告是由于调用编译器时未指示正确的版本引起的.

Thank you for all of the help. I see now that the warning is a result of not indicating the proper version when invoking the compiler.

推荐答案

此答案在很大程度上总结了来自评论线程(包括我的和许多其他评论)的观察和建议,并将其包裹在一些说明性散文中.

This answer largely summarizes the observations and suggestions from the comments thread, including mine and many others', and wraps them in a bit of expository prose.

首先,出现此问题的原因是,当使用当前的工具链以当前形式构建程序时,未显式声明aligned_alloc()函数.在没有声明的情况下,编译器会推断其签名:它猜测该函数返回int,并且其参数类型是通过应用于该类型的默认参数提升获得的参数类型.实际争论.然后,编译器会警告您,这些推论(尤其是返回类型)似乎与您实际使用该函数的方式不一致.

In the first place, the problem arises because when you build your program with your present toolchain, in its current form, the aligned_alloc() function is not explicitly declared. In the absence of a declaration, the compiler is inferring its signature: it guesses that the function returns int, and that its parameter types are those obtained via the default argument promotions applied to the types of the actual arguments. The compiler then warns you that those inferences -- especially the return type -- seem inconsistent with how you're actually using the function.

该解决方案(假定该函数在您的C库中完全可用)是确保提供正确的原型.您可以可以手动插入原型,但不可以.由于它是标准库函数,因此应从相应的标头中获取其声明,该标头为stdlib.h.

The solution, supposing that the function is available in your C library at all, is to ensure that a correct prototype is provided. You could insert the prototype manually, but you shouldn't. Since it's a standard library function, you should get its declaration from the appropriate header, which for this function is stdlib.h.

但是,此特定功能是C11中的新增功能,并且显然您使用的是GCC版本,该版本默认为较早的标准进行编译. Glibc通过使用功能来保护C11中的新增功能,在某种程度上支持了这一点. -test宏_ISOC11_SOURCE.这是出于保护您的目的:如果您要为更早的标准编写代码,并且恰好提供的代码是与C11的新功能之一同名的自己的功能,则功能测试系统可以防止您遭受痛苦名称冲突.

HOWEVER, this particular function is new in C11, and evidently you're using a version of GCC that defaults to compiling for an earlier standard. Glibc supports that in part by protecting functions that are new in C11 with a feature-test macro, _ISOC11_SOURCE. This is for your protection: in the event that you're building code written for an earlier standard, and that code happens to provide is own function with the same name as one of C11's new functions, the feature-test system prevents you from suffering a name collision.

如果确实确实是为C11编写的,并且您的gcc版本具有支持C11的选项(即-std=c11和/或-std=gnu11),则在启用该选项的情况下进行编译是您最好的选择.如果您碰巧有一个提供aligned_alloc()的Glibc版本,但没有一个支持C11模式的编译器版本,那么您可以选择手动确保在编译器中的任何一个之前将所需的功能测试宏定义给了编译器.包含标准标头.您可以通过源文件顶部的#define或通过编译器的命令行选项(例如-D_ISOC11_SOURCE=1)来做到这一点.

If you are indeed writing for C11, as seems to be the case, and if your version of gcc has an option to support C11 (i.e. -std=c11 and/or -std=gnu11), then compiling with that option enabled is your best alternative. If you happen to have a version of Glibc that provides aligned_alloc() but not a version of the compiler that supports a C11 mode, then you have the alternative of manually ensuring that the needed feature test macro is defined to the compiler before any of the standard headers are included. You can do that via a #define at the top of your source file, or via a command-line option to the compiler (e.g. -D_ISOC11_SOURCE=1).

Glibc 确实至少在2.17版中具有aligned_alloc()(但我认为早在2.16版中).从4.8版开始,GCC 确实具有C11模式.如果这些组件的版本至少是最新版本,则在编译命令中添加选项-std=c11(以省略GNU扩展名)或-std=gnu11(以支持GNU扩展名)就足够了:

Glibc does have aligned_alloc() from at least version 2.17 (but I think from as early as 2.16). GCC does have a C11 mode since at least version 4.8. If your versions of these components are at least that recent, then it should be sufficient to add the option -std=c11 (to omit GNU extensions) or -std=gnu11 (to support GNU extensions) to your compilation command:

gcc -std=c11 my_program.c

这篇关于align_alloc返回赋值警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆