为什么我的变量没有初始化?

阿德里安·霍尼(Adrien Horgnies)

我将代码简化为最简单的代码来隔离问题,我发现了问题所在,但无法解决。实际上,我什至不知道是否有问题。

我有一个旨在初始化未初始化变量并重新初始化已初始化变量的函数。我的问题是我声明的变量似乎已初始化。

这是剩下的代码:

/**
  * This software defines the type TabDyn and gives the tools to manipulate it
  * 
  * TabDyn is conceptually an array of integers. The first element is the size while the others are the members of the array.
  *
  * Here are the function provided to manipulate TabDyn :
  *     td_clear(TabDyn* td) : Create the TabDyn object if non existant and initialize it to an empty one. If it exists, it empties it.
  *
  */

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

// TabDyn[0] := size
// tabDyn[i] := i^th element of the array, first index being 1 (i>1)
typedef int* TabDyn;

/**
  * param - TabDyn* td_ptr : address of a declared int
  * void : initialize {td_ptr} to an empty array (size=0, no member)
  */
void td_clear(TabDyn* td_ptr)
{
    //this is the size of each member of TabDyn and thus the size of an empty TabDyn
    size_t TabDynByteCount = sizeof(int); 

    //We must free initialized TabDyn variables
    if(td_ptr && *td_ptr) 
    {
        printf("INITIALIZED!\n"); //#TOREMOVE#
        free(*td_ptr);  
    }

    //Create TabDyn object of size = 0 and give it to param
    *td_ptr = calloc(1, TabDynByteCount);
}

/**
  * Contains various test of the TabDyn function to ensure a correct behaviour by testing it at runtime with Valgrind
  */
int main()
{
    //* TEST decl-init-free #VALID:v0.04#
    printf("\n--- TEST OF td_clear BATCH 1 ---\n");
    printf("Declaring TabDyn variable\n");
    TabDyn tabTestAllocate;
    printf("Initialising TabDyn variable\n");
    td_clear(&tabTestAllocate);
    printf("Freeing now useless variables\n");
    free(tabTestAllocate);
    //*/
    //* TEST decl-init-init-free
    printf("\n--- TEST OF td_clear BATCH 2 ---\n");
    printf("Declaring TabDyn variable\n");
    TabDyn tabTestAllocate2;
    printf("Initialising TabDyn variable\n");
    td_clear(&tabTestAllocate2);
    printf("Re-initialising TabDyn variable\n");
    td_clear(&tabTestAllocate2); // It is not a duplicate
    printf("Freeing now useless variables\n");
    free(tabTestAllocate2);
    //*/
}

这就是Valgrind所说的:

--- TEST OF td_clear BATCH 1 ---
Declaring TabDyn variable
Initialising TabDyn variable
==10875== Conditional jump or move depends on uninitialised value(s)
==10875==    at 0x400654: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x4006CD: main (in /home/adrien/Documents/c/examen)
==10875== 
INITIALIZED!
==10875== Conditional jump or move depends on uninitialised value(s)
==10875==    at 0x4C2CDE1: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10875==    by 0x40066E: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x4006CD: main (in /home/adrien/Documents/c/examen)
==10875== 
==10875== Invalid free() / delete / delete[] / realloc()
==10875==    at 0x4C2CE2B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10875==    by 0x40066E: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x4006CD: main (in /home/adrien/Documents/c/examen)
==10875==  Address 0x400540 is in the Text segment of /home/adrien/Documents/c/examen
==10875==    at 0x400540: _start (in /home/adrien/Documents/c/examen)
==10875== 
Freeing now useless variables

--- TEST OF td_clear BATCH 2 ---
Declaring TabDyn variable
Initialising TabDyn variable
==10875== Conditional jump or move depends on uninitialised value(s)
==10875==    at 0x400654: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x40070D: main (in /home/adrien/Documents/c/examen)
==10875== 
INITIALIZED!
==10875== Conditional jump or move depends on uninitialised value(s)
==10875==    at 0x4C2CDE1: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10875==    by 0x40066E: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x40070D: main (in /home/adrien/Documents/c/examen)
==10875== 
==10875== Invalid free() / delete / delete[] / realloc()
==10875==    at 0x4C2CE2B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10875==    by 0x40066E: td_clear (in /home/adrien/Documents/c/examen)
==10875==    by 0x40070D: main (in /home/adrien/Documents/c/examen)
==10875==  Address 0xffefffe70 is on thread 1's stack
==10875== 
Re-initialising TabDyn variable
INITIALIZED!
Freeing now useless variables
==10875== 
==10875== HEAP SUMMARY:
==10875==     in use at exit: 0 bytes in 0 blocks
==10875==   total heap usage: 3 allocs, 5 frees, 12 bytes allocated
==10875== 
==10875== All heap blocks were freed -- no leaks are possible
==10875== 
==10875== For counts of detected and suppressed errors, rerun with: -v
==10875== Use --track-origins=yes to see where uninitialised values come from
==10875== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)

