gpt4 book ai didi

c - Valgrind: "Conditional jump or move depends on uninitialised value(s)"

转载 作者:行者123 更新时间:2023-12-04 09:39:30 25 4
gpt4 key购买 nike

我正在尝试编写一个将新结构添加到链表中的函数。无论我做什么,Valgrind 都会不断给我这个错误。这是代码:

/* Stores a new address record to the linked list
*
* Parameters:
* first: pointer to the start of the linked list
* name: name of the new record to be added (same name can be stored multiple times)
* ad: IPv4 address to be stored with the name
*
* Returns:
* Pointer to the new node in linked list (that becomes the first node in list) */
struct addrRecord *storeIPv4(struct addrRecord *first, const char *name, const addr_4 *ad)
{
if (first==NULL) {
first=malloc(sizeof(struct addrRecord));
if (!first)
return NULL;
strcpy(first->name,name);
first->type=IPv4;
strcpy(first->u.in4.a,ad);
first->next=NULL;
return first;
}
struct addrRecord *new=malloc(sizeof(struct addrRecord));
if (!new)
return NULL;
strcpy(new->name,name);
new->type=IPv4;
strcpy(new->u.in4.a,ad);
new->next=first;
return new;
}

下面是主要代码:
struct addrRecord *recs = NULL;
addr_4 ad4 = {{128, 214, 4, 64}};
recs = storeIPv4(recs, "www.example.com", &ad4);
if (!recs)
return EXIT_FAILURE;

最后是标题:
#ifndef AALTO_ADDRESS_H
#define AALTO_ADDRESS_H

/* container for 32-bit IPv4 address */
typedef struct {
char a[4];
} addr_4;

/* container for 128-bit IPv6 address */
typedef struct {
char a[16];
} addr_6;

/* container for Unix domain address */
typedef struct {
char a[108];
} addr_un;

typedef enum {
NONE, // address not in use
IPv4,
IPv6,
UNIX
} adType;

/* One node in linked list of name / address entries */
struct addrRecord {
char name[20];
adType type;
union {
addr_4 in4;
addr_6 in6;
addr_un un;
} u;
struct addrRecord *next;
};

/* Store IPv4 address to addrRecord */
struct addrRecord *storeIPv4(struct addrRecord *first, const char *name, const addr_4 *ad);

/* Store IPv6 address to addrRecord */
struct addrRecord *storeIPv6(struct addrRecord *first, const char *name, const addr_6 *ad);

/* Store Unix domain address to addrRecord */
struct addrRecord *storeUnix(struct addrRecord *first, const char *name, const addr_un *ad);

/* Find address from linked list starting from 'first' */
struct addrRecord *findAddress(struct addrRecord *first, const char *name);

/* Prints the address in address record to standard output */
void printAddress(struct addrRecord *ad);

#endif /* AALTO_ADDRESS_H */

我得到的错误:
==358== Conditional jump or move depends on uninitialised value(s)
==358== at 0x4C25897: strcpy (mc_replace_strmem.c:311)
==358== by 0x403203: storeIPv4 (address.c:79)
==358== by 0x401706: initRec (test_source.c:30)
==358== by 0x401AB0: test_store (test_source.c:97)
==358== by 0x406350: srunner_run_all (in /tmc/test/test)
==358== by 0x402C5A: tmc_run_tests (tmc-check.c:121)
==358== by 0x402915: main (test_source.c:324)

抱歉,代码太长,提前致谢。

最佳答案

这只是一个猜测,因为您的代码不是一个完整的示例。

尽量不要使用 strcpy 复制 addr_4 结构。而是使用 memcpy。

所以,改变这一行:

strcpy(first->u.in4.a,ad);

对此:
memcpy(first->u.in4.a,ad,sizeof(*ad));
strcpy需要一个以空字符结尾的字符串,而 ad 可能不是。

关于c - Valgrind: "Conditional jump or move depends on uninitialised value(s)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22203885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com