const int buf_length = 255;
char buf[ buf_length + 1 ];
snprintf(buf, buf_length, "%d Next on [%s] %s:", channel, station_channel(channel), station_name(channel));
strncat(buf, "(", buf_length - strlen (buf));
strncat(buf, station_country( xmltv ), buf_length - strlen(buf));
strncat(buf, ")", buf_length - strlen (buf));
country_list_set_text( buf );
Run Code Online (Sandbox Code Playgroud)
这得到警告:
可变长度数组折叠为常量数组作为扩展名.
你能帮忙解决这个问题吗?
我使用下面的代码:
char filename[ 255 ];
strncpy( filename, getenv( "HOME" ), 235 );
strncat( filename, "/.config/stationlist.xml", 255 );
Run Code Online (Sandbox Code Playgroud)
收到此消息:
(warning) Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.
(error) Dangerous usage of 'filename' (strncpy doesn't always null-terminate it).
Run Code Online (Sandbox Code Playgroud) 我正在用C进行学习,无法识别内存泄漏情况
当我使用cppcheck检查代码时:get(错误)资源泄漏:f
enum bus_type {
MEDIA_BUS_UNKNOWN,
MEDIA_BUS_VIRTUAL,
MEDIA_BUS_PCI,
MEDIA_BUS_USB,
};
static enum bus_type get_bus(char *device)
{
char file[PATH_MAX];
char s[1024];
FILE *f;
if (!strcmp(device, "/sys/devices/virtual"))
return MEDIA_BUS_VIRTUAL;
snprintf(file, PATH_MAX, "%s/modalias", device);
f = fopen(file, "r");
if (!f)
return MEDIA_BUS_UNKNOWN;
if (!fgets(s, sizeof(s), f)) /* <-- (error) Resource leak: f */
return MEDIA_BUS_UNKNOWN;
fclose(f);
if (!strncmp(s, "pci", 3))
return MEDIA_BUS_PCI;
if (!strncmp(s, "usb", 3))
return MEDIA_BUS_USB;
return MEDIA_BUS_UNKNOWN;
}
Run Code Online (Sandbox Code Playgroud)
我觉得我在内存管理方面缺少一些东西。
我想在我的代码中用nanosleep替换过时的usleep函数:
static int timediff( struct timeval *large, struct timeval *small )
{
return ( ( ( large->tv_sec * 1000 * 1000 ) + large->tv_usec )
- ( ( small->tv_sec * 1000 * 1000 ) + small->tv_usec ) );
}
struct performance_s
{
struct timeval acquired_input;
};
performance_t *performance_new( int fieldtimeus )
{
performance_t *perf = malloc( sizeof( performance_t ) );
if( !perf ) return 0;
gettimeofday( &perf->acquired_input, 0 );
return perf;
}
performance_t *perf = 0;
int performance_get_usecs_since_frame_acquired( performance_t *perf …
Run Code Online (Sandbox Code Playgroud) 每次调用strtok_r()(从字符串中提取标记)后,我不应该释放s_ptr吗?
static void get_uevent_info(struct media_device_entry *md_ptr, char *dname)
{
FILE *fd;
char file[PATH_MAX], *name, *p;
char s[1024];
char *s_ptr;
snprintf(file, PATH_MAX, "%s/%s/uevent", dname, md_ptr->node);
fd = fopen(file, "r");
if (!fd)
return;
while (fgets(s, sizeof(s), fd)) {
p = strtok_r(s, "=", &s_ptr);
if (!p)
continue;
name = p;
p = strtok_r(NULL, "\n", &s_ptr);
if (!p)
continue;
if (!strcmp(name, "MAJOR"))
md_ptr->major = atol(p);
else if (!strcmp(name, "MINOR"))
md_ptr->minor = atol(p);
}
fclose(fd);
}
Run Code Online (Sandbox Code Playgroud)
我从未使用过这个功能,所以也许我错了.
最好的祝福.
我完全陷入了困境.我在3个文件中有以下代码:
文件mixer_oss.c
#include "mixer.h"
static char *devices[] = SOUND_DEVICE_NAMES;
static char **oss_get_device(void)
{
int i, o, devs, res;
char **result;
if ((ioctl(fd, SOUND_MIXER_READ_RECMASK, &devs)) == -1) {
return NULL;
} else {
result = malloc(sizeof(char*)*SOUND_MIXER_NRDEVICES);
o = 0;
for (i=0; i < SOUND_MIXER_NRDEVICES; i++) {
res = (devs >> i)%2;
if (res) {
result[o] = malloc(strlen(devices[i])+1);
sprintf(result[o], "%s", devices[i]);
o++;
}
result[o] = NULL;
}
}
return result;
}
struct mixer oss_mixer = {
.get_device = oss_get_device,
};
Run Code Online (Sandbox Code Playgroud)
文件mixer.h
#ifdef …
Run Code Online (Sandbox Code Playgroud) 我有以下代码从命令行读取参数.如果字符串是这种形式hw:1,0我想打破.
gboolean parse_one_option (gint opt, const gchar * arg, GError ** err)
{
switch (opt) {
case DEVICE:
if (!strncmp(arg, "hw:", 3) && isdigit(arg[3]) && arg[4] == ',' && isdigit(arg[5])) {
char *device = g_strdup (arg);
break;
break;
Run Code Online (Sandbox Code Playgroud)
编译器给了我一个警告:
warning: implicit declaration of function 'isdigit' is invalid in C99 [-Wimplicit-function-declaration]
if (!strncmp(arg, "hw:", 3) && isdigit(arg[3]) && arg[4] == ',' && isdigit(arg[5])) {
^
Run Code Online (Sandbox Code Playgroud)
还有一个问题:
将g_strdup与GOptionContext结合使用是正确的