1

I'm reading this book for the second time: "C++ primer plus" and one thing caught my eye: http://imgur.com/K73bnK9

Often memory is represented like a linear block(1), say you allocate 2 new blocks of memory,"a" and "b" in (2) and then delete "a" in (3). The arrow is the pointer to free memory according to the book/tutorials. Now these are my questions:

  • Do you have 2 pointers to the free memory or just one?
  • If you can only have one pointer, and the pointer points to block "a" (which is free again), what if you want to allocate more memory then you have available in "a"?

EDIT: I'm trying to understand how memory allocation really works behind the scenes, so I would like to know into more detail what happens when I type: "int i=0;" or "while(running)".

rene
  • 37,946
  • 78
  • 99
  • 132
Anton D
  • 1,055
  • 1
  • 11
  • 18
  • Well, I'm the kind of person who wants to understand how the things he uses work and lately OS's and memory allocation has caught my attention. – Anton D Aug 14 '13 at 19:08
  • 2
    But you aren't really asking a question about OS and memory allocation. If you want to know something specific then you need to include specific details in the question. As it stands you've got some very hand wavy discussion and a context free image. We've little idea what you are talking about. I'm not sure you've got much more of an idea. – David Heffernan Aug 14 '13 at 19:11
  • 1
    Well, 2 people answered my question which has been helpful + I've provided a small description of the image. English isn't my first language so it's hard to describe exactly what i want to know but it seems that most people understand it. – Anton D Aug 14 '13 at 19:16
  • And both answers are poor. The quality of answer reflects that of the question. Your English is fine. If you improved your question I'm sure it would attract good answers from knowledgeable experts. – David Heffernan Aug 14 '13 at 19:18
  • Ok then, can you point me in a general direction where I can learn more about how the memory is handled? – Anton D Aug 14 '13 at 19:22
  • Not really. How about you ask a specific question with some detail. Asking about "how the memory is handled" is very vague. This is an immense topic. – David Heffernan Aug 14 '13 at 19:24
  • Do you know a good introductory book to OS's, I would like to know what happens when i say for example: "int i = 0;" and more then "You create a new variable called "i" and give it a value of 0" – Anton D Aug 14 '13 at 19:36
  • 1
    Book on OS won't help there. That's nothing to do with the OS. That will be a simple stack reservation. The compiler will make sure the function reserves space in the stack frame for the local var. And nothing at all to do with heap allocation which is what you discuss in the question. I think you need to work out what your question is. – David Heffernan Aug 14 '13 at 19:42
  • You might take a look at this if you're interested in how memory allocators work: http://g.oswego.edu/dl/html/malloc.html – Retired Ninja Aug 14 '13 at 19:56
  • Ok here's the deal: i'm a 17 year old guy who has thaught himself programming in java and i'm now making the switch to c++. This means that i've never had a programming course before so i'm trying to understand some fundamentals. Either help with that or please stop acting like i'm a big fool, i just want to be a better programmer. – Anton D Aug 14 '13 at 19:57
  • 1
    I am helping. I don't think you are a fool. I just think you need to work out what your question is. The simple fact is that you've asked a poor question. Nothing wrong with that. But if you want help, you'll need to edit it to make it better. You need to add more details, give some background. Then you'll get good answers I am sure. – David Heffernan Aug 14 '13 at 20:04
  • 1
    *" I would like to know what happens when i say for example: "int i = 0;""* -- Ask that then. That's a simple to understand question. The questions you ask in your post are worded strangely and I have no idea what they mean. *"Do you have 2 pointers to the free memory or just one?"* -- Who? In what situation? -- *"If you can only have one pointer..."* -- Who said you can only have one pointer? – Benjamin Lindley Aug 14 '13 at 20:07
  • @DavidHeffernan and Benjamin: I've edited the question like you asked. – Anton D Aug 15 '13 at 11:57
  • The edit doesn't really help. It now adds two extra questions, making three in total, all unrelated, asked in equally vague fashion. I'm wondering. Do you know the difference between heap and stack memory? I suspect not judging from the questions you ask. That would be a good concept to get on top of. – David Heffernan Aug 15 '13 at 12:08
  • I have a basic understanding of those topics, but i will search more about it. This is turning into a discussion about bad questions because apparently my questions are not clear enough, so let's close this question as unclear. – Anton D Aug 15 '13 at 12:13
  • Have a look at this thread: http://stackoverflow.com/q/79923/1025391 - there are some awesome posts that do address your question. – moooeeeep Nov 08 '13 at 23:36

2 Answers2

-1

There are no pointers to free memory. Ultimately this is handled, and controlled by, the operating system. It could have already allocated the memory used by a to another process at that point.

You can have as many pointers as you want (well, as many as you have memory for). And they can point to anything, whether they are pointing to used memory or not.

If you try to allocate more memory than the OS will give you, new will fail, resulting in an exception (std::bad_alloc).

