Restricted arrays

S

Spiros Bousbouras

Is there a way to mimick restricted pointers
using array syntax ? So I'm looking for something
to add to a statement such as "int arr[50]" which
will tell the compiler that I will only access the contents of the
array through arr. If I was using
pointers I would do for example
int * restrict p = malloc(50 * sizeof(int))

Is there a way to do the same thing using
arrays ?
 
K

Keith Thompson

Spiros Bousbouras said:
Is there a way to mimick restricted pointers
using array syntax ? So I'm looking for something
to add to a statement such as "int arr[50]" which
will tell the compiler that I will only access the contents of the
array through arr. If I was using
pointers I would do for example
int * restrict p = malloc(50 * sizeof(int))

Is there a way to do the same thing using
arrays ?

I don't think so.

A possible workaround might be:

int do_not_refer_to_this[50];
int * restrict p = do_not_refer_to_this;

Of course p is not an array, so sizeof p won't give you the size of
the array as it would if you could do this directly.
 
B

bigcaterpillar

if use "int array[50] in local fuction" .when the program
run ,compiler allocates memory space in stack area.
if you use malloc function ,compiler acclocates memory space in heap
area.
"int array[50]" array is a constant pointer ,you can't change it.
"malloc..." you can change its direction and point somewhere.
 
S

Szabolcs Borsanyi

Is there a way to mimick restricted pointers
using array syntax ? So I'm looking for something
to add to a statement such as "int arr[50]" which
will tell the compiler that I will only access the contents of the
array through arr...

I am not quite sure if this works out, the experts will surely know...
If you declare your array as
struct { int a; } arr[50];
then the aliasing rules will restrict the access to it either through
the struct above (e.g. arr[3].a) or through a pointer to char.

Szabolcs
 

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

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top