# More on alignment

Discussion in 'C Programming' started by H.K. Kingston-Smith, Jun 16, 2008.

1. ### H.K. Kingston-SmithGuest

Thanks to everybody who replied to my question on alignment on
stack arrays. In a similar vein, what can one say about the alignment of
p in

char *p = alloca(N) ;

where N can be any integer smaller than the stack space available? Is it
going to follow similar alignment patterns as malloc(N) or as char p[N]?
I am aware that using alloca() is not encouraged, but I have come across
a few situations in which it would advantageous to do so.

H.K. Kingston-Smith, Jun 16, 2008

2. ### Kenny McCormackGuest

In article <g367ts\$7e9\$>,
H.K. Kingston-Smith <> wrote:
> Thanks to everybody who replied to my question on alignment on
>st**k arrays. In a similar vein, what can one say about the alignment of
>p in
>
> char *p = alloca(N) ;
>
>where N can be any integer smaller than the stack space available? Is it
>going to follow similar alignment patterns as malloc(N) or as char p[N]?
>I am aware that using alloca() is not encouraged, but I have come across
>a few situations in which it would advantageous to do so.
>

You are now allowed to use that kind of language ("st**k" or "a((oca")
in this newsgroup.

Now, go wash your mouth out with soap.

Kenny McCormack, Jun 16, 2008

3. ### santoshGuest

H.K. Kingston-Smith wrote:

> Thanks to everybody who replied to my question on alignment on
> stack arrays. In a similar vein, what can one say about the alignment
> of p in
>
> char *p = alloca(N) ;
>
> where N can be any integer smaller than the stack space available? Is
> it going to follow similar alignment patterns as malloc(N) or as char
> p[N]? I am aware that using alloca() is not encouraged, but I have
> come across a few situations in which it would advantageous to do so.

Strictly speaking alloca is not defined by the C standard (or any other
standard AFAIK) so it's behaviour cannot be accurately determined
without examining your implementation of it. However all the
implementations of alloca that I have seen have returned memory that is
aligned for the strictest possible type in the system. Hence it can
used to initialise any pointer type. In this way it's exactly like
malloc, as far as I can see.

santosh, Jun 16, 2008
4. ### rahulGuest

On Jun 16, 10:29 pm, "H.K. Kingston-Smith" <> wrote:
> Thanks to everybody who replied to my question on alignment on
> stack arrays. In a similar vein, what can one say about the alignment of
> p in
>
> char *p = alloca(N) ;
>
> where N can be any integer smaller than the stack space available? Is it
> going to follow similar alignment patterns as malloc(N) or as char p[N]?
> I am aware that using alloca() is not encouraged, but I have come across
> a few situations in which it would advantageous to do so.

Quoting from the manual :

The alloca() function is machine and compiler dependent. On many
systems its implementation is
buggy. Its use is discouraged.

The more serious issue:
On many systems alloca() cannot be used inside the list of arguments
of a function call, because
the stack space reserved by alloca() would appear on the stack in the
middle of the space for the
function arguments.

rahul, Jun 17, 2008