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
     */
}
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top