This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

use of alloca() inside the arguments of a function call


Greetings,

I have only tried this on egcs 1.1, so I have no idea how other compilers deal with this.
What are the limitations of using a call to alloca() inside the arguments to a function?

Consider this test:

// from the gnu libc docs
//
// Do not use alloca inside the arguments of a function call--you will get
// unpredictable results, because the stack space for the alloca would appear
// on the stack in the middle of the space for the function arguments. An example
// of what to avoid is foo (x, alloca (4), y). 

#include <iostream.h>
#include <string.h>

#if ALLOCA_LAST

#define FOO(p, arg1, arg2) foo(arg1, arg2, alloca(size))
static void *foo(int x, int y, void *p)
{
  cout << x << " " << y << endl;
  return p;
}

#elif ALLOCA_MIDDLE

#define FOO(p, arg1, arg2) foo(arg1, p, arg2)
static void *foo(int x, void *p, int y)
{
  cout << x << " " << y << endl;
  return p;
}

#else // ALLOCA_FIRST

#define FOO(p, arg1, arg2) foo(alloca(size), arg1, arg2)
static void *foo(void *p, int x, int y)
{
  cout << x << " " << y << endl;
  return p;
}

#endif

const static size_t size = 4;

int main(int, char **)
{
  int x = 42;
  int y = 24;
  void *i  = FOO(NULL, x, y);
  memset(i, 0, size);
  return 0;
}


If you define ALLOCA_MIDDLE (-DALLOCA_MIDDLE) you get a segv on the memset.  However, if you
define ALLOCA_LAST, or nothing at all (alloca() first) then all is well.  The comments at the
top of this file are straight out of the gnu libc docs.  So whar *are* the limitations?  Are
g++ limitations similiar to that of other compilers (vc++, SunPro CC, ...)?

Cheers,
Robert


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]