Sign in to follow this  
mychii

Noob pointer question

Recommended Posts

Hi guys,

 

I've been in JavaScript for a very long time for work, and now back to C++ due to excitement of Vulkan. Of course, my hazy memory about pointers already got into me to lots of pointer errors. So here's a quick one...

 

Why is this working:

int* a = new int(5);
int* pA = a;
delete pA;

But this one isn't:

int a = 5;
int* pA = &a;
delete pA;

I may be wrong, but is it because I'm trying to delete a pointer (is it even a pointer I'm deleting?) allocated in reserved memory or something?

 

Thanks!

Edited by Alectora

Share this post


Link to post
Share on other sites

There are some things you will have to lookup and take your time to get a good understanding of. I'll try to give a short version of that in this post, but it's by no means complete, but I hope it's enough to send you off in the right direction.

 

Using new and delete in C++ are ways of allocating and deallocating memory. It's true that new returns a pointer and that delete takes a pointer as input, but there is no such thing as deleting a pointer.

 

What new does is basically request some memory (the size of the given type) and returns something that allows you to access that part of memory, a pointer. A pointer is nothing more than some value indicating a location in memory. If you view your memory as a giant array of bytes, a pointer would just be an index. When this memory is allocated, it'll stay that way until you instruct the computer to deallocate it. Unlike JavaScript, you have to manually manage this memory. You can deallocate memory by calling delete and passing it the pointer you got from new.

 

So why doesn't it work in your second case? Well there are two types of memory allocation, one is for static allocations and the other for dynamic allocations. The stack is used for static allocation and the heap is for used for dynamic allocations. The stack is basically some memory allocated beforehand, which requires no work from your side, that's used for things like local variables and function arguments. It's called the stack because it's literally how it works. Things are put on top in some order and in then they're taken off in the reverse order. In code that means that at the beginning of a scope (e.g. a function) the necessary space is reserved, and at the end of the scope (e.g. exiting a function) that reservation is lifted. Everything about this is automatic, but the stack has limited size and one other limitation; what if you want to retain the memory outside of the scope? This can't be managed automatically by the stack, it only knows how to put stuff on and then take it off again, it wouldn't be able to know when that part of memory will be free for use again. This is where heap memory comes in, it's memory that you can manually manage and is practically unlimited in size. This manual management is done with new and delete.

 

In your second example, you are trying to delete memory from the stack, but delete is meant for heap allocations.

Share this post


Link to post
Share on other sites

Oh gosh, thanks guys!

 

Just want to re-confirm to myself (please correct me if I conclude this wrong):

If I "delete" pA, it's as if I'm "deleting" a, cause I'm deleting what they are pointing at. This means deleting the memory they are pointing to, NOT the pointer itself (no such thing!).

Since the 2nd case's pA is pointing to a stack memory, deleting it will definitely not work.

1st case works cause I'm allocating it to a heap (with new), and therefore I have the ability to manually delete them, which of course no error to do so. I also can't use anymore cause I'm deleting the memory that both pA and a pointed to.

 

@fastcall22

Yeah I've attempted a pool allocation before, but it's already 2 years back when I had to leave country for a while. Now everything went quite blank.  :(

 

Okay now I remember these stack and heap thing. I will re-read these two.   :)

Edited by Alectora

Share this post


Link to post
Share on other sites

Ah, I forgot, just one more. Based on 1st case, are 'a' and 'pA' themselves go to a stack memory for storing values after deletion? I mean, after deleting the memory they are pointing to, they themselves are still there... pointing to zero or whatever value isn't it?

Share this post


Link to post
Share on other sites

Ah, I forgot, just one more. Based on 1st case, are 'a' and 'pA' themselves go to a stack memory for storing values after deletion? I mean, after deleting the memory they are pointing to, they themselves are still there... pointing to zero or whatever value isn't it?

 

The pointers themselves are on the stack.  They're basically just integers (of whatever size the system uses for pointers) that hold the address.  Once you call delete the memory that they're pointing to gets deallocated, but the pointer stays on the stack with whatever value they had before.  It's up to you to set them to NULL.

Share this post


Link to post
Share on other sites

Based on 1st case, are 'a' and 'pA' themselves go to a stack memory for storing values after deletion? I mean, after deleting the memory they are pointing to, they themselves are still there... pointing to zero or whatever value isn't it?


Yes, a variable's value is stored somewhere as well (for a pointer, that value is the integer representing the memory address).

Variable storage depends on how it's declared:

Local variables and function parameters: Stored on the stack and/or in registers. The compiler decides based on how you use the variable and what optimizations it can do.

struct/class member fields: Stored as part of the struct/class (i.e. on the stack if it's a local variable or on the heap if you 'new' it).

Global/static variables: Stored in a non-stack, non-heap region of memory that is cleaned up when the program exits. You aren't allowed to 'delete' memory in this region.

Thread static variables: Stored in thread-local-storage. You aren't allowed to 'delete' memory in this region, either. Edited by Nypyren

Share this post


Link to post
Share on other sites

// Create a variable of type int pointer that points to an object of type int with value 5

int* a = new int(5); 

 

// Create a variable of type int pointer, and call the assignment operator on it, and assign it the address of a pointer that

// is pointing to an address of the integer above

int* pA = a;
 

// Call delete on variable pA, thus calling the destructor of whatever we are pointing to

// Note: pA still points to whatever it was pointing to, you should also do "pA = nullptr;" 

delete pA;

 

// Create a locally scoped variable called 'a' and assign it the value 5

int a = 5;

 

// Create a variable of type int pointer, and call the assignment operator on it, and assign it the address that is currently

// being used by the variable 'a'

// Note: Pointers point to addresses

int* pA = &a;

 

// Wont work, you only delete objects that have been instantiated with 'new' or the equivalent for malloc.
delete pA;

Edited by d4n1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this