Is it possible to "destroy" a local variable ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Horacius ReX

    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 ?

  • vippstar@gmail.com

    #2
    Re: Is it possible to "destroy&q uot; a local variable ?

    On May 30, 1:44 am, Horacius ReX <horacius....@g mail.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.

    Comment

    • Bartc

      #3
      Re: Is it possible to &quot;destroy&q uot; a local variable ?


      "Horacius ReX" <horacius.rex@g mail.comwrote in message
      news:52c1179f-3244-4c93-a230-942a0603aa77@f6 3g2000hsf.googl egroups.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


      Comment

      • pete

        #4
        Re: Is it possible to &quot;destroy&q uot; 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

        Comment

        • robertwessel2@yahoo.com

          #5
          Re: Is it possible to &quot;destroy&q uot; a local variable ?

          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.

          Comment

          • Horacius ReX

            #6
            Re: Is it possible to &quot;destroy&q uot; a local variable ?

            On May 30, 2:51 am, pete <pfil...@mindsp ring.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 ?

            Comment

            • Horacius ReX

              #7
              Re: Is it possible to &quot;destroy&q uot; 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....@g mail.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.

              Comment

              • pete

                #8
                Re: Is it possible to &quot;destroy&q uot; a local variable ?

                Horacius ReX wrote:
                On May 30, 2:51 am, pete <pfil...@mindsp ring.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)sizeo f onion);
                printf("sizeof onion.pi is %u\n", (unsigned)sizeo f onion.pi);
                printf("sizeof onion.array is %u\n", (unsigned)sizeo f onion.array);
                printf("sizeof onion.ret is %u\n", (unsigned)sizeo f onion.ret);
                strcpy(onion.ar ray, 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

                Comment

                • Bartc

                  #9
                  Re: Is it possible to &quot;destroy&q uot; a local variable ?


                  "Horacius ReX" <horacius.rex@g mail.comwrote in message
                  news:7404ab66-e556-41ba-a539-09c3cf244d92@e3 9g2000hsf.googl egroups.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


                  Comment

                  • santosh

                    #10
                    Re: Is it possible to &quot;destroy&q uot; 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.

                    Comment

                    • Keith Thompson

                      #11
                      Re: Is it possible to &quot;destroy&q uot; a local variable ?

                      santosh <santosh.k83@gm ail.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_Keit h) 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"

                      Comment

                      Working...