MSalters
  • 159,923
  • 8
  • 140
  • 320
Shade
  • 765
  • 3
  • 16
  • So it is not like every program gets a certain block of memory and they can request more? – Anton D Aug 14 '13 at 19:04
  • 1
    Who says it is controlled by the OS? What about the runtime's allocator? What about virtual memory? The OS cannot allocate a block of virtual memory in your process to another process. – David Heffernan Aug 14 '13 at 19:04
  • 2
    About your last sentence, if `new` fails to allocate memory it throws a `std::bad_alloc` exception. – Blastfurnace Aug 14 '13 at 19:07
  • 1
    @DavidHeffernan I guess I shouldn't say OS, it would typically be the resource manager, ie. kernel, or a sandbox / VM. For virtual memory... I think thats out of the scope of the question. Look at the title of the book. – Shade Aug 14 '13 at 19:12
  • @Anton, Memory is a lot more dynamic that that. If you look at the system monitor on your system, you see processes with varying amounts of memory, and can watch them change even more. – Shade Aug 14 '13 at 19:13
  • No it would not be kernel or sandbox or vm or anything. Memory allocation is handled by the C++ runtime. Or do you think that a call to new is implemented by a syscall? – David Heffernan Aug 14 '13 at 19:16
  • Something has to control, and keep track of, all the memory in the system. (or if in a VM, the virtual system). Im pretty sure thats the kernel, and that the program is given memory by the system when the program calls alloc(), malloc(), etc., or it uses new, or any other implementation in any other language. – Shade Aug 14 '13 at 19:32
  • 1
    Typically the C/C++ runtime obtains "large" blocks of memory as needed from the OS, and then divvies them up & provides them to the user program as needed. When the user program frees/deletes the memory, the runtime normally hangs on to the memory, perhaps coalescing smaller chunks back into larger chunks, but generally never giving it back to the OS. The OS gets it back when the program exits, at which point any leaked memory is reclaimed by the system as well as all of the unallocated memory that the runtime is still holding on to. – phonetagger Aug 14 '13 at 19:43
  • @Shade Read up on sub allocators. Yes indeed the OS manages the memory at the lowest level. But above that is the C++ runtime which has its own abstraction layer, built on top of the OS virtual memory services. – David Heffernan Aug 14 '13 at 19:50
  • Fixed some trivial issues – MSalters Aug 15 '13 at 07:28
-1

Dynamic memory allocation works a bit differently than you presented it in your question. If you allocate two blocks of memory separately, they aren't neccessarily next to each other. If you allocate them with the same new statement (such as as part of a struct or array) then later delete only one of them, then they would be adjacent, but that is a very odd thing to do.

Look at the syntax of new:

int* a = new int;

The new operator actually returns a pointer to the allocated memory, and stores it in the pointer a. If you call it twice, you get two different pointers to two different blocks of memory.

If you're allocating with an array, then you get one pointer to the first element of the array. However, calling delete doesn't get rid of the pointer, it gets rid of the thing that it points to. So if you were to delete the first element of a (not the entire array) then attempting to access the memory at a would produce undefined behavior, but accessing a[1] would be just fine.

IanPudney
  • 5,531
  • 1
  • 20
  • 37
  • 2
    You can't allocate an array of objects with a single `new[]` and then `delete[]` only one. – Blastfurnace Aug 14 '13 at 19:11
  • @quinxorin Depends. Memory allocation from the caller is not like that. But the question sounds more like a discussion of implementing a sub allocator. In which case there is a large block of linear memory, with sub blocks. And linked lists to free blocks. And so on. – David Heffernan Aug 14 '13 at 19:14
  • @Blastfurnace yes you can; if you allocate an array of `int`s, you cast the array to `int*` and call delete on that, only the first element will be deleted. – IanPudney Aug 14 '13 at 19:16
  • 2
    Sorry, but that is incorrect. You can't `delete` a __portion__ of a memory block obtained from `new`. Same thing with `malloc/free`. – Blastfurnace Aug 14 '13 at 19:19
  • @quin Absolutely not!! That's just utter nonsense. – David Heffernan Aug 14 '13 at 19:19
  • It just worked with minGW-G++ on Windows 7. I allocated an array of four `int`s and deleted the first one. The first one became gibberish while the others retained their values. But I do admit, it's pretty absurd. – IanPudney Aug 14 '13 at 19:25
  • I can't tell you what MinGW was doing in your case but that's invalid C++. Trying that in any other environment is asking for some type of heap corruption fault. Did you try passing the `*int+1` pointer to `delete` to release the rest of the memory? – Blastfurnace Aug 14 '13 at 19:28
  • 3
    I'm surprised that someone who is aware of the idea of undefined behavior doesn't realize that what you did there (calling delete on a single element of an array) was just that, undefined behavior. – Benjamin Lindley Aug 14 '13 at 19:53