Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
how to allocate memory for pointer object in one function and how to deallocate in another function in c...

can you help me.
Posted

Use malloc() and free()


char* pchar = malloc(_MAX_PATH);

// do some stuff with pchar, including error checking to see 
// if the memory has been allocated.

free(pchar);
 
Share this answer
 
Comments
Albert Holguin 5-Jul-11 10:22am    
that easy, 5
Joan M 5-Jul-11 13:08pm    
The OP is asking on how to free memory in a different function... so meanwhile your answer is correct to free memory inside the same function it is important to pass the parameters as needed... see my answer for more details.
It can be done, but doing it is a bad idea. Such design of code is error-prone and not supportable. Also, there are no situations when it helps.

[EDIT]

I recommend limiting allocation and deallocation of the same variable by the same stack frame. In between, the pointers can be passes to any other function, but deallocation should be done only when all the executions returns where it was allocated.

Another layout for the allocating and deallocating code should be this: allocation in a constructor and deallocation in a destructor. As this is C, I did not mention it in first place, but even in C one can introduce regular functions logically playing the role of constructor and destructor.

(In a sense, even fully-fledged OOP is quite possible in plain C and even in Assembly Language. On way of doing it is to introduce explicit notion of vtable and pass "this" explicitly as a regular by-pointer function parameter. Automatic covariance and contravariance would be pretty hard to achieve though.:-))

Please see my argument with Espen below.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 5-Jul-11 14:35pm    
erhm, I did not vote 1 on this, but it's pretty hard to do anything without doing this. MISRA kind of hard, and you need a pretty good reason for going down that path :)
Sergey Alexandrovich Kryukov 5-Jul-11 21:59pm    
Than you for this note, but are you sure we're talking about same thing? I'm not talking about passing a pointer to other functions, I'm talking about the stack frame for allocation and deallocation. If you allocate a variable in the beginning, pass it as much as needed to other functions and return to the same stack frame and deallocate in it.

Also, I did not mean "special" functions, namely constructor and destructor. Those are different functions; and it is a must to allocate in constructor and deallocate in destructor.

I did not mention constructor/destructor, because this is C. In fact, even is C there could be "Init" and "Done" with allocation and deallocation. So yes, my advice not quite correct.

Anyway, both approaches require symmetric allocation and deallocation operations.

--SA
Sergey Alexandrovich Kryukov 6-Jul-11 15:59pm    
I modified my answer accordingly, please see.
Still disagree? Tell me about it.
--SA
Espen Harlinn 7-Jul-11 9:03am    
No, not at all Sergey :) It's also fairly common to create a structure similar to Anders' TComponent framework, that allows you to keep track of your data - ensuring that everything gets cleaned up in a "Done" function. As you say, while object oriented languages adds the syntactic sugar to ease object oriented development - there is nothing wrong in applying the principles in c too.

Making the mental leap from the initial reply to this - was perhaps not quite obvious. My 5
Sergey Alexandrovich Kryukov 7-Jul-11 12:40pm    
Thank you very much, Espen. I realized how uncertain and incorrect was my first post of this solution. Thank you for helping me to make in more clear.
--SA
As RHuros states you should use malloc and free.

If I'm right and you want to do that across different functions you should take a look at this code:

C#
#include "stdafx.h"
#include "stdlib.h"
#include "conio.h"

void reserve(void* &pchar)
{
    pchar = malloc(10000);
    printf("malloc ==> ");
}

void remove(void* &pchar)
{
    free(pchar);
    printf("free.\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
    void* pchar = NULL;

    while (!_kbhit())
    {
        reserve(pchar);
        remove(pchar);
    }
    return 0;
}


NOTE:
Keep in mind that this is not a good design... you should not do that as it would be hard to maintain in the future. It can be done and it will work anywhere, but it should not be done to avoid possible problems.


Hope this helps... :thumbsup:
 
Share this answer
 
v2
Comments
@BangIndia 6-Jul-11 1:02am    
i want to use that in the library
Joan M 6-Jul-11 2:00am    
so? feel free to use it where you need it... in the way you've asked it it seems you want to go to the library (that place where you can borrow books) and use the code there... COME ON! do you think that anyone here will understand this? "i want to use that in the library"... in your profile you state you are from the States so you should be able to speak English in a much better way than me (from Spain) and you should try to work it a little bit more when asking... I'm sorry, but I do not understand what you are asking.
Albert Holguin 6-Jul-11 16:43pm    
if you're talking about a .lib or dll, you shouldn't do that...
Joan M 7-Jul-11 2:07am    
You are right Albert, of course this is not a good design... who knows... it could be possible to find a situation in which it could be necessary... Anyway, a lot of people has adviced the OP in several messages to not do that but no one had shown the OP how it can be done... so after all this is an answer that shows how to do what the OP has asked... (I'll modify it just to point out that it should not be done).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900