简体   繁体   中英

Pointer pointing to an empty array

I have a pointer that is pointing to the start of an array, but I need to check that it is unallocated. I thought of dereferencing the pointer and checking if NULL but that leads to a type error. Can anyone see what I'm doing wrong?

int mydispose(int *array){
    int i = 0;

    if(*array == NULL){
            return 1;
    }

    return ;

}

EDIT: Sorry if I was unclear: I have a pointer that points to the start of an array, but I wish to check whether the array is empty.

*array == NULL is wrong. You are dereferencing the pointer first (which could lead to a segfault if the pointer really is null) and then comparing its int value to a pointer value. Moreover, your compiler will perfectly accept that erroneous expression if NULL is defined as just 0 and not (void *) 0 .

You should be checking array == NULL to see if the passed pointer refers to anything, and then dereference it only in case it's not NULL .

Be aware, however, that dereferencing a non-null pointer isn't guaranteed to be a safe operation either. If the pointer contains a garbage value because it was allocated on the stack and not initialized, or if it refers to a deallocated region of memory, nasty bugs can happen.

You want if (array == NULL) -- but unless you first initialize array to NULL , it won't do any good either. I think you'd be better off backing up and telling us a bit more about what you're trying to accomplish, and trying to get help trying to accomplish your overall goal.

The only safe way to determine the allocation status of *array is to:

  1. Make sure *array is set to NULL is not allocated. int *array = NULL;
  2. Check if the array is NULL: if (array == NULL) return -1;

You can't reliably check if some memory location is allocated. *array is not a valid code, because it's the same as array[0] , but array[0] is not allocated. Non-allocated memory location can contain any value.

The only option is to ensure that you get the information whether the array is allocated alongside with your array. A popular option is representing unallocated array as NULL , but you may choose other option as well.

By the way, there is a difference between an empty array (that is, array of size 0), and array which is not allocated at all. The first option occurs when you use malloc(0) , the second when your pointer is not initialized at all. For malloc(0) it's allowed to return NULL , but it's allowed to return a non-NULL pointer (which you however can't dereference) as well. Both ways are valid according to the standard.

The immediate problem with your code is that you dereferencing array before comparing it to NULL. The type of the expression *array is int , not int * . Leave off the dereference operator and the types will match:

if (array == NULL) {...}

Note that an uninitialized pointer is only guaranteed to contain NULL if it was declared with static extent (ie it was either declared at file scope or with the static keyword in front of it). Similarly, calling free on a pointer won't set that pointer value to NULL; it will contain the same pointer value as before, but it will now be invalid.

你必须像这样使用它:

if(array == NULL) ...

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