1

I have this example from an official site:

char* variable1;
variable1=getenv ("PATH");
if (variable1!=NULL)
{
 printf("variable1=%s",variable1);
}
//Shouldn't this be here to avoid memory leak?
//delete variable1;

Or we can say that the pointer variable get its value in an equation and there is no need for deallocation because there is no allocation?

MD128
  • 501
  • 4
  • 12
  • 2
    `delete`? in C? how? – Sourav Ghosh Jan 06 '21 at 08:56
  • 1
    *"the pointer variable has a value in an equation and there is no need for deallocation"* - I don't quite follow the logic here. What equation? Anyway, there is no universal rule. It's up to the contract that the function you used works under. It may document that you need to `free` the pointer, or use a library specific deallocation function. It may also document itself as returning a pointer to an object with static storage duration. Read the documentation of the specific function you are using. There is no silver bullet. – StoryTeller - Unslander Monica Jan 06 '21 at 08:58
  • @SouravGhosh Why C? It is tagged C++ and the headline is C++... – Klaus Jan 06 '21 at 09:10
  • @StoryTeller-UnslanderMonica Can you give an example where a Ponter variable is first declared and then taken in an equation and also needs allocation? – MD128 Jan 06 '21 at 09:13
  • @alwaystudent Declaring a pointer per se doesn't allocate anything (besides the memory needed for the pointer variable itself). – πάντα ῥεῖ Jan 06 '21 at 09:14
  • ََ@πάνταῥεῖ The claim is that the equation for the pointer acts as an allocation and no longer needs pointer allocation. The question is whether we need deallocation or not – MD128 Jan 06 '21 at 09:22
  • @Klaus Please check the edit history, thanks! – Sourav Ghosh Jan 06 '21 at 11:46

2 Answers2

1

If you call malloc or similars you must deallocate the memory (using syscall free). Otherwise, if you call a third party library, they usually provide methods to perform cleanup so read on their docs to see if they have something. If the third party lib makes an allocation and returns it to you, they can't really tell when you're truly done using the variable so they cannot take care of deallocating it for you.

So the short answer is, well yes, but actually check what the docs say.

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
0

To quote the man page of getenv() :

As typically implemented, getenv() returns a pointer to a string within the environment list. The caller must take care not to modify this string, since that would change the environment of the process.

The implementation of getenv() is not required to be reentrant. The string pointed to by the return value of getenv() may be statically allocated, and can be modified by a subsequent call to getenv(), putenv(3), setenv(3), or unsetenv(3).

https://man7.org/linux/man-pages/man3/getenv.3.html

So in the case of getenv() you don't need to deallocate it yourself.

As the other answer mentioned, if YOU call malloc, you are required to use free. If you call library functions which do allocations, you call library freeing functions. If you are not sure, read their docs.

Raildex
  • 3,406
  • 1
  • 18
  • 42