Deallocating Local Variable used by Malloc

F

FireHead

Hello All,

Its hard to explain but here it goes:

char &free_malloc(char* object_copy){
Here ------------------------------------------------------------------

object_copy is parametre were the contents of the malloc object in main
driver module is copied.
Which would mean that if I free the object_copy it should not effect
the malloc pointer in the main module.

Here ------------------------------------------------------------------
char *ret_buf ;
char dummy;
char *source;

dummy = 0;
source = object_copy;

if(object_copy !=0 || object !=NULL)
free(object_copy);

/* determine how pointer length is needed */
do{
dummy = *source;
source++;
ret_buf++;
}while(dummy!='\0' || dummy!=0);
//insert here the end of the string
ret_buf++;
ret_buf = dummy;
//clean up all of the contents here //
ret_buf = strcpy( ret_buf,"");

//Does the following code work
ret_buf = &(*object_copy);

ret_buf = strcpy( ret_buf,object_copy);
return ret_buf;
}


int main(int argc,char *argv[]){
char *malloc = malloc(6);
char *buffer = 0;
buffer = free_malloc(malloc);
free(malloc);
printf("Display Buffer = %s\n", buffer);

return 0;
}

Basically what I am trying to do is copy the RAM object contents to the
local stack.
I am trying to accomplish this by using the above function that I would
want to use.
This way I can forget calling malloc/ free combination.

I have no idea how can I copy the contents of mallocated RAM object to
the local variable and return new local variable as a returned object.


Can somebody help?
 
M

mkaras

FireHead said:
Hello All,

Its hard to explain but here it goes:

char &free_malloc(char* object_copy){
Here ------------------------------------------------------------------

object_copy is parametre were the contents of the malloc object in main
driver module is copied.
Which would mean that if I free the object_copy it should not effect
the malloc pointer in the main module.

Here ------------------------------------------------------------------
char *ret_buf ;
char dummy;
char *source;

dummy = 0;
source = object_copy;

if(object_copy !=0 || object !=NULL)
free(object_copy);

/* determine how pointer length is needed */
do{
dummy = *source;
source++;
ret_buf++;
}while(dummy!='\0' || dummy!=0);
//insert here the end of the string
ret_buf++;
ret_buf = dummy;
//clean up all of the contents here //
ret_buf = strcpy( ret_buf,"");

//Does the following code work
ret_buf = &(*object_copy);

ret_buf = strcpy( ret_buf,object_copy);
return ret_buf;
}


int main(int argc,char *argv[]){
char *malloc = malloc(6);
char *buffer = 0;
buffer = free_malloc(malloc);
free(malloc);
printf("Display Buffer = %s\n", buffer);

return 0;
}

Basically what I am trying to do is copy the RAM object contents to the
local stack.
I am trying to accomplish this by using the above function that I would
want to use.
This way I can forget calling malloc/ free combination.

I have no idea how can I copy the contents of mallocated RAM object to
the local variable and return new local variable as a returned object.


Can somebody help?

First off in the subroutine you would not want to free the object
before you copied the data from its memory space. Once malloc'ed memory
is free'd it is not legitimate to keep pointers that reference into
that memory or to try to stash data there.

Based on this I think you should re-think what you are trying to do and
come up with some alternate ideas.

- mkaras
 
K

Keith Thompson

FireHead said:
Hello All,

Its hard to explain but here it goes:

char &free_malloc(char* object_copy){

Is that C++? (C doesn't have references.)

[...]
char *malloc = malloc(6);

Giving a variable the same name as a function may be legal, but it's
rarely a good idea.
 
R

raxitsheth2000

Basically what I am trying to do is copy the RAM object contents to the
local stack.

Basically I have to accept that i didn't understand what you want to do
, but just an assumption.(if its wrong then ignore this reply)

You want to copy the content from mallocated memory to some auto
variable (char array not char *) and then you want to return the char
array (which is auto, not mallocated) to some func.

if this is the case then its bad idea,

f1()
{
char arr[5];
strcpy(arr,"abc");//or some mallocated memory
return arr;<--- array is stacked and hence good compiler should warn
you.
}


I am trying to accomplish this by using the above function that I would
want to use.
This way I can forget calling malloc/ free combination.

Use good compiler, use "All Warning" flag and try to avoid all
warning.
and then run your code with memory leak detector.
I have no idea how can I copy the contents of mallocated RAM object to
the local variable and return new local variable as a returned object.
i think this is NOT GOOD (Specifically "returning local object")
Can somebody help?
U,your comipler and your debugger... :)

--raxit sheth
 
S

santosh

FireHead said:
Hello All,

Its hard to explain but here it goes:

char &free_malloc(char* object_copy){

A conforming C compiler may not compile the above line.
Here ------------------------------------------------------------------

object_copy is parametre were the contents of the malloc object in main
driver module is copied.
Which would mean that if I free the object_copy it should not effect
the malloc pointer in the main module.

Here ------------------------------------------------------------------
char *ret_buf ;
char dummy;
char *source;

dummy = 0;
source = object_copy;

if(object_copy !=0 || object !=NULL)

Why check against both 0 and NULL? Literal 0 in a pointer context is
interpreted as a null pointer, so any one of the two expressions would
do.

[snip code which invokes undefined behaviour]
Basically what I am trying to do is copy the RAM object contents to the
local stack.

Huh? C has no notion of a "local stack". Anyway what do you expect to
gain by doing this? I mean other than wasting memory, causing confusion
and possible triggering undefined behaviour. malloc() and friends exist
for a good reason. Often the local variable storage may not be big
enough. Also dynamically allocated memory persists even after execution
leaves the concerned scope.
This way I can forget calling malloc/ free combination.

You'll still have to free() any memory allocated with malloc().
I have no idea how can I copy the contents of mallocated RAM object to
the local variable and return new local variable as a returned object.

This is not possible in standard C. Local storage is released when
execution goes out of scope. Also returning local variables may cause
problems.
Can somebody help?

All considered, I suggest that you rethink your idea. _WHY_ do you want
to eliminate dynamic memory? If it is because of memory leaks caused by
incorrect use or non-use of free() then I suggest that you try to be
more cautious and systematic in your programming and also look at
options like CBFalconer's nmalloc(), Valgrind, Garbage Collector etc.
Of course, using them will render your code unportable. But if you
really don't like manual memory management, then C may not be the best
langauge for you.
 
O

Old Wolf

FireHead said:
Basically what I am trying to do is copy the RAM object contents to the
local stack.

Iet's ignore for now why you might want to do this...
I am trying to accomplish this by using the above function that I would
want to use.

You can't return arrays from functions. So you are shit out of
luck on that one.
This way I can forget calling malloc/ free combination.

Well there's no need to call malloc/free anyway:

char array[size];
I have no idea how can I copy the contents of mallocated RAM object to
the local variable and return new local variable as a returned object.

You can't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Alternative to Malloc in C 0
C pipe 1
malloc 40
malloc() and implicit cast 49
Params -> local vars? 15
Copy string from 2D array to a 1D array in C 1
Adding adressing of IPv6 to program 1
malloc and maximum size 56

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top