gpt4 book ai didi

即使在正确初始化后,C 结构打印错误的值

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:59 25 4
gpt4 key购买 nike

在下面的程序中,我正确地为指针分配内存,然后正确地分配单个指针和设置值,即使我正在将垃圾值获取到结构成员之一。我不明白我到底哪里出错了。

下面程序的示例输出是:

***CK: H2 nSupport: 0

CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0

CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0
***CK: SP2 nSupport: 0

我不明白我是如何获得值 1303643608 的,尽管它在开始时设置正确。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOK 15
#define ML 100

typedef struct sample_test_for_ties_t
{
char sender[NOK];
char receiver[NOK];
char message[ML];
}sample_test_for_ties;

typedef struct CUOfS_t{
char cK[NOK];
char eK[NOK];
char nK[NOK];
char AL[ML];
int nSupport;
}CUOfS;

CUOfS **Btenders = NULL;
sample_test_for_ties test_ties[] = {
{"H2","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"FR2","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"SP2","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"H30","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"F30","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
{"W30","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
};

void InitBtenders(int numOfBCtenders)
{
int count =0;
if(!Btenders)
{
if(Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders))
{
while(count < numOfBCtenders)
{
Btenders[count] = NULL;
count++;
}
}
else
{
printf("Malloc failed\n");
}
}
}

void freeBtenders(int numOfBCtenders)
{
int count =0;
if(Btenders)
{
while(count<numOfBCtenders)
{
if(Btenders[count]) {
free(Btenders[count]);
Btenders[count] = NULL;
}
count++;
}
free(Btenders);
Btenders = NULL;
}
}

void UpdateBtendersInfo(char *aContenders)
{
static int counter =0;
if(Btenders)
{
if(Btenders[counter] == NULL) {
Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter]));
if(Btenders[counter])
{
strcpy(Btenders[counter]->cK,aContenders);
strcpy(Btenders[counter]->eK,"\0");
strcpy(Btenders[counter]->nK,"\0");
memset(Btenders[counter]->AL,0,sizeof(Btenders[counter]->AL));
Btenders[counter]->nSupport = 0;
counter++;
}
else
{
printf("Insufficient memory for Btender\n");
}
}
}
else
{
printf("Looks like memory not allocated for Btenders\n");
}
int count =0;
while(count <counter && Btenders[count])
{
printf("***CK: %s nSupport: %d\n",Btenders[count]->cK,Btenders[count]->nSupport);
count++;
}
printf("\n");
}

int main()
{
int numOfBCtenders = 3;
int noc =0;
InitBtenders(numOfBCtenders);
while(noc < numOfBCtenders)
{
UpdateBtendersInfo(test_ties[noc].sender);
noc++;
}
freeBtenders(numOfBCtenders);
return 0;
}

我希望

***CK: H2 nSupport: 0

但是我得到了

***CK: H2 nSupport: 1303643608

最佳答案

您为 Btenders 分配内存的方式不正确。

Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders)   // WRONG

sizeof (**Btenders)sizeof(CUOfS) 相同,但您需要 sizeof(CUOfS*) :

Btenders = (CUOfS **)malloc(sizeof (*Btenders) * numOfBCtenders)    // FIXED

同样适用于:

Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter]));    // WRONG

sizeof (Btenders[counter])sizeof(CUOfS*) 相同,但您需要 sizeof(CUOfS) :

Btenders[counter] = (CUOfS *)malloc(sizeof (*(Btenders[counter]))); // FIXED

关于即使在正确初始化后,C 结构打印错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58445914/

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