understand memory/pointers

Hi guys I'm reading Alex Allains jumping into c++ book and I'm on the pointer/memory section and one thing that is confusing me is I'm not sure how you "can access more memory with pointers" I don't know what he means by this I'll write out the paragraph I'm confused with here if that helps

**The most important function of pointers is to enable you toget more memoryat any given time from the operating system.how do you get that memory from the OS?the OS tells you the address of the memory.You need poinbters to store the memory address.If you need more memory later,you can just ask for more memory and change the value that you are pointing to.Consequently,pointers let us go beyond a fixed amount of data letting us choose at run time how much memory we will need. **

can someone try explain to me this in a bit simplier terms and if it's possible could you show me a piece of code which would "get more memory from the OS with pointers" Thanks I really appreciate it I really want to understand this paragraph as I believe it's important.

Thanks
One of the most important functions of an operating system is to manage memory. There is only so much of it as its a hardware constraint. All operating systems provide various functions in their application programming interfaces that return a pointer to some variable amount of memory requested by a caller. For example, Windows has a function named HeapAlloc() which returns a pointer to a memory block of the requested size. If you want a million bytes of memory for 250,000 ints (for a four byte int) you might do this...

1
2
3
4
5
int* pInts = (int*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1000000);
if(pInts)
{
   pInts[0]=123456789;
}


The above code would set the 1st int (four bytes) of the above memory to 123456789.

When I started out learning C many years ago the author of the book I was studying wrote that learning to use pointers is critical to mastering C, and if you don't learn pointers well you won't be able to write any very good programs in C. At the time I was like you and didn't fully understand why they were I mportant, but I worked hard on them just the same, and I eventually found out why they were important. They are! So take heed and learn them! You'll eventually get it.

Last edited on
closed account (48bpfSEw)
There is a memory management system, which manages the allocated memory.

You get memory (or more memory) by using the "new" keyword in CPP, and release the allocated memory by using the "delete" keyword. Both are commands of the memory management system, which administrates the given memory optimized, that means if there were memory leaks it tries reorganize the allocated memories in that ways that there are greater areas the application can allocate.
Topic archived. No new replies allowed.