public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: vector of aligned elements
@ 2008-06-22 12:01 Eus
  0 siblings, 0 replies; 2+ messages in thread
From: Eus @ 2008-06-22 12:01 UTC (permalink / raw)
  To: massimiliano cialdi; +Cc: gcc-help

Hi Ho!

--- On Fri, 6/20/08, "massimiliano cialdi" <cialdi@gmail.com> wrote:

> is possoble to declare an array of element aligned?

Yes, it is.

> for example is possible to declare an array of long aligned to 8 byte,
> so as if &array[0] is x then &array[1] is x+MAX(8,sizeof(long))

Yes, it is.

> I tried the following
>
> #include <stdio.h>
>
> typedef long aligned_long __attribute__ ((aligned (32)));
>
> aligned_long a[4];
>
> int main(void)
> {
>     printf("test %d\n", sizeof(a));
>     return 0;
> }
>
> compiled with the following command line
> gcc -Wall main.c -o main
> (gcc 4.2.1)
>
> I obtain this error
> error: alignment of array elements is greater than element size
>
>
> So is it impossible or is there another way?

The following one should work:

typedef union {
	long value;
	char alignment [32];
} aligned_long;

> this is not the real problem, I simply try to answer to the following question:
> can I assume that (&array[1]-&array[0])==sizeof(array[0])?

You mean: ((int) &array[1] - (int) &array[0]) == sizeof (array[0])? It is because &array[1]-&array[0] will always give you 1 since it is a pointer arithmetic operation.

If so, yes, I think you can assume that because sizeof() also takes into account the alignment, for example,

struct x {
	char a;
	int b;
};

sizeof(x) == 8

struct x array[2];

sizeof(array) == 16.

So, ((int) &array[1] - (int) &array[0]) == sizeof (array[0]).

> thanks

Your welcome.

> --
> Et nunc, auxilium solis, vincam!
> Oppugnatio solaris!
> VIS!
>
> Massimiliano Cialdi
> cialdi@gmail.com
> massimiliano.cialdi@powersoft.it

Best regards,
Eus


      

^ permalink raw reply	[flat|nested] 2+ messages in thread

* vector of aligned elements
@ 2008-06-20  7:13 massimiliano cialdi
  0 siblings, 0 replies; 2+ messages in thread
From: massimiliano cialdi @ 2008-06-20  7:13 UTC (permalink / raw)
  To: gcc-help

is possoble to declare an array of element aligned?

for example is possible to declare an array of long aligned to 8 byte,
so as if &array[0] is x then &array[1] is x+MAX(8,sizeof(long))

I tried the following

#include <stdio.h>

typedef long aligned_long __attribute__ ((aligned (32)));

aligned_long a[4];

int main(void)
{
	printf("test %d\n", sizeof(a));
	return 0;
}

compiled with the following command line
gcc -Wall main.c -o main
(gcc 4.2.1)

I obtain this error
error: alignment of array elements is greater than element size


So is it impossible or is there another way?

this is not the real problem, I simply try to answer to the following question:
can I assume that (&array[1]-&array[0])==sizeof(array[0])?

thanks
-- 
Et nunc, auxilium solis, vincam!
Oppugnatio solaris!
VIS!

Massimiliano Cialdi
cialdi@gmail.com
massimiliano.cialdi@powersoft.it

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-06-22 12:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-22 12:01 vector of aligned elements Eus
  -- strict thread matches above, loose matches on Subject: below --
2008-06-20  7:13 massimiliano cialdi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).