Is it possible to "destroy" a local variable ?

H

Horacius ReX

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 ?
 
V

vippstar

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.
 
B

Bartc

Horacius ReX said:
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.
 
R

robertwessel2

{ 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.
 
H

Horacius ReX

Horacius said:
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 ?
 
H

Horacius ReX

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


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.
 
B

Bartc

Horacius ReX said:
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.
 
S

santosh

Horacius said:
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.
 
K

Keith Thompson

santosh said:
Horacius said:
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.
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top