What is memory segmentation?

Asked

Viewed 811 times

11

I read about memory segmentation, however, I cannot understand what memory segmentation is and how it influences the functioning of my program.

Illustration

See this example of a program that generates an error Segmentation fault:

int main(void)
{
    int* v = NULL;
    *v = 3;

    return 0;
}

Doubts

  1. What is memory segmentation?
  2. How memory segmentation influences how mine works program?
  3. The Segmentation fault generated by the above program related to memory segmentation or something else?

2 answers

6


Remember the page fault? It’s similar, but the reason is different. Page failure is inherent in virtual memory system. The segmentation failure is a bug in the code (probably).

It occurs when you try to access a memory that is not available for your application. It has not been allocated (reserved) for the running process. It also occurs when access is done on protected pages in the process, usually more when writing.

Your mistake

You cannot write to the address 0 of the memory of your application. And not only the 0, others are also reserved. Actually don’t even read 0. You can’t write in the code part (save special privilege), the code is fixed, it’s static. The same occurs with the static data part (usually placed by literals in your code).

Remembering that the memory address in question is virtual, has nothing to do with physical memory.

What is Memory Segmentation?

Areas of memory where each part is:

  • the binary code running
  • static data
  • static memory, dynamic
  • parts that only the operating system should access
  • parties protected at the request of the application
  • etc..

These are the segments, each with a different configuration in the operating system. These segments usually consist of several pages.

Esquema de segmentos de memória

It can occur whenever you do an unauthorized operation, read, write or run. It’s like in filesystem when trying to access a file without permission for that.

How Memory Segmentation Influences How My Program Works?

To develop it common nothing, but there has every organization of how the process will treat what is in memory. Of course he cannot try to do operations in segments not allowed in the languages that allow this.

The Segmentation fault generated by the above program is related to Memory Segmentation or is something else?

This error occurs by accessing something that is not authorized in that context. This does not occur in languages with managed memory, except for bug in the Runtime or virtual language machine.

Those bugs often occur by problem handling pointers, but there are some situations that may occur.

Worse when the Segmentation fault. The hardware can not prevent all wrong access in memory because it is valid and this becomes a logical error, much more difficult to detect.

  • 1

    Normally on Unix systems I have segments and pages, right? In this case, is it said that a page belongs to a single segment (which in turn contains several pages), or is the opposite? Or has nothing to do with each other?

  • 2

    My understanding is that this is correct, although virtual page and physical page are different things, you can have the same physical page being pointed out by different virtual pages, in different processes and therefore different segments, though only because they’re different processes. Segments are concepts a little more abstract and are much bigger things, so much so that there are few, pages can exist, in the extreme, billions.

2

Let’s go in parts: The problem with this code is that the V variable is initialized as NULL(pointing nowhere), then in the sequence is assigned the value 3 to it, which makes no sense, because the pointer is still not pointing anywhere.

Memory segmentation is a widely used technique in operating systems to protect improper access to memory positions, an alternative method to memory paging. For such part of the memory is removed from the process with the use of registers and if a data wants to be access and is in this removed part occurs a Segmentation Fault. Memory Segmentation ensures better protection for your software against edits of values in memory via Hexing, for example.

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.