Sign in to follow this  

new and delete for pointers?

This topic is 4087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When do you use new and delete for pointers in C++? I just finished the section on pointers, but then it starts using new and delete to add or remove space from the heap. Before it wasn't using these commands with pointers. It doesn't seem to specify when and when not to do this. Thanks

Share this post


Link to post
Share on other sites
You use them whenever you need dynamic memory allocation/deallocation. Static memory already manages things for you via a stack, but there are a couple of restriction: sizes of things must be known at compile time and the stack itself has a limit. There are situations when you don't know sizes of things in advance. For example, if there is a file you want to load into memory, you can do one of the following things:


//Static memory
#define MAX_FILE_SIZE (1024*1024)
unsigned char buffer[MAX_FILE_SIZE]; //The static buffer for reading the file
fread(buffer ...); //What happens if the file is bigger than 1MB? Oops!

... Do Something with the buffer ...

return true; //The memory for the buffer will be automatically released here

//Dynamic memory
int fileSize = FileSize(file); //Calculate the file's size first.
unsigned char* buffer = new unsigned char[fileSize]; //Allocate exactly the amount of space we need.
fread(buffer, ...); //No worries here, it won't overflow the buffer.

... Do something with the buffer ...

delete [] buffer; //Release the dynamic memory.
buffer = NULL; //Set the pointer to null, as a precaution.

.. Do something else that doesn't involve the buffer ...

return true; //The memory for the buffer is not automatically released, so that's why we called delete [].




As you can see, in the static memory example we just pulled a max file size number out of out $%^ and hoped that we will never have to deal with files larger than that. There is also a danger of overflowing the stack when you have too many static variables like that.

Dynamic memory allocation is much more flexible than that, it allows specifying sizes at runtime and there is no built-in limit to the size you want to allocate. There is a catch, however: dynamic memory must be carefully managed using pointers and new/delete operators.

Share this post


Link to post
Share on other sites
I see why you would use new and delete, but doesn't the pointer already take up dynamic memory when you use it? Even if you don't request a space on the heap? Thats why you use a pointer. So you can use dynamic memory instead of static memory.

Share this post


Link to post
Share on other sites
I guess what I'm getting at is when you use pointers should you always use new to allocate a space for the memory address or are there times when you just declare the pointer and use it without allocating a space in memory?

Share this post


Link to post
Share on other sites
If you know the size you want the pointer to be, and that size is constant, then sure, define without new and delete. For the file size example, new and delete are needed because the file size can vary.

Share this post


Link to post
Share on other sites
Quote:
I see why you would use new and delete, but doesn't the pointer already take up dynamic memory when you use it? Even if you don't request a space on the heap?

No, not at all. You always need to remember what a pointer actually does, it points somewhere in memory. Initially a pointer doesn't point to anything you can use though, therefore you need to make it point at some memory which you have the permission to modify. You can either point it somewhere on the stack by taking the address of a stack variable, of course this means that if you write something to the pointer's location, then the stack variable will be overwritten. Sometimes you don't have a stack variable you want to waste, or you might not know the exact size you need until runtime. Stack variables size is always known at compile time. So we need to get an address to some memory we can use.

Somewhere in the OS there is a memory manager which have a list of all the free memory, so you need to point to some of that, you don't know where it's though, and you don't know how to tell the manager that the memory isn't free anymore. This is what new is for, when you call new we tell the manager we would like a chunk of memory, it then chooses some of the free memory, marks it as used and send us the address. Since we only have limited free memory, the memory manager needs to reuse the memory, so when we are done with some memory we tell it that we are done by calling delete. Then the memory manager marks that memory as free once more.

For completeness I will mention, that on some systems, where you know you are the only application which runs, you can often explicitely assign pointer values. So you might do, int* a = 0x0400A. This is sometimes used by programmers on these platforms to avoid the overhead new and delete has. You most likely doesn't have access to such a platform though (Gameboy, MS-DOS, etc.), so ignore it, I just thought I'd mention it.

Share this post


Link to post
Share on other sites
When you declare a pointer, it is a null pointer pointing at nothing.
When you use "new", it returns a pointer to the allocated space in the heap, and your pointer will point to something you can use from there on.

you don't have to use the "new" if there is another pointer already, in which case you can do something like

pPointer1 = pPointer2;

and the pointer1 will be pointing to the same thing in the heap as the pointer2.

Share this post


Link to post
Share on other sites
Pointers do also take up some stack memory, but just 4 bytes (enough to point to a location in memory, which is 4 bytes on many compilers/processors).

Sometimes pointeres are helpfull when you need one object to be able to look at another. Say, you've got a Map object (if you haven't covered classes yet, just think of them as a physical object for now) which contains a number of Units. Each Unit may need to know which Map it is part of. You could use a pointer for this, even though at no point does the Unit allocate memory for the Map, neither on the stack nor the heap.

References can do alot of what pointers do, and are safer and easier. Use them once you learn them and when possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by wonjun
When you declare a pointer, it is a null pointer pointing at nothing.


