Portable alloca() replacement?

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

    #1

    Portable alloca() replacement?

    Recently I've been spending some time googling around for ideas for an
    alloca() replacement that could function in a strict ANSI C environment.
    As we all know, alloca() is system/arch dependant.

    I'm thinking of trying to implement something like "local allocate",
    lallocat(), for temporary variables that should be automatically freed
    when they go out of scope(function or block).

    For simplicity sake, I'm considering something like this...

    {
    char *mystr = lallocat((char) mystr, (sizeof(mystr) + k));
    }

    void *lallocat(void foo, size_t size){

    void *newfoo[size];

    memcpy(newfoo, foo, size);

    return newfoo;
    }

    As you can see, this code has several obvious problems.
    As soon as the lallocat() function returns, does it return a pointer
    to an array, or a NULL pointer, or undefined. Could be any one of the
    three. size_t may have different values depending on the system. Lots of
    things.

    I cant depend on non-standard concepts such as a stack frame. But I need
    to be able to dynamically allocate memory which is local to the scope of
    the function or block, which when upon leaving the scope should
    automatically be freed for reuse(or possibly returned to the host system.)

    Is there any standard way of doing this?

    Freejack
  • REH

    #2
    Re: Portable alloca() replacement?


    "Freejack" <freejack@nowhe re.net> wrote in message
    news:pan.2005.0 1.18.17.17.04.1 83655@nowhere.n et...[color=blue]
    > Recently I've been spending some time googling around for ideas for an
    > alloca() replacement that could function in a strict ANSI C environment.
    > As we all know, alloca() is system/arch dependant.
    >
    > I'm thinking of trying to implement something like "local allocate",
    > lallocat(), for temporary variables that should be automatically freed
    > when they go out of scope(function or block).
    >
    > For simplicity sake, I'm considering something like this...
    >
    > {
    > char *mystr = lallocat((char) mystr, (sizeof(mystr) + k));
    > }
    >
    > void *lallocat(void foo, size_t size){
    >
    > void *newfoo[size];
    >
    > memcpy(newfoo, foo, size);
    >
    > return newfoo;
    > }
    >
    > As you can see, this code has several obvious problems.
    > As soon as the lallocat() function returns, does it return a pointer
    > to an array, or a NULL pointer, or undefined. Could be any one of the
    > three. size_t may have different values depending on the system. Lots of
    > things.
    >
    > I cant depend on non-standard concepts such as a stack frame. But I need
    > to be able to dynamically allocate memory which is local to the scope of
    > the function or block, which when upon leaving the scope should
    > automatically be freed for reuse(or possibly returned to the host system.)
    >
    > Is there any standard way of doing this?
    >[/color]
    How about a variable length array?



    Comment

    • Andrey Tarasevich

      #3
      Re: Portable alloca() replacement?

      Freejack wrote:[color=blue]
      > Recently I've been spending some time googling around for ideas for an
      > alloca() replacement that could function in a strict ANSI C environment.
      > As we all know, alloca() is system/arch dependant.
      > ...[/color]

      There's no way to implement a portable replacement for 'alloca'
      function. But in C99 you don't need to. C99 has variable length arrays,
      which essentially are core language level equivalents of 'alloca'.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Freejack

        #4
        Re: Portable alloca() replacement?

        On Tue, 18 Jan 2005 17:11:40 -0500, REH wrote:
        [color=blue]
        >
        > "Freejack" <freejack@nowhe re.net> wrote in message
        > news:pan.2005.0 1.18.17.17.04.1 83655@nowhere.n et...[color=green]
        >> Recently I've been spending some time googling around for ideas for an
        >> alloca() replacement that could function in a strict ANSI C environment.
        >> As we all know, alloca() is system/arch dependant.
        >>
        >> Is there any standard way of doing this?
        >>[/color][/color]
        [color=blue]
        > How about a variable length array?[/color]

        It's my understanding that variable length arrays, once allocated, are
        fixed in size. That's not so much of a problem, but how would one go about
        growing the array in a loop? Also, I understand that implementations might
        vary depending on whether it's GNU source or ANSI.

        I suppose I could do something like...

        {
        char *next;
        char str[0];
        while(fudge /= EOF){
        /* Some Code */
        char str[strlen (str) + strlen (next) + 1];
        strcat(str, next);
        }
        }

        But that looks like it might send performance all to piss. And I would
        also be using it for binary data. I'll test it out.

        Although it is a possibility.

        Freejack

        Comment

        • E. Robert Tisdale

          #5
          Re: Portable alloca() replacement?

          Freejack wrote:
          [color=blue]
          > Recently I've been spending some time googling around for ideas for an
          > alloca() replacement that could function in a strict ANSI C environment.[/color]

          Use C99 variable size arrays instead.

          Comment

          • REH

            #6
            Re: Portable alloca() replacement?


            "Freejack" <freejack@nowhe re.net> wrote in message
            news:pan.2005.0 1.18.18.11.38.4 20596@nowhere.n et...[color=blue]
            > It's my understanding that variable length arrays, once allocated, are
            > fixed in size. That's not so much of a problem, but how would one go about
            > growing the array in a loop? Also, I understand that implementations might
            > vary depending on whether it's GNU source or ANSI.
            >[/color]
            Sorry, I missed the part where you wanted it to be able to grow. Anyways, I
            thought you wanted something like the non-standard alloc function? As far
            as I know, that doesn't create "growable" memory either. You can allocate
            more space, but not grow a previous one. That will stay the same until the
            function returns, and it is "destroyed. "



            Comment

            • Andrey Tarasevich

              #7
              Re: Portable alloca() replacement?

              Freejack wrote:[color=blue]
              > ...
              > It's my understanding that variable length arrays, once allocated, are
              > fixed in size.[/color]

              But the same is true for 'alloca'! If you need a resizable array why do
              you consider 'alloca' in the first place?
              [color=blue]
              > That's not so much of a problem, but how would one go about
              > growing the array in a loop?[/color]

              Growing the array in a loop? You didn't mention this in your original
              message. In that case you can forget about 'alloca' and variable length
              arrays. Use "regular" dynamic memory manipulation routines: 'malloc'
              and/or 'realloc'.
              [color=blue]
              > Also, I understand that implementations might
              > vary depending on whether it's GNU source or ANSI.[/color]

              As long as the implementation satisfies the requirements of the language
              specification, why care about how it is implemented?

              --
              Best regards,
              Andrey Tarasevich

              Comment

              • pete

                #8
                Re: Portable alloca() replacement?

                Freejack wrote:
                [color=blue]
                > growing the array in a loop?[/color]

                Maybe it's a job for a linked list instead?

                --
                pete

                Comment

                • Christian Kandeler

                  #9
                  Re: Portable alloca() replacement?

                  Freejack wrote:
                  [color=blue]
                  > void *lallocat(void foo, size_t size){
                  >
                  > void *newfoo[size];
                  >
                  > memcpy(newfoo, foo, size);
                  >
                  > return newfoo;
                  > }
                  >
                  > As you can see, this code has several obvious problems.[/color]

                  You forgot to mention the most obvious one: void is an incomplete type and
                  thus cannot be used as the type of a function parameter.


                  Christian

                  Comment

                  • Villy Kruse

                    #10
                    Re: Portable alloca() replacement?

                    On Tue, 18 Jan 2005 14:42:50 -0800,
                    Andrey Tarasevich <andreytarasevi ch@hotmail.com> wrote:

                    [color=blue]
                    > Freejack wrote:[color=green]
                    >> Recently I've been spending some time googling around for ideas for an
                    >> alloca() replacement that could function in a strict ANSI C environment.
                    >> As we all know, alloca() is system/arch dependant.
                    >> ...[/color]
                    >
                    > There's no way to implement a portable replacement for 'alloca'
                    > function. But in C99 you don't need to. C99 has variable length arrays,
                    > which essentially are core language level equivalents of 'alloca'.
                    >[/color]

                    The sources for gcc used to come with one, which would work on all systems
                    to which gcc was ported. This was required because many systems didn't
                    have a working alloca and gcc itself used it internally. So to botstrap
                    a gcc compiler using the native compiler a working alloca implementation
                    had to be provided. That was, by the way, long before there was any C99
                    standard.

                    Villy

                    Comment

                    Working...