0

This may be a deeper question than I expect, but can you see if a character pointer is unallocated? For example, a string that is unallocated is not NULL, as seen from when I ran this code:

char *ptr; /* Unallocated char pointer */
if (ptr == NULL) {
        ptr = malloc(10); /* Not casted cause it doesn't matter */
        printf("ptr is allocated\n");
        return(0);
}
printf("ptr is unallocated\n");
return(0);

When I ran the code, I received the message ptr is unallocated. That makes sense because the string could not have memory for a null character. So, is there any way to see if a string is unallocated? Or am I asking the wrong question?

3 Answers 3

4

No, it is not possible. In your case, ptr is uninitialised, which means it might contain any value at all, until the first time you assign something to it. It might be NULL, or it might not.

Sign up to request clarification or add additional context in comments.

4 Comments

So, then, if I were passing strings to functions and I would need to have already initialized them to NULL in all cases?
Yes, you must always initialise local variables. Otherwise, you can't make any assumptions about what they might be.
@Trifork: yes, if you want your program to behave properly.
@Greg Hewgill: Ah, I see. I wasn't sure if that also applied to strings, and whether or not NULL had any effect on uninitialized pointers.
2

That's because you defined ptr but didn't initialize it. Try this:

char *ptr = NULL;

It's a good coding style to initialize a pointer to a null pointer in general; it is crucial if you want to test whether it is initialized to some valid string.

2 Comments

Does setting ptr to NULL give it a null character, or is it just an acceptable form of initialization? Would you use malloc() afterwards to allocate it, or realloc()?
No; setting ptr to NULL means that ptr points to no memory address. You would then call ptr = malloc(1); *ptr = '\0'; to put a null character in the location pointed to by ptr
2

There is no way to do this without providing an initial value since unintialized automatic variables will have an indeterminate value. They must be initialized; there is no way to determine the initial value of an automatic variables otherwise. In fact using an uninitialized variable is undefined behavior.

The draft C99 standard says in section 6.7.8 Initialization paragraph 10 says:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [...]

and Annex J.2 Undefined behavior paragraph 1 says:

The behavior is undefined in the following circumstances:

and includes the following bullet:

The value of an object with automatic storage duration is used while it is indeterminate (6.2.4, 6.7.8, 6.8).

2 Comments

I see, so I can never use uninitialized variables in any case.
@Trifork you should never use the value of an unintialized variable, it is undefined behavior which means your program can literally do anything, although usually the results are behavior that appear normal but can break at anytime or a crash of some sort. The link I provided should be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.