Can I allocate an array on the stack?

L

luser-ex-troll

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

Nate Eldredge

luser-ex-troll said:
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.
 
L

luser-ex-troll

luser-ex-troll said:
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.
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
 

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,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top