By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,325 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,325 IT Pros & Developers. It's quick & easy.

Deallocating Local Variable used by Malloc

P: n/a
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?

Oct 16 '06 #1
Share this Question
Share on Google+
5 Replies

P: n/a

FireHead wrote:
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

Oct 16 '06 #2

P: n/a
"FireHead" <sa************@htech.health.nsw.gov.auwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 16 '06 #3

P: n/a
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

Oct 16 '06 #4

P: n/a
FireHead wrote:
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.

Oct 16 '06 #5

P: n/a
FireHead wrote:
>
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.

Oct 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.