如何检查用C未初始化的指针

如何检查用C未初始化的指针

问题描述:

我的计算机学教授给我们布置任务。基本上,我们有这两个全球指针未初始化, * min_ptr,* max_ptr 。我们写这个无效spray_paint(字符* X,INT SZ,焦炭托克,字符* T),这需要TOK的价值并投入某处内存启动功能 X X + SZ-1 的char * T 只是为了调试的一些随机的事情。 * min_ptr,* max_ptr 用于跟踪我们打印的东西的范围。不过,我有麻烦初始化前两个指针。起初,我试图检查,如果他们被初始化为NULL,但我知道他们是在默认情况下C99后才初始化为NULL,我的教授说,你不能假设他们被初始化为NULL,如果没有指定,我们只能访问无效spray_paint(字符* X,INT SZ,焦炭托克,字符* T),而不是其他任何地方在code。反正是有检查两个全球指针是否初始化?或者,反正是有初始化它们第一次子程序调用,决不再做一次吗?

My CS professor gives us this assignment. Basically we have these two global pointers uninitialized, *min_ptr, *max_ptr. We are writing this void spray_paint( char *x, int sz, char tok, char *t ) function that takes the value of tok and puts into somewhere in the memory starting from x tox+sz-1. char *t is just some random thing for debugging purpose. *min_ptr, *max_ptr are used to keep track of the range of the stuff we have printed. However, I am having trouble initializing the first two pointers. At first, I was trying to check if they are initialized to NULL, but I realize that they are only initialized to NULL by default after C99, and my professor says that you can't assume they are initialized to NULL if not specified, and we only have access to void spray_paint( char *x, int sz, char tok, char *t ), not anywhere else in the code. Is there anyway to check whether the two global pointers are initialized? Or is there anyway to initialize them the first time the subroutine being called, and never do it again?

char *min_ptr, *max_ptr;
void spray_paint( char *x, int sz, char tok, char *t )
{
char *marker = x + 3;
char *painter = x;
int k;

//if (!min_ptr || !max_ptr)
if (*marker != '~')
{
    *marker = '~';
    min_ptr = x;
    max_ptr = x + sz - 1;
}

for (k = sz - 1; k >= 0; k--)
{
    *(painter+k) = tok;
    if ((unsigned long) x < (unsigned long) min_ptr)
        min_ptr = x;
    else if ((unsigned long) x > (unsigned long) max_ptr)
        max_ptr = x;
}

printf("%s\n",t);
printf("\n"); 
}

一个想到的就是找在记忆的地方,都子程序把一些奇怪的东西出现在第一时间,即 *标记,我们只是需要检查有这样一个奇怪的事情还是不知道是否子程序之前已经调用。这引出了另一个问题,可以不返回任何一个子程序做了一些变化,呆在那里子程序被终止即使?我试了一下X code,与C,但问题是,任我没有获得我所选择的任意位置,或者我不知道,我应该选择是不会影响到位置程序并不会被其他意外操作来改变。

One thought is to find a place in the memory, have the subroutine put some weird stuff there the first time, i.e *marker, and we just need to check if there is such a weird thing or not to know whether the subroutine has been called before. That leads to another question, can a subroutine that returns nothing make some changes that stay there even after the subroutine is terminated? I tried it on Xcode, with C, but the problem is either I don't have the access to the random location I have chosen, or I don't know where I should choose the location that is not gonna affect the program and won't be changed by other unexpected operations.

全局变量(静态变量)没有明确的初始化保证是 0 算术类型和空指针的指针类型。由于C89这一直是如此。

The global variable (and static variable) not explicit initialized are guaranteed to be 0 for arithmetic type and null pointer for pointer type. This has been true since C89.

C89§3.5.7初始化

如果有静态存储持续时间的对象不明确初始化,它是隐式初始化,如果每一个有算术类型成员被分配0和每一个具有指针类型成员被分配一个空指针常量。如果有自动存储时间的对象没有明确初始化,它的价值是不确定的。

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant. If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.