473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

malloc inside while loop

Hi All,

I tried to execute follwoing program, which got killed by the OS.

#include<stdlib.h>

main()
{
while(1) { char *c=(char*)malloc(10000); }
}

I got one message stating "Killed" in the command prompt.
Pls, any one can explain me why it got killed by the OS.
Note:
OS: Linux
Compiler: g++
Jul 22 '09 #1
9 8759
JosAH
11,448 Expert 8TB
You simply allocated too much memory and your OS didn't like it,

kind regards,

Jos
Jul 22 '09 #2
Thanks Jos for ur reply.
I need the details about how the memory allocated and in what condition the OS will kill the process.
Jul 22 '09 #3
gpraghuram
1,275 Expert 1GB
Hi,
Usually for every process OS allocates some memory and when u start grabbing mor and more memory then at one point the virtual memory available with OS will gtes exhausted and finally the process will get killed.
Raghu
Jul 23 '09 #4
donbock
2,426 Expert 2GB
What is the purpose of this program? All it does it is repeatedly allocate memory without ever using it or freeing it. Nothing is accomplished.

Let me guess .. this is a test question from your programming course.
I would prefer for you to tell us what you think this program does.
Then our responses can correct any errors and fill in any gaps.
Jul 23 '09 #5
It is a test program to understand the memory managemet in Linux machine.
Can any give me a clear picture about the memory management in Linux.
Something like How memory is allocated & when/how it is deallocated.
What happens when the program tries to allocate more as the program does.
I wanted to know the detiail picture of the memory managed by the OS.

Thanks in advance.
Jul 23 '09 #6
ashitpro
542 Expert 512MB
By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. This is a really bad bug. In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer. In case Linux is employed under circumstances where it would be less desirable to suddenly lose some randomly picked processes, and moreover the kernel version is sufficiently recent, one can switch off this overcommitting behavior using a command like

# echo 2 > /proc/sys/vm/overcommit_memory

(Taken from malloc man page)...

This is the link for more info on OOM killer....
http://linux-mm.org/OOM_Killer
Jul 23 '09 #7
JosAH
11,448 Expert 8TB
@ashitpro
I didn't know that; I experienced that bug on an old AIX version years ago, but I can't reproduce it on my Linux box. I'll check it out ...

kind regards,

Jos
Jul 23 '09 #8
donbock
2,426 Expert 2GB
Follow link to Why Linux has an "OOM killer" and ... for an easy-to-follow presentation of the design tradeoffs behind the optimistic memory allocation strategy in Linux. I don't agree with everything in the post, but it is a good nontechnical introduction to the topic.

Saravanan82: please lay out for us your current understanding of memory allocation in Linux. That way nobody will waste time explaining things you already know.
Jul 23 '09 #9
First i would like to thank you for the reply.

I dont know how the memory allocated by the OS, but have some idea about the process memory structure in the main memory.
That's the reason why i wanted to understand the memory allocated by the OS.
In one of my project we decided to use the wrapper for malloc, in order to avoid system calls coz in each malloc call it hits the OS and gets the memory for us(We need to overcome that process by allocating a bulk and manage it with our code).
Jul 23 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: John Eskie | last post by:
Lately I've seen alot of C and C++ code (not my own) which doesn't do any checking if memory obtained by new or malloc is valid or if they return NULL pointers. Why does most people not care about...
6
by: mike79 | last post by:
Hey all, Im use to using malloc() with a one-dimensional array. But I have found the need to use a 2D array, and would like to confirm whether I am allocating memory correctly. As I can...
116
by: Kevin Torr | last post by:
http://www.yep-mm.com/res/soCrypt.c I have 2 malloc's in my program, and when I write the contents of them to the screen or to a file, there aren addition 4 characters. As far as I can tell,...
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
7
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
9
by: questions? | last post by:
if I use malloc() in a function to allocate a space for my array of structures. I didn't free() them anywhere in the program. I found that I can still use that space after I come back to...
6
by: Jack | last post by:
In the following code: char *p; while(1){ p = malloc(10*sizeof(*p)); //Do something with p
9
by: fool | last post by:
Dear group, if I want to allocate a string with a help of malloc then, shall I use the following in a loop? char *s; s = malloc(strlen(s)+1); Any pointer reference is help full. I went through...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.