Can I allocate an array on the stack?

Discussion in 'C Programming' started by luser-ex-troll, Mar 10, 2009.

  1. Is there a way to replace the heap allocation
    with stack allocation in the following function?

    void Oroll() {
    int i,j,n;
    Object *a;
    stackis2(integer,integer,roll);
    j = pop.u.i;
    n = pop.u.i;
    j %= n; //eliminate bogus loops
    (void)((a=calloc((size_t)n,sizeof *a))
    ||error("VMerror in roll"));
    for(i=0;i<n;i++) a = pop;
    if(j>0) {
    for(i=j-1; i>=0; i--) push(a);
    for(i=n-1; i>=j; i--) push(a);
    } else { //j<0
    for(i=n-1+j; i>=0; i--) push(a);
    for(i=n-1; i>=n+j; i--) push(a);
    }
    free(a);
    }

    I have a vague memory of a function called
    alloc() that would do this, but gcc doesn't
    have it.
    From something posted recently I got the
    idea to try:
    Object a[n];
    But, of course, n doesn't contain the argument
    yet during the declaration section. (I did try it:
    Segfault).
    Can I make a bogus function call to load values
    on the stack (being careful not to call another
    function while expecting them to be there)?

    tia
    lxt

    ps.
    great group. excellent people.
    hope I'm not making enemies.
    luser-ex-troll, Mar 10, 2009
    #1
    1. Advertising

  2. luser-ex-troll <> writes:

    > Is there a way to replace the heap allocation
    > with stack allocation in the following function?
    >
    > void Oroll() {
    > int i,j,n;
    > Object *a;
    > stackis2(integer,integer,roll);
    > j = pop.u.i;
    > n = pop.u.i;
    > j %= n; //eliminate bogus loops
    > (void)((a=calloc((size_t)n,sizeof *a))
    > ||error("VMerror in roll"));
    > for(i=0;i<n;i++) a = pop;
    > if(j>0) {
    > for(i=j-1; i>=0; i--) push(a);
    > for(i=n-1; i>=j; i--) push(a);
    > } else { //j<0
    > for(i=n-1+j; i>=0; i--) push(a);
    > for(i=n-1; i>=n+j; i--) push(a);
    > }
    > free(a);
    > }
    > I have a vague memory of a function called
    > alloc() that would do this, but gcc doesn't
    > have it.


    You're thinking of the function `alloca'. Don't use it; it's
    non-portable and has a number of pitfalls. Variable-length arrays are
    better. See below.

    > From something posted recently I got the
    > idea to try:
    > Object a[n];
    > But, of course, n doesn't contain the argument
    > yet during the declaration section. (I did try it:
    > Segfault).


    In C99 this will work; declarations no longer need to be at the very
    beginning of the block, and variable-length arrays are supported. Just
    declare the array `a' after `n' has been set.

    int i,j,n;
    j = pop();
    n = pop();
    Object a[n];

    If your compiler doesn't support this, I'd probably suggest sticking
    with malloc/free rather than resorting to alloca, except in very special
    situations. (Note you can replace the call to calloc with a call to
    malloc. You don't need the array zeroed out, since you immediately fill
    it in with other data, and omitting the zeroing will save a few cycles.)

    > Can I make a bogus function call to load values
    > on the stack (being careful not to call another
    > function while expecting them to be there)?


    I'm not quite sure what you have in mind, but it doesn't sound to me
    like a good idea.
    Nate Eldredge, Mar 10, 2009
    #2
    1. Advertising

  3. On Mar 9, 10:57 pm, Nate Eldredge <> wrote:
    > luser-ex-troll <> writes:
    > > Is there a way to replace the heap allocation
    > > with stack allocation in the following function?

    >
    > > void Oroll() {
    > >     int i,j,n;
    > >     Object *a;
    > >     stackis2(integer,integer,roll);
    > >     j = pop.u.i;
    > >     n = pop.u.i;
    > >     j %= n; //eliminate bogus loops
    > >     (void)((a=calloc((size_t)n,sizeof *a))
    > >         ||error("VMerror in roll"));
    > >     for(i=0;i<n;i++) a = pop;
    > >     if(j>0) {
    > >         for(i=j-1; i>=0; i--) push(a);
    > >         for(i=n-1; i>=j; i--) push(a);
    > >     } else { //j<0
    > >         for(i=n-1+j; i>=0; i--) push(a);
    > >         for(i=n-1; i>=n+j; i--) push(a);
    > >     }
    > >     free(a);
    > > }
    > > I have a vague memory of a function called
    > > alloc() that would do this, but gcc doesn't
    > > have it.

    >
    > You're thinking of the function `alloca'.  Don't use it; it's
    > non-portable and has a number of pitfalls.  Variable-length arrays are
    > better.  See below.
    >
    > > From something posted recently I got the
    > > idea to try:
    > > Object a[n];
    > > But, of course, n doesn't contain the argument
    > > yet during the declaration section. (I did try it:
    > > Segfault).

    >
    > In C99 this will work; declarations no longer need to be at the very
    > beginning of the block, and variable-length arrays are supported.  Just
    > declare the array `a' after `n' has been set.
    >
    > int i,j,n;
    > j = pop();
    > n = pop();
    > Object a[n];
    >


    Thanks! [thumps forehead theatrically]
    I've decided to open a new block just before
    the new decl to keep splint happy.
    (I think it also helps "announce" a declaration
    happening where it might not be expected;
    although for such a short function, it does
    add two lines.)

    > If your compiler doesn't support this, I'd probably suggest sticking
    > with malloc/free rather than resorting to alloca, except in very special
    > situations.  (Note you can replace the call to calloc with a call to
    > malloc.  You don't need the array zeroed out, since you immediately fill
    > it in with other data, and omitting the zeroing will save a few cycles.)
    >


    That's a good way to distinguish the two.
    I've always just associated malloc with make-one-thing
    and calloc with make-lots-of-things.
    It's easy to remember.

    > > Can I make a bogus function call to load values
    > > on the stack (being careful not to call another
    > > function while expecting them to be there)?

    >
    > I'm not quite sure what you have in mind, but it doesn't sound to me
    > like a good idea.


    Wild flailing. Apologies for the screenspace.
    lxt
    luser-ex-troll, Mar 10, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    600
    Martin Steen
    May 4, 2005
  2. Michael B Allen
    Replies:
    11
    Views:
    507
    Richard Bos
    Jul 31, 2003
  3. valerio
    Replies:
    3
    Views:
    358
  4. Replies:
    4
    Views:
    303
    John Harrison
    Jun 30, 2007
  5. CD1
    Replies:
    6
    Views:
    2,128
Loading...

Share This Page