Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

memory allocations in c++ functions

Status
Not open for further replies.

syedshan

Advanced Member level 1
Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134
hello every one,

If I allocate a memory using malloc or _aligned_malloc() in some function, note that this is internal to the function only.

Hence when I come out of the function will this memory be released automatically or do I have to release them manually.

It is because when I try to release the memory, it gives me some error.

Thanks
 

You have to release the memory manually, otherwise you get a memory leak once the last pointer to the memory goes out of scope.

I would suspect that you may be writing past the end of the allocated space, or may be modifying the pointer to the memory before passing it to free.

Regards, Dan.
 

Two additional comments:

- An automatic memory allocation and release inside a function can be achieved by using local C++ class objects. If it's appropriate for embedded design depends on the effectivity of your compiler tool and available resources.

- I can imagine only a few cases where malloc() and corresponding free() of local memory makes sense. Usually, temporary local variable storage will be made on the stack.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top