Distinction between paging and segmentation? In my operating systems textbook, there is a paragraph which states:



As for the contents of each PTE, we have a number of different bits
in there worth understanding at some level. A valid bit is common to
indicate whether the particular translation is valid; for example, when
a program starts running, it will have code and heap at one end of its
address space, and the stack at the other. All the unused space in-between
will be marked invalid, and if the process tries to access such memory, it
will generate a trap to the OS which will likely terminate the process.
Thus, the valid bit is crucial for supporting a sparse address space; by
simply marking all the unused pages in the address space invalid, we
remove the need to allocate physical frames for those pages and thus save
a great deal of memory.



However, the bold sentences confuse me. I thought the whole point of paging was that you do away with the contiguous regions of logical code segments like stacks and heaps? With paging, couldn't the stack and heap be anywhere and non-contiguous? What guarantee do you have that the code, heap and stack will be separated to the top and bottom of the address space,as they say?
There are two different address spaces, let's call them logical and physical.  The addresses a programmer thinks about are the logical addresses, the page table translates logical addresses to physical addresses.

As you point out, paging allows you to sparsely map the logical address space to the physical address space.

But the bold sentences in your quote are talking about the sparsity of the logical address space.  The quote is arguing that the valid bit is necessary for the sparsity of the logical address space.