473,287 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Deallocating Local Variable used by Malloc

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
5 2217

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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alzane | last post by:
I'm new to C++ programming. I have an exercise that I have written code for but getting warnings. Can I get some help? #include "stdafx.h" #include <string.h> #include <stdio.h> #include...
5
by: AMT2K5 | last post by:
If I have the class class IOLabel : public IOField { private: int len; char format; public: IOLabel(int row, int col, int len):IOField(row, col)
8
by: pertheli | last post by:
I am in a situation where only "goto" seems to be the answer for my program logic where I have to retry calling some repeated functions. Can anybody help in the usage of goto and its effect in...
9
by: William L. Bahn | last post by:
I am having a strange problem - and I suspect it is something specific to my compiler - but want to get a read on the Standard C portion of it first. Basic Question: Under the C Standard, under...
15
by: MackS | last post by:
The system I am working on supports a subset of C99, among which "standard-compliant VLAs". I've already learnt that VLAs can't have global scope. My question is whether I can safely declare a...
6
by: student1976 | last post by:
All Beginner/Intermediate level question. I understand that returning ptr to local stack vars is bad. Is returning foo_p_B from fnB() reliable all the time, so that using foo_p_A does not...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
10
by: Horacius ReX | last post by:
Hi, in some C program I need to port to some architecture, I send to a function the parameter char with predefined values. Inside the function, this data is read and something is calculated. But...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.