Pycz
Pycz

Reputation: 376

Is malloc/realloc/calloc used?

For C programmers.

How can I know if a pointer char *, for example, was initialized by using malloc or realloc? I mean in kind of that function:

char* func(char** x){
    /* need some reallocating of *x but 
     * *x can be a pointer to const string
     */
}

Upvotes: 0

Views: 379

Answers (1)

John Bode
John Bode

Reputation: 123568

There's no portable way to determine whether a pointer refers to a static or auto variable, or to memory allocated via the *alloc functions, by looking at the pointer value alone. If you are intimately familiar with the memory model on your platform you could make some educated guesses, but that's about it.

Otherwise, if it matters, you will have to track that information yourself.

Upvotes: 2

Related Questions