[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Funtions to automatic allocate and deallocate memory.

Posted on 2006-04-17
14
Medium Priority
?
362 Views
Last Modified: 2010-04-15
Functions to allocate and deallocate automatic memory?

I have a group of functions that allocate and deallocate memory on demand. But fails in some cases to deallocate.
Some group of functions to make this in glibc or similar?
0
Comment
Question by:ZOOMPLUS
  • 2
  • 2
  • 2
  • +6
12 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16467651
Put some examples.
0
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16467846
Hi,
Maybe you reassign some pointer somewhere in between allocation and deallocation?
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 16468064
Hi,

I do not think there are any inbuilt automatic functions to auto allocate and deallocate in pure C.

C++ however provides you constructors and destructors which get called when you create and destroy objects.

your malloc and free() functions could be embedded in these constructors and destructors to ensure automatic allocation and deallocation.

Regards,
Siddhesh
0
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
LVL 85

Expert Comment

by:ozo
ID: 16468088
0
 

Author Comment

by:ZOOMPLUS
ID: 16468343
I have some problems sometimes to realloc and free.
glibc. Linux.gcc

The pointer is the same, there is no problem here.
I do not know which is the problem.
0
 
LVL 2

Expert Comment

by:rahul_r_jadhav
ID: 16468350
can you post the code??
0
 
LVL 22

Expert Comment

by:grg99
ID: 16470231
Why do you think it's not deallocating?  

Many memory libraries keep a few memory blocks allocated at all times to handle the next allocation request.

0
 

Author Comment

by:ZOOMPLUS
ID: 16471696
glibc said fail, something like.

realloc next pointer fail, or free fail or something like this
0
 
LVL 22

Expert Comment

by:grg99
ID: 16472038
You're probably writing past the end of a pointer.  Be sure all your array indices are in range, and not off by one.

Or maybe you're free()ing a pointer twice.

 
0
 
LVL 27

Expert Comment

by:Nopius
ID: 16505546
Use realloc and remember, the pointer may be changed itself (it changes when data boundary grows/shrinks 4K*N size):
use:
p=realloc(p, new_size); // instead of //  realloc(p, new_size);

You don't need to use free(p) then realloc(p..), just realloc() as many times as you want
0
 
LVL 5

Accepted Solution

by:
Dragon_Krome earned 500 total points
ID: 16560137
Try running your program with valgrind or a memory allocation debugging tool (there are more of those, dmalloc is one for example).
They might help you catch the bug, because there's surely one ;)
0
 
LVL 5

Expert Comment

by:Dragon_Krome
ID: 16560140
also, you might try to compile with -fbounds-check if your compiler supports it
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question