No, it's actually a random pointer pointing to whatever happened to previously be at that point in memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris27
When do you use new and delete for pointers in C++? I just finished the section on pointers, but then it starts using new and delete to add or remove space from the heap. Before it wasn't using these commands with pointers. It doesn't seem to specify when and when not to do this.

Thanks

Since you are learning from a book or tutorial I would blame it for not teaching you about heap and stack memory.

It is quite essential to know the difference and when to use what.
Maybe you should switch for another tutorial or get you a better book, if it fails to educate you about such elementary stuff at this stage I wonder how well it will do at the more advanced stage.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris27
I'm currently using Ivor Horton's Visual C++ 2005. It's sapposed to cover the basics of both ISO/ANSI C++ and CLI/C++.

I am not familiar with that book myself, but from the book description it seems more like a book teaching you how to use MS Visual C++ 2005 rather than teaching you C++.
Having a book covering your IDE/Debugger ect. is very handy, but it might not be the best place to start when learning the language.

Maybe you could try to find an additional book that covers C++ programming, or find suitable tutorials (I would prefer a book).

I am sure here are some members who could give you an advise for a good learning book I myself can't since the one I used was horrible ;).

Share this post


Link to post
Share on other sites
I have another question about memory. I was going through a tutorial on memory and it says that the stack is used for static memory allocation and the heap is used for dynamic memory allocation. It seems all your variables would be in static memory allocation. What exactly would you store in dynamic memory? Would that be used when you are declaring a pointer that doesn't point to anything?

int *pdynamic = NULL;
pdynamic = new int;

What about if a pointer is pointing to something on the stack. Does that take up space on the heap just because you are using a pointer?

int rnumber = 10;
int* pstatic = &rnumber;

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris27
What exactly would you store in dynamic memory? Would that be used when you are declaring a pointer that doesn't point to anything?

int *pdynamic = NULL;
pdynamic = new int;

As mentioned in some of the previous posts, dynamic memory is used when you don't know the size of a particular structure at compile time. For example, the following will not compile:

#include <iostream>

int main() {
int size;
std::cin >> size; // Get the size of the array from the user.
int someArray[size]; // Create the array statically.

return 0;
}


The compiler has no idea what 'size' is at compile time, so it can't allocate the space for you. How can we solve this problem? Well, you could use some preset value for the size of the array, as in deathkrush's post. But as his post also explains, what happens if that preset value is too small? In this particular example, you want the user to be able to specify the array's size, so a preset value isn't going to cut it. The solution is to use a pointer and allocate the memory dynamically:


#include <iostream>

int main() {
int size;
std::cin >> size; // Get the size of the array from the user.
int *pointerToSomeArray = new int[size]; // Create the array dynamically.

return 0;
}



Now the program will compile. But why does this work when the first didn't? The difference is that now, the compiler knows exactly how much space to set aside for you: four bytes for a pointer. The array itself is allocated dynamically at run timel, after the user has typed in the size they want. Once new sets aside a block of memory for the array, it returns the address of this memory block. This gets stored in the pointer, giving you a way to access the array.

Granted, the above is a very trivial example, but I hope it illustrates the idea. For example, you could replace the user entering a size with other things, like a call to some function. One of the previous examples used was for files. In that case, the function 'FileSize' will determine how big the file is. This can't possibly be known at compile time (we most likely have no idea how big the file is when we're writing the program). So we use dynamic memory and learn how big the file is at run time. Again, the compiler knows exactly how much space to set aside: enough space to store an address (four bytes usually).

Quote:
Original post by Chris27
What about if a pointer is pointing to something on the stack. Does that take up space on the heap just because you are using a pointer?

int rnumber = 10;
int* pstatic = &rnumber;

Thanks


In this case, no. Your example makes use of the stack only, so there is no dynamic memory allocation going on. The first line allocates enough space on the stack to store an integer and initializes that memory to binary 10. On most systems (if I remember right) integers take up four bytes. So the compiler sets aside that four bytes on the stack and puts the number 10 in it. The second line creates a pointer, so another four bytes of stack space is taken up by that. A pointer "points" to whatever address happens to be in the memory it was assigned. When a pointer is created, it points to whatever junk was in memory (not very safe). In your particular example, you've initialized the pointer right away. &rnumber returns the address of rnumber (the location of that first group of bytes allocated on the stack). This number is stored in the space assigned to pstatic. So, to summarize: rnumber is placed on the stack and given the value 10. pstatic is also placed on the stack and given the value of rnumbers stack location (ie. it's memory address).

Hope that helps some.

Share this post


Link to post
Share on other sites
Let me fix something...


#include <iostream>

int main() {
int size;
std::cin >> size; // Get the size of the array from the user.
int *pointerToSomeArray = new int[size]; // Create the array dynamically.

// Fill pointerToSomeArray
// Use pointerToSomeArray

delete [] pointerToSomeArray; // don't forget to delete!
// and use [] since it's an array

return 0;
}

Share this post


Link to post
Share on other sites

This topic is 4087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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