473,395 Members | 1,556 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,395 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 2225

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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.