简体   繁体   中英

How to check uninitialized pointer in C

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 to x+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"); 
}

One thought is to find a place in the memory, have the subroutine put some weird stuff there the first time, ie *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.

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 Initialization

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.

Replace

char *min_ptr, *max_ptr;

By

char *min_ptr = 0, *max_ptr = 0;

You can't verify if a pointer has been initialized, but by testing if its value is NULL or not.

You can use a static variable for this.

void spray_paint( char *x, int sz, char tok, char *t )
{
    static int initialized = 0;

    if (!initialized)
    {
        initialized = 1;
        min_ptr = x;
        max_ptr = x;
    }
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM