ac 函数中是否总是需要 return 语句?

Sha*_*jun 3 c gcc

PUBLIC void delset( set )\nSET *set;\n{\n    /* Delete a set created with a previous newset() call. */\n\n    if( set->map != set->defmap )\n        free( set->map );\n    free( set );\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

ac 函数中是否总是需要有 return 语句?上面的函数会抛出以下警告。请注意,我没有返回语句。

\n\n
src/tools/set.c: In function \xe2\x80\x98delset\xe2\x80\x99:\nsrc/tools/set.c:41:1: warning: control reaches end of non-void function [-Wreturn-type]\n }\n ^\n
Run Code Online (Sandbox Code Playgroud)\n\n

一旦我添加return 0;警告就会消失。即使我只是指定,我也会收到警告return;

\n\n
PUBLIC void delset( set )\nSET *set;\n{\n    /* Delete a set created with a previous newset() call. */\n\n    if( set->map != set->defmap )\n        free( set->map );\n    free( set );\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在用gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

\n\n

编辑

\n\n

# define PUBLIC

\n\n

看起来这只是一个标记。这个问题似乎比我能更好地解释它。

\n\n
void delset( set )\nSET *set;\n{\n    /* Delete a set created with a previous newset() call. */\n\n    if( set->map != set->defmap )\n        free( set->map );\n    free( set );        \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我删除了函数前面的 PUBLIC 标记和底部的返回行。我仍然遇到同样的错误。

\n\n
typedef struct _set_\n{\n    unsigned char nwords;                                   /* Number of words in map */\n    unsigned char compl;                                    /* is a negative true set if true */\n    unsigned nbits;                                         /* Number of bits in map */\n    _SETTYPE *map;                                          /* Pointer to the map */\n    _SETTYPE defmap[ _DEFWORDS ];                           /* The map itself */\n\n} SET;\n
Run Code Online (Sandbox Code Playgroud)\n\n

包含以下头文件 ( debug.h) 导致了该问题。不确定到底是哪条线造成的。

\n\n
#ifdef DEBUG\n#   define PRIVATE\n#   define D (x) x\n#else\n#   define PRIVATE static\n#   define D (x)\n#endif\n#   define PUBLIC\n\n#ifdef MSDOS\n#   define MS(x) x \n#   define UX(x)\n#   define ANSI\n#   define _8086\n#else\n#   define MS(x)\n#   define UX(x) x\n#   define O_BINARY 0 /*no binary input mode in UNIX open() */\n    typedef long time_t; /* for the VAX, may have to change this */\n    typedef unsigned s1ze_t; /* for the VAX, may have to change this. Renamed the type as s1ze_t as stdio.h contains a type of the same name */\n    extern char *strdup(); /* You need to supply one. */\n#endif\n\n#ifdef ANSI /* If ANSI is defined, put arg lists into */\n#   define P(x) x /* function prototypes. */\n#   define VA_LIST ... /* and use ellipsis if a variable number of args */\n#else\n#   define P(x) () /*Otherwise, discard argument lists and translate*/\n#   define void char /* void keyword to int. */\n#   define VA LIST _a_r_g_s /* don\'t use ellipsis */\n#endif\n\n/* SEG (p) Evaluates to the segment portion of an 8086 address.\n * OFF (p) Evaluates to the offset portion of an 8086 address.\n * PHYS (p) Evaluates to a long holding a physical address\n */\n\n#ifdef _8086\n#   define SEG(p) ( ((unsigned *)&(p)) [1] )\n#   define OFF(p) ( ((unsigned *)&(p)) [0] )\n#   define PHYS(p) (((unsigned long)OFF(p)) + ((unsigned long)SEG(p) << 4))\n#else\n#   define PHYS(p) (p)\n#endif\n\n/* NUMELE (array) Evaluates to the array size in elements\n * LASTELE(array) Evaluates to a pointer to the last element\n * INBOUNDS(array,p) Evaluates to true i f p points into the array.\n * RANGE(a,b,c) Evaluates to true i f a <= b <= c\n * max(a,b) Evaluates to a or b, whichever is larger\n * min (a, b) Evaluates to a or b, whichever is smaller\n *\n * NBITS (type) Returns number of bits in a variable of the indicated\n * type;\n * MAXINT Evaluates to the value of the largest signed integer\n */\n\n#define NUMELE(a)       (sizeof(a)/sizeof(*(a)))\n#define LASTELE(a)      ((a) + (NUMELE(a)-1))\n#define TOOHIGH(a,p)        ((p) - (a) > (NUMELE(a) - 1))\n#define TOOLOW(a,p)         ( (p) - (a) < 0 )\n#define INBOUNDS(a,p)       (!(TOOHIGH(a,p) || TOOLOW(a,p))\n\n#define _IS(t, x) (((t)1 << (x)) != 0) /* Evaluate true if the width of a */\n                    /* variable of type of t is < x. The !=0 */\n                    /* assures that the answer is 1 or 0 */\n\n#define NBITS(t) (4 * (1 + _IS(t, 4) + _IS(t, 8) + _IS(t,12) + _IS(t, 16) + _IS(t,20) + _IS(t,24) + _IS(t,28) + _IS(t,32) ) \n\n\n#define MAXINT (((unsigned)~0) >> 1)\n\n#ifndef max\n#   define max(a, b)    (((a) > (b)) ? (a) : (b))\n#endif\n#ifndef min\n#   define min(a,b)     (((a) < (b)) ? (a) : (b))\n#endif\n#define RANGE(a,b,c)    ((a) <= (b) && (b) <= (c))\n
Run Code Online (Sandbox Code Playgroud)\n

M.M*_*M.M 5

不需要返回语句。

如果您的函数具有非 void 返回类型,并且您不返回值,并且调用者使用返回值,那么它会导致未定义的行为。

编译器会发出警告,因为您的函数具有非 void 返回类型,并且它认为您可能打算返回一个值但忘记了。

您发布的代码与编译器消息不对应,除非PUBLIC定义为奇怪的东西。