D
David Mathog
Normally in a case like this:
char *string=NULL;
....
string=malloc(ALLOCSIZE);
it is fine and proper to later access string with these
string
string++
However, these are situations where one would like to forbid nonzero
offset access to a pointer. Is there a way to tell a compiler that
this should be an error? For instance, in a structure situation like
this:
typedef struct is_inner INNER;
typedef struct is_outer OUTER;
struct is_outer {
INNER *list;
int blah;
/*etc.*/
};
struct is_inner{
char *string;
int length;
}
OUTER *ohandle;
ohandle=malloc(sizeof(OUTER));
ohandle->list=malloc(sizeof(INNER)*N);
These operations:
ohandle++
ohandle
are mistakes, at least in the intended use of this code, since ohandle
is a single struct. However, the compiler does not issue a warning
since they are valid pointer operations. Where they occur the
programmer probably meant
ohandle->list++;
ohandle->list;
Is there any standard way to tell a compiler that "accessing this
pointer with any nonzero offset is illegal?" This isn't static and it
isn't const. The closest words II can think of for the desired
behavior are "pinned" and "locked", but there are no such keywords.
I know the situation can be avoided by passing the structure and not a
pointer to the structure, but that isn't going to be good, performance
wise, when the structure in question is large and complex.
Thanks,
David Mathog
char *string=NULL;
....
string=malloc(ALLOCSIZE);
it is fine and proper to later access string with these
string
string++
However, these are situations where one would like to forbid nonzero
offset access to a pointer. Is there a way to tell a compiler that
this should be an error? For instance, in a structure situation like
this:
typedef struct is_inner INNER;
typedef struct is_outer OUTER;
struct is_outer {
INNER *list;
int blah;
/*etc.*/
};
struct is_inner{
char *string;
int length;
}
OUTER *ohandle;
ohandle=malloc(sizeof(OUTER));
ohandle->list=malloc(sizeof(INNER)*N);
These operations:
ohandle++
ohandle
are mistakes, at least in the intended use of this code, since ohandle
is a single struct. However, the compiler does not issue a warning
since they are valid pointer operations. Where they occur the
programmer probably meant
ohandle->list++;
ohandle->list;
Is there any standard way to tell a compiler that "accessing this
pointer with any nonzero offset is illegal?" This isn't static and it
isn't const. The closest words II can think of for the desired
behavior are "pinned" and "locked", but there are no such keywords.
I know the situation can be avoided by passing the structure and not a
pointer to the structure, but that isn't going to be good, performance
wise, when the structure in question is large and complex.
Thanks,
David Mathog