• C

Memory allocation problems in C

Why do segmentation faults like chunk_alloc and chunk_free come?
p_sreekanthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.