473,554 Members | 3,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc inside while loop

4 New Member
Hi All,

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

#include<stdlib .h>

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

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

kind regards,

Jul 22 '09 #2
4 New Member
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
1,275 Recognized Expert Top Contributor
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.
Jul 23 '09 #4
2,426 Recognized Expert Top Contributor
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
4 New Member
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
542 Recognized Expert Contributor
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_memo ry

(Taken from malloc man page)...

This is the link for more info on OOM killer....
Jul 23 '09 #7
11,448 Recognized Expert MVP
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,

Jul 23 '09 #8
2,426 Recognized Expert Top Contributor
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
4 New Member
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

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 doing this kind of error checking? When I used to learn the language I was told always to check "if (p != NULL)" and return a error otherwise. I...
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 gather, unlike a 1D array, you cannot allocate memory to a 2D array with only 1 line of code. Just say i wish to have an array of strings, I wish to...
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, both the code to register the malloc and to write information into the malloc is solid. Why then ismy program returning an additional 4 characters? ...
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 follow? Second, have I correctly passed the structure's pointer to the functions in this program?
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 pointers, which seems to go ok. I want to then take it one step further and dynamically create the array (pointers to pointers). I try to print out...
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 main(). Am I supposed to use that space or it is temporary and will be removed soon afterwards? Thanks
by: Jack | last post by:
In the following code: char *p; while(1){ p = malloc(10*sizeof(*p)); //Do something with p
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 the K & R book's chapter 5. But I am not able to get the answer.
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 the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was...
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. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.