Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Releasing a pointer that points a static array

Tags:

c++

pointers

I have the following array declared inside a header file.

static const float elementsArray[300] = { ... };

I want to do:

float *elementsPointer = &elementsArray[0];

I'm sure I can do that, but... Do I need to release elementsPointer with delete?

Do I need to do anything else?

Thank you.

like image 657
VansFannel Avatar asked Feb 21 '26 07:02

VansFannel


2 Answers

No. The memory that elementsPointer points to is statically allocated and does not require a call to free.

like image 151
Niki Yoshiuchi Avatar answered Feb 23 '26 21:02

Niki Yoshiuchi


Do I need to release elementsPointer with free

Exactly the opposite - you must not free that particular pointer value since it hasn't been allocated with malloc() or calloc().

If that pointer might sometimes be assigned with a value that was dynamically allocated you'll need to arrange (maybe with a flag) to call free() only with those memory blocks (if the code using elementsPointer is responsible for freeing that memory).

like image 44
Michael Burr Avatar answered Feb 23 '26 19:02

Michael Burr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!