PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Is it possible to "destroy" a local variable ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is it possible to "destroy" a local variable ?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/05/2008, 23h44   #1
Horacius ReX
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 ?

  Réponse avec citation
Vieux 29/05/2008, 23h51   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

On May 30, 1:44 am, Horacius ReX <horacius....@gmail.com> 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]. 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.
  Réponse avec citation
Vieux 30/05/2008, 00h50   #3
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?


"Horacius ReX" <horacius.rex@gmail.com> wrote in message
news:52c1179f-3244-4c93-a230-942a0603aa77@f63g2000hsf.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


  Réponse avec citation
Vieux 30/05/2008, 01h51   #4
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

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
  Réponse avec citation
Vieux 30/05/2008, 05h05   #5
robertwessel2@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

On May 29, 5:51pm, 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.
  Réponse avec citation
Vieux 30/05/2008, 08h18   #6
Horacius ReX
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

On May 30, 2:51 am, pete <pfil...@mindspring.com> wrote:
> 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 ?
  Réponse avec citation
Vieux 30/05/2008, 08h20   #7
Horacius ReX
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

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.com> 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]. 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.

  Réponse avec citation
Vieux 30/05/2008, 08h51   #8
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

Horacius ReX wrote:
> On May 30, 2:51 am, pete <pfil...@mindspring.com> wrote:
>> 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
  Réponse avec citation
Vieux 30/05/2008, 10h28   #9
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?


"Horacius ReX" <horacius.rex@gmail.com> wrote in message
news:7404ab66-e556-41ba-a539-09c3cf244d92@e39g2000hsf.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


  Réponse avec citation
Vieux 30/05/2008, 19h24   #10
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

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.

  Réponse avec citation
Vieux 30/05/2008, 23h40   #11
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to "destroy" a local variable ?

santosh <santosh.k83@gmail.com> writes:
> 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) kst-u@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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23055 seconds with 19 queries