从结构的链接列表中打印字符串时,无效读取的大小为1

问题描述 投票:0回答:1

我已经创建了一个单链接结构列表,我正在尝试从中打印字符串。我的链接列表定义为:

typedef struct LinkedListNode
{
    void* data;
    struct LinkedListNode* next;
} LinkedListNode;

typedef struct LinkedList
{
    int count;
    struct LinkedListNode* head;
} LinkedList;

我的结构定义为:

typedef struct
{
    char missile[10]; /* trying to print this */
    funcPtr missileFunc;
} missileStruct;

我的printf语句是这样的:

printf("Print this: %s\n",(*(missileStruct*)((*currentNode).data)).missile);

Valgrind输出:

==11680== Invalid read of size 1
==11680==    at 0x4E88CC0: vfprintf (vfprintf.c:1632)
==11680==    by 0x4E8F898: printf (printf.c:33)
==11680==    by 0x401F9E: game (game.c:53)
==11680==    by 0x401112: menu (menu.c:57)
==11680==    by 0x400A6F: main (main.c:13)
==11680==  Address 0xa656c676e6973 is not stack'd, malloc'd or (recently) free'd
==11680== 
==11680== 
==11680== Process terminating with default action of signal 11 (SIGSEGV)
==11680==  General Protection Fault
==11680==    at 0x4E88CC0: vfprintf (vfprintf.c:1632)
==11680==    by 0x4E8F898: printf (printf.c:33)
==11680==    by 0x401F9E: game (game.c:53)
==11680==    by 0x401112: menu (menu.c:57)
==11680==    by 0x400A6F: main (main.c:13)
Print this: ==11680== 
c valgrind
1个回答
0
投票

解决了!原来我在我的h文件之一的typedef结构中有一个错字,我写的是char * missile而不是char missile [10]。

© www.soinside.com 2019 - 2024. All rights reserved.