如何在c中初始化值

How to initialize values in c

我开始使用 learncodethehardway 书学习 C。有一个 exercise 它在哪里谈论valgrind。最后它询问 "fix" valgrind 错误。该程序非常简单,但无论我尝试什么,valgrind 都会抛出

Use of uninitialised value of size
Conditional jump or move depends on uninitialised value(s)

我在网上找过,没有找到相关的解决办法。我使用 malloc 来初始化内存,但这也不起作用。任何指针都会非常有帮助。

#include<stdlib.h>

int main( int c, char *argv[]){

    int age = malloc(sizeof(int));
    age = 12;
    int height = 12;

    printf("I am %d years old.\n",age);
    printf("I am %d inches tall.\n",height);

    return 0;
}

根据下面提供的解决方案,我更新了代码, 更新代码:

#include<stdlib.h>

int main( int c, char **argv[]){

    int* age = malloc(sizeof(int));
    *age = 12;
    int* height = malloc(sizeof(int)) ;
    *height = 23;

    printf("I am %d years old.\n",age);
    printf("I am %d inches tall.\n",height);

    free(age);
    free(height);
    return 0;
}

错误:

   ==19385== Memcheck, a memory error detector
==19385== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==19385== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==19385== Command: ./ex4
==19385== 
I am -16775192 years old.
==19385== Use of uninitialised value of size 8
==19385==    at 0x4E71EBB: _itoa_word (_itoa.c:195)
==19385==    by 0x4E73E96: vfprintf (vfprintf.c:1622)
==19385==    by 0x4E7D169: printf (printf.c:35)
==19385==    by 0x40053D: main (ex4.c:9)
==19385== 
==19385== Conditional jump or move depends on uninitialised value(s)
==19385==    at 0x4E71EC5: _itoa_word (_itoa.c:195)
==19385==    by 0x4E73E96: vfprintf (vfprintf.c:1622)
==19385==    by 0x4E7D169: printf (printf.c:35)
==19385==    by 0x40053D: main (ex4.c:9)
==19385== 
==19385== Conditional jump or move depends on uninitialised value(s)
==19385==    at 0x4E73FAA: vfprintf (vfprintf.c:1622)
==19385==    by 0x4E7D169: printf (printf.c:35)
==19385==    by 0x40053D: main (ex4.c:9)
==19385== 
==19385== Conditional jump or move depends on uninitialised value(s)
==19385==    at 0x4E73FC8: vfprintf (vfprintf.c:1622)
==19385==    by 0x4E7D169: printf (printf.c:35)
==19385==    by 0x40053D: main (ex4.c:9)
==19385== 
I am 0 inches tall.
==19385== 
==19385== HEAP SUMMARY:
==19385==     in use at exit: 0 bytes in 0 blocks
==19385==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==19385== 
==19385== All heap blocks were freed -- no leaks are possible
==19385== 
==19385== For counts of detected and suppressed errors, rerun with: -v
==19385== Use --track-origins=yes to see where uninitialised values come from
==19385== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 4 from 4)
int *age = malloc(sizeof(int));

*age = 12;
如果要分配内存并存储 malloc() 的 return 值,

age 应该是一个指针,然后取消引用指针以存储该值。

如果年龄只是一个变量那么做

int age = 12;

使用后释放内存

free(age);

使用完此内存后

malloc函数用于在堆上分配内存,它returns一个指向内存的指针。因此,age 不应该是一个 int,它应该是一个指向 int 的指针,或者 int*:

int* age = malloc(sizeof(int));

然后,您需要分配给 age 引用的位置 ,因此您需要使用 * 运算符取消引用 age :

*age = 12;

虽然对于这样一个简单的程序并不重要,但您还应该在程序终止之前释放堆上分配的所有内存以避免内存泄漏(尽管当您程序退出时,所有内存将被自动释放操作系统)。

只需将以下行放在 return 语句之前:

free(age);
  • 代码不包含 stdlib.h。
  • main 的格式不正确。
  • age 需要是一个指针。
  • 您需要检查 malloc 的结果。
  • 您需要 free() 分配的内存。