c - 在结构体中设置 const char 指针

标签 c string struct valgrind

我正在尝试编写一个程序,在其中可以创建 token ,以便每个 token 都有一个常量类型和值。

token.h:

#ifndef TOKEN_H
#define TOKEN_H
typedef struct{
    const char *type;
    const char *value;
}token;

token *gen_token(const char *r, const char *val);

#endif

token .c:

#include <stdlib.h>
#include <string.h>
#include "lib/token.h"

token *gen_token(const char *type, const char *val){
    token o = {type, val}, *out = &o;
    return out;
}

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "lib/token.h"

int main(int argi, char *argv[]){
    const char th[] = "line", va[] = ":";

    token *t = gen_token(th, va);

    printf("(%s, %s)\n", t->type, t->value);

    return 0;
}

当我正常运行时,似乎没有发生错误,但是当 valgrind 查看它时,它会提示很多。这是我的日志:

==4086== Memcheck, a memory error detector
==4086== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4086== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4086== Command: ./a lol
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4E7FD4F: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C2EBE2: strlen (vg_replace_strmem.c:458)
==4086==    by 0x4E80D77: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C2EBF4: strlen (vg_replace_strmem.c:458)
==4086==    by 0x4E80D77: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4EAAEEB: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1307)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4EAAEF1: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1309)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4EAAF03: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1307)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4EAAF0D: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1309)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EB3: is_overlap (vg_replace_strmem.c:137)
==4086==    by 0x4C32EB3: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EB9: is_overlap (vg_replace_strmem.c:140)
==4086==    by 0x4C32EB9: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EC7: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32ECC: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32F64: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C32F70: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C32F7E: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086==  (line, :)
==4086== 
==4086== HEAP SUMMARY:
==4086==     in use at exit: 0 bytes in 0 blocks
==4086==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==4086== 
==4086== All heap blocks were freed -- no leaks are possible
==4086== 
==4086== For counts of detected and suppressed errors, rerun with: -v
==4086== ERROR SUMMARY: 36 errors from 14 contexts (suppressed: 0 from 0)

我非常感谢对我的代码的任何帮助和/或批评。

最佳答案

在函数中

token *gen_token(const char *type, const char *val){
    token o = {type, val}, *out = &o;
    return out;
}

变量out指向局部变量o,该变量在函数返回后被释放。这纯粹是偶然的,您可以在调用函数后读取这些值。因此有一个关于

的警告

Uninitialised value was created by a stack allocation

相反,您需要分配内存并将值复制到其中。 注意你需要记住在退出程序之前释放分配的内存,特别是如果你想让 valgrind 开心:)

关于c - 在结构体中设置 const char 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291712/

相关文章:

c - 链接列表中的取消引用错误 - 不是 typedef 错误

char *argv[] 到普通 C 中的浮点类型转换

c - 从文件中读取数据到结构体数组 (C)

c - csv 文件中的指针和结构

具有默认值的 c 结构?

c - 用消息队列C发送文件内容

c - 循环展开不适用于剩余元素

php - 将字符串转换为 mySql DECIMAL 类型

c - strncat() 上的段错误

c - C中使用getchar存储用户输入的多个字符