execl 컴파일 시 경고:warning: not enough variable arguments to fit a sentinel 설명 (전재)
GCC 4 warnings about sentinels
Introduction
When compiling something with GCC 4, it may be possible that you get one of the following warnings:
warning: missing sentinel in function call warning: not enough variable arguments to fit a sentinel
This document explains what these warnings mean and how to resolve them.
Sentinels & warnings
A sentinel is a special value which indicates the end of a series of data. In the context of GCC, it is a null pointer which ends a list of parameters to a function.
The function
execl(const char * path, const char * arg, ...)
takes a variable amount of parameters. To know where to stop reading parameters, the last parameter must be a null pointer. When you do not end the list with a null pointer, execl will not stop scanning, which will result in errors. The following code, therefore, is wrong:
#include <unistd.h>
int main() {
execl("/bin/ls", "ls", "-l");
return 0;
}
The execl function call is not terminated by a null pointer and GCC will give a warning. The following code is better:
#include <unistd.h>
int main() {
execl("/bin/ls", "ls", "-l", NULL);
return 0;
}
As you can see, the execl function is now terminated by a null pointer and it will stop scanning it's arguments after that. However, GCC 4 will still issue a warning: warning: missing sentinel in function call This is because NULL is not of the right type: it is defined as integer 0 instead of a pointer with the value 0. Doing an explicit cast can make the warning go away:
#include <unistd.h>
int main() {
execl("/bin/ls", "ls", "-l", (char *)NULL);
return 0;
}
Using sentinels
One can specify that a function uses a sentinel by declaring it as follows:
char * build_path(const char * str, ...) __attribute__((__sentinel__(0)));
This indicates that the parameter list is ended with the special value 0, which must be a char pointer.
On most systems, there is no difference between 0 and (char *)0. On 64 bit systems, however, the integer 0 is 32 bits and the pointer 0 is 64 bits. The compiler does not know whether it is an integer or a pointer, and defaults for the integer. This will not clear the upper 32 bits and the function will not stop scanning its parameters.
home | home
© Sjoerd Langkemper
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.