473,396 Members | 1,775 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,396 software developers and data experts.

Is it possible to "destroy" a local variable ?

Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?

Jun 27 '08 #1
10 7428
On May 30, 1:44 am, Horacius ReX <horacius....@gmail.comwrote:
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?
A char[50000] needs not to exist in a C89 implementation.
C99 guarantees that objects can be at least 65536 bytes in size.
A way to "delete" such object is:

{ char array[N]; /* do stuff with array */ }
/* array no longer exists */
My suggestion is:
Split your function to two functions. The first should do the
calculations you speak of, such as:

{ char array[50000]; f1(array); }
/* f2(...) */

This may, or may not work. Standard C doesn't guarantee anything.
Jun 27 '08 #2

"Horacius ReX" <ho**********@gmail.comwrote in message
news:52**********************************@f63g2000 hsf.googlegroups.com...
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?
Where does this 50KB exist: as initialised (static) data, as a local
variable as you suggest (that's a big variable), or on the heap?

So you call a function with this 50KB array, then you no longer need that
array and would like to use it for something else?

If it's initialised data, probably you will be able to overwrite with
something new, but you can't easily add it to the memory pool of malloc; you
would have to make use of the memory explicitly, like setting an array
pointer to the start of it (or creating special versions of malloc/free to
use that block).

The same goes for local variable (or 'stack') data - if you stay in that
function. The memory will be recovered if you return from the function. But
even then 'stack' and 'heap' (malloc) memory may not be shared so it could
just exist as spare stack memory and not extend the heap.

Best solution would be to use malloc-ed memory for this 50KB block, provided
it can be initialised without the initialisation code/data itself taking
50KB.

--
Bartc
Jun 27 '08 #3
Horacius ReX wrote:
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000].
That's what a union is for.

--
pete
Jun 27 '08 #4
On May 29, 5:51*pm, vipps...@gmail.com wrote:
{ char array[N]; /* do stuff with array */ }
/* array no longer exists */

Since the OP was concerned about actual memory allocated for the
local, we should remember that scoped variables are allocated in the
enclosing routines activation record, although often as an (effective)
union with other parallel scoped definitions. So on most
implementations, this is unlikely to accomplish what the OP wants.
Jun 27 '08 #5
On May 30, 2:51 am, pete <pfil...@mindspring.comwrote:
Horacius ReX wrote:
Hi,
in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000].

That's what a union is for.

--
pete
could you please tell me what you mean ?
Jun 27 '08 #6
my data comes at the beginning from another function:

function do_smt(char thing[50000]){

... do things here with "thing" and afterwards eliminate it

}

and this data was not created with malloc
vipps...@gmail.com wrote:
On May 30, 1:44 am, Horacius ReX <horacius....@gmail.comwrote:
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?
A char[50000] needs not to exist in a C89 implementation.
C99 guarantees that objects can be at least 65536 bytes in size.
A way to "delete" such object is:

{ char array[N]; /* do stuff with array */ }
/* array no longer exists */
My suggestion is:
Split your function to two functions. The first should do the
calculations you speak of, such as:

{ char array[50000]; f1(array); }
/* f2(...) */

This may, or may not work. Standard C doesn't guarantee anything.
Jun 27 '08 #7
Horacius ReX wrote:
On May 30, 2:51 am, pete <pfil...@mindspring.comwrote:
>Horacius ReX wrote:
>>Hi,
in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values. Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000].
That's what a union is for.
could you please tell me what you mean ?
A union declaration looks a lot like a struct declaration,
but all of a union's members overlap in memory.
The lowest addressable byte of each member of a union
is the same byte.
A union is as large as its largest member,
plus whatever additional padding bytes the compiler might want.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

#define STRING "\nhello world"

union csa {
double pi;
char array[sizeof STRING];
int ret;
};

void func(union csa *onion);

int main(void)
{
union csa onion;

printf("sizeof onion is %u\n", (unsigned)sizeof onion);
printf("sizeof onion.pi is %u\n", (unsigned)sizeof onion.pi);
printf("sizeof onion.array is %u\n", (unsigned)sizeof onion.array);
printf("sizeof onion.ret is %u\n", (unsigned)sizeof onion.ret);
strcpy(onion.array, STRING);
func(&onion);
printf("\nonion.ret is %d\n", onion.ret);
return onion.ret;
}

void func(union csa *onion)
{
puts(onion -array);
onion -ret = 0;
}

/* END new.c */
--
pete
Jun 27 '08 #8

"Horacius ReX" <ho**********@gmail.comwrote in message
news:74**********************************@e39g2000 hsf.googlegroups.com...
my data comes at the beginning from another function:

function do_smt(char thing[50000]){

.. do things here with "thing" and afterwards eliminate it

}

and this data was not created with malloc
Ok so you're already in the function that uses the 50K array?

As written, I think that 'thing' will be a pointer to an array. So you can
now use it for any purpose you like:

int *p;
p=thing; /* p now points to maybe 12500 or 25000 ints */

Assuming that 'thing' points to normal read/write memory. So whatever
allocations you /would/ have used malloc for, you can manually allocate and
manage from 'thing'. Remembering that on exit from this function, they may
no longer exist.

What you can't do is add the 'thing' memory to the memory used by malloc. Or
free it, since you say it didn't come from malloc.

--
Bartc
Jun 27 '08 #9
Horacius ReX wrote:
Hi,

in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values.
How is this array allocated? How and when it can be deallocated would
depend on this. Is it an auto object? Is it a file scope or static
object? Is it got from malloc?
Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?
If it has been allocated via malloc then you could obviously deallocate
it via free. If it is an auto object it will automatically be
deallocated when it's scope is exited, which is the enclosing block.
You cannot free it yourself though you could simply re-use it. If it is
a file scope or static qualified object then it will persist till the
program terminates and you have no portable way of deallocating the
associated memory, though again, you can just re-use it if you want.

If this does not clear up your problem then please provide more details
on the exact nature of the array.

Jun 27 '08 #10
santosh <sa*********@gmail.comwrites:
Horacius ReX wrote:
>in some C program I need to port to some architecture, I send to a
function the parameter char[50000] with predefined values.

How is this array allocated? How and when it can be deallocated would
depend on this. Is it an auto object? Is it a file scope or static
object? Is it got from malloc?
>Inside the
function, this data is read and something is calculated. But for some
reasons that I can not explain here (too long) the memory is really
small and I would need to use the space used by this char[50000]. Then
I wonder if it can be deleted or destroyed in some way. Afterwards I
need to use malloc and free and I think I would have more memory if I
could "delete" this. Any hint ?

If it has been allocated via malloc then you could obviously deallocate
it via free. If it is an auto object it will automatically be
deallocated when it's scope is exited, which is the enclosing block.
You cannot free it yourself though you could simply re-use it. If it is
a file scope or static qualified object then it will persist till the
program terminates and you have no portable way of deallocating the
associated memory, though again, you can just re-use it if you want.
Except that, in many implementations, block-scope auto objects are not
physically deallocated until the enclosing function terminates.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: BW | last post by:
Please ignore the rest of the code, except for the highlighted part (or the line 'ent1=Entry(topf, width=25)' to line 'ent1.insert(INSERT, wrong, if you cannot see the color). You can copy this into...
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
7
by: Ottar | last post by:
I've made a program sorting incomming mail in public folder. The function runs every minute by using the form.timer event. In Access XP it runs for weeks, no problem. Access 2003 runs the same...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.