但是,如果我将变量显式初始化为NULL,就不会再有任何问题了:

 TabDyn tabTestAllocate = NULL;
 TabDyn tabTestAllocate2 = NULL;

我的变量不应该初始化为NULL吗?还是我的if语句未测试我认为测试的内容?

迈克猫

不,不建议将它们初始化为,NULL并且它们将具有不确定的值,因为它们具有自动存储时间,并且不会显式初始化。

N1256 6.7.8初始化

10如果未自动初始化具有自动存储期限的对象,则其值不确定。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的变量没有初始化值?

来自分类Dev

为什么变量没有初始化?

来自分类Dev

为什么函数内部的变量没有初始化?

来自分类Dev

为什么我的全局变量没有在Test :: Class中初始化?

来自分类Dev

为什么我的变量没有在此代码中初始化?

来自分类Dev

为什么我的代码块实例变量没有初始化并打印出“ NSGlobalBlock”?

来自分类Dev

为什么我的写方法没有初始化?

来自分类Dev

我的 C# 程序没有按照我认为的对象初始化顺序初始化对象。为什么?

来自分类Dev

为什么私有变量没有在 dart 中初始化?

来自分类Dev

尽管有初始化,为什么我会收到“在初始化之前使用的变量‘网格’”错误?

来自分类Dev

为什么最新的Swift版本总是说变量在使用前没有初始化

来自分类Dev

为什么没有变量时main初始化栈帧

来自分类Dev

为什么我无法在jQuery中初始化变量?

来自分类Dev

为什么我收到“未初始化的变量”错误

来自分类Dev

为什么我的 Python 数据类没有正确初始化布尔值?

来自分类Dev

为什么我的状态没有在 Flutter Consumer/Provider 中初始化?

来自分类Dev

为什么我的函数没有真正初始化数组?

来自分类Dev

为什么我的未初始化变量有效?

来自分类Dev

为什么我收到有关变量初始化的错误?

来自分类Dev

为什么快速关闭抱怨没有初始化

来自分类Java

为什么静态字段没有及时初始化?

来自分类Dev

为什么 x 没有在此初始化?

来自分类Dev

为什么Vue没有初始化?

来自分类Dev

为什么JSColor没有在输入上初始化?

来自分类Dev

为什么这似乎没有初始化

来自分类Dev

变量没有被初始化

来自分类Dev

为什么从来没有使用固定的“变量“错误”的初始化”?导致新的错误

来自分类Dev

当用Ruby解析文件时,为什么常量没有像局部变量那样初始化?

来自分类Dev

从单元测试调用时,为什么在Scala中没有初始化隐式变量?

Related 相关文章

  1. 1

    为什么我的变量没有初始化值?

  2. 2

    为什么变量没有初始化?

  3. 3

    为什么函数内部的变量没有初始化?

  4. 4

    为什么我的全局变量没有在Test :: Class中初始化?

  5. 5

    为什么我的变量没有在此代码中初始化?

  6. 6

    为什么我的代码块实例变量没有初始化并打印出“ NSGlobalBlock”?

  7. 7

    为什么我的写方法没有初始化?

  8. 8

    我的 C# 程序没有按照我认为的对象初始化顺序初始化对象。为什么?

  9. 9

    为什么私有变量没有在 dart 中初始化?

  10. 10

    尽管有初始化,为什么我会收到“在初始化之前使用的变量‘网格’”错误?

  11. 11

    为什么最新的Swift版本总是说变量在使用前没有初始化

  12. 12

    为什么没有变量时main初始化栈帧

  13. 13

    为什么我无法在jQuery中初始化变量?

  14. 14

    为什么我收到“未初始化的变量”错误

  15. 15

    为什么我的 Python 数据类没有正确初始化布尔值?

  16. 16

    为什么我的状态没有在 Flutter Consumer/Provider 中初始化?

  17. 17

    为什么我的函数没有真正初始化数组?

  18. 18

    为什么我的未初始化变量有效?

  19. 19

    为什么我收到有关变量初始化的错误?

  20. 20

    为什么快速关闭抱怨没有初始化

  21. 21

    为什么静态字段没有及时初始化?

  22. 22

    为什么 x 没有在此初始化?

  23. 23

    为什么Vue没有初始化?

  24. 24

    为什么JSColor没有在输入上初始化?

  25. 25

    为什么这似乎没有初始化

  26. 26

    变量没有被初始化

  27. 27

    为什么从来没有使用固定的“变量“错误”的初始化”?导致新的错误

  28. 28

    当用Ruby解析文件时,为什么常量没有像局部变量那样初始化?

  29. 29

    从单元测试调用时,为什么在Scala中没有初始化隐式变量?

热门标签

归档