473,395 Members | 1,641 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,395 software developers and data experts.

is there any probiem if we do malloc and free inside infinite while loop..?

I am observing code is dumping at that point in the load. since we are allocating dynamically memory each time
Feb 11 '10 #1
4 3931
donbock
2,426 Expert 2GB
Nothing wrong with an infinite loop that malloc's some memory, then frees it, then goes back to the top of the loop. If you don't free all the memory then you have a "memory leak" -- that is a problem regardless of whether you have an infinite loop.

A core dump indicates a serious problem. Are you trapping the malloc error return (ie, when it returns NULL)?
Feb 11 '10 #2
i m not trapping... how to trap..?
Feb 11 '10 #3
Banfa
9,065 Expert Mod 8TB
With an if statement, test the return value of malloc.
Feb 11 '10 #4
donbock
2,426 Expert 2GB
@jagadeesha
Expand|Select|Wrap|Line Numbers
  1. foo_t *ptr;
  2. ptr = malloc(sizeof(foo_t));
  3. if (ptr == NULL) {
  4.     printf("malloc failed!\n");
  5.     exit(1);
  6.     }
Replace the printf/exit with whatever you think is an appropriate action to take when malloc fails.
Feb 12 '10 #5

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

Similar topics

13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
10
by: Capstar | last post by:
Hi NG, I'm trying to allocate some resources, in this example a structure, with containing a buffer. When a allocation failes, all previous allocations need to be freed. When I try to compile it...
30
by: Ed | last post by:
Hi, When free() is used to free the memory allocated by malloc(), how does it know the size of the memory to be freed? I guess I'm asking where does the info stored? Thanks Ed
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
1
by: John | last post by:
Hi All, I'm an experienced C# developer that has very little experience with C++. Unfortunately, it fell to me to wrap some unmanaged c++ (itself ported from ADA) to provide managed types that...
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
8
by: Sheldon | last post by:
Hi, I have a problem that and the answer eludes me: I am converting a 2D array to a 1D array within a for loop that looks something like this: ii=0; for (i = 0; i < 300; i++) { for (j =...
6
by: deepak | last post by:
main() { int *a=NULL; int *b; a = (int *) malloc(sizeof(int)); *a=10; b=a;
1
by: sumitmishra | last post by:
HI FRIENDS i have a problem . i am trying to allocate array inside the for loop . Below is my code here int** character has been allocated inside the for loop and then i have used the delete...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
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...
0
tracyyun
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...

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.