Best practice pointer

I'm just starting to learn C and (of course) struggling with pointers :)

Given this snippet:

int *storage, *storage_p; storage = malloc(sizeof(int[GROW_BY])); storage_p = storage; // do something with storage, using storage_p free(storage); storage = NULL; 

Is it really necessary that the two variables declared to work with malloc() 'ed data? Is it good practice to create storage and storage_p , as I did? If not, what would be the β€œway”?

+4
source share
7 answers

You will need a pointer that stores the value returned by malloc () so you can free it later.

If you plan to use storage_p for, this will change its value, then you will need two pointers.

However, I usually keep a clean starting pointer and create new, special ones, as I need it.

 int *storage = (int*) malloc(sizeof(int[GROW_BY])); // : int* ptr = storage; while (*ptr) { // : ++ptr; } 
+3
source

I would only duplicate the pointer that malloc created for one reason: I want to change it.

For example, if you iterate over an array of characters you are endowed with that nose, I would copy the pointer to the new variable to iterate over and leave the first one untouched.

Also, when it comes to dynamic allocation, look at free listings, they simplify a lot:

http://en.wikipedia.org/wiki/Free_list

+2
source

I would suggest not having a copy of pointers. It just increases the likelihood that you might accidentally fool a free pointer, which you might accidentally use later, or an extra copy that you don't mean, or an extra copy that you might not want to double. I personally do not see the need for storage_p here.

Gool 'ol:

 int *storage = malloc(size_of_whatever); storage[0] = do_something(); free(storage); storage = NULL; 

will be good.

+1
source

No, I don’t see where you got anything with both storage and storage_p . I usually had one of them.

+1
source

The reason you want to do this is that after several manipulations with storage you may not remember what to do with free() . Having storage_p as a copy that you never modify helps prevent a memory leak, because you can call free(storage_p) later, regardless of what happened to storage . If this outweighs the disadvantages mentioned, it depends on the details of the situation.

Example:

 int *storage; storage = malloc(sizeof(int[GROW_BY])); storage++; free(storage); //SEGFAULT or MEMORY LEAK or OTHER BAD STUFF storage = NULL; 

against

 int *storage, *storage_p; storage = malloc(sizeof(int[GROW_BY])); storage_p = storage; storage++; free(storage_p); storage_p=NULL; storage = NULL; 
+1
source

This seems unnecessary to me. I'm not sure what might be the middle part, but I don't see any use for overlaying pointers like what if you are not modifying storage_p (for example, increasing its iteration). In fact, having two pointers with an alias can make tracking allocated memory difficult.

0
source

There is no inherent reason to reassign a space that was malloc'd for another pointer.

In any case, it will simply add lines once again and will compromise readability.

0
source

Source: https://habr.com/ru/post/1316581/


All Articles