• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Memory allocation problems in C

Why do segmentation faults like chunk_alloc and chunk_free come?
0
p_sreekanth
Asked:
p_sreekanth
  • 3
  • 2
1 Solution
 
p_sreekanthAuthor Commented:
operating system is linux with 64 MB RAM  with 2 GB hard disk.
0
 
jjmcdCommented:
When memory is allocated for your program, different artifacts are allocated in different parts of the virtual address space.  Each of these segments of virtual address space is mapped to a specific part of the machine's physical address space.

Each chunk of memory has a limited size.  The machine's hardware detects an attempt to read or write outside the program's allocated memory and generates a segment fault.

From the programmer's perspective, you can view this as one of two possibilities ... either you tried to access past the end of an available piece of memory, or you attempted to access memory with an uninitialized (or improperly initialized) pointer.

Note that in a virtual memory operating system, it really doesn't matter how much physical memory you have, only which chunks of memory your program is entitled to.  Physical memory is only a performance thing.
0
 
amorousdonjuanCommented:
memory allocation is constrained by the
construction of a program.so it really doesnt matter how much RAM you have.

consider my case.i am developing an assembler for a processor which has a 4GB virtual memory.

i dont think there is anybody who can simulate 4 Gb memory.even though theoretically intel chips have 64 terra byte of virtual memory.

my application will just about scratch the surface and simulate maybe upto 10 MB using the standard  instructions in C
 

during start up of a computer many system programs get loaded first and also if u observe many applications like anti virus and nortons utilities which eat up a lot of memory.

Intel offers what is known as paged memory management which maps all of these applications to a definite page frame in virtual memory.

so in this case u start a game from a CD rom it might just not work because all of your memory is used up and game programs take a lot of memory themselves.so an error known as page fault occurs.

so one has to free up the memory before doing anything else.

and hey dont forget that even though available physical space is around 4GB
your PC cabinet does not offer space to hold this much memory.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jjmcdCommented:
In a virtual memory operating system, like Windows 95/98/NT, all programs APPEAR to have a lot of memory.  In the case of Windows, there is 4Gb of address space, of which 2 is reserved for the operating system, PER PROGRAM.  Other programs DON'T MATTER.

What does matter is that your program only asks for permission to use certain parts of that 4Gb address space.  If it attempts to use parts that are not allocated, a segment fault occurs.

And the actual, PHYSICAL memory you have doesn't matter either, at least from a segment fault point of view.  It's purely a performance issue.

In fact, in many cases, it's not bad programming practice to allocate a LOT of memory.  The paging algorithms tend to be more efficient than programmatic workarounds (tho not always), so the memory stinginess that is so important in DOS can often work against you in a virtual memory environment.

And there are real, production, systems for which 4Gb is a problem.  That's why we have 64-bit machines.
0
 
jjmcdCommented:
Oh another common misconception.  A page fault is not actually an error.  In fact, if there were no page faults, no programs would run!
0
 
amorousdonjuanCommented:
when i say page fault is an error i mean to say that the microprocessor identifies that a particular page is missing and takes corrective action to rectify the problem i.e. it must be branching off to the corresponding interrupt service routine.

0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now