Re: Memory alignment
On Oct 4, 4:34 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Thanks Richard. I understood what said. I'd like to apologize for
asking for more questions. But I really would like to get to the
bottom of this.
If the memory is corrupted, wouldn't the system eventually crash
if you run it long enough? If so, then we can be sure that the
corruption will be noticeable.
I went back to c-faq to read 2.6 many times again. I'll paste
the code here for easy reference.
#include <stdlib.h>
#include <string.h>
struct name {
int namelen;
char namestr[1];
};
struct name *makename(char *newname)
{
struct name *ret =
malloc(sizeof(s truct name)-1 + strlen(newname) +1);
/* -1 for initial [1]; +1 for \0 */
if(ret != NULL) {
ret->namelen = strlen(newname) ;
strcpy(ret->namestr, newname);
}
return ret;
}
Although not specifically stated, padding is likely
to occur for namestr. So strcpy must have written into
the padding bytes. The faq says "... has deemed that
it is not strictly conforming with the C Standard,
although it does seem to work under all known
implementations ...".
I know it's bad and it shouldn't be done. But when
I look at tens of thousands of lines of code written
by someone else and many of them make use of this
hack. What can we conclude? Perhaps it does work,
just like the faq says.
I appreciate all of you who took the time to answer
my questions. Not all of us work with C everyday,
that's why we ask question here. Of course we try
to google and ask our colleagues before turning to
the group, but it doesn't always work - as this
"struct hack" indicated. It doesn't help to answer
the question with an almighty attitude, again as
this "struct hack" has indicated, although the
consensus is not to do it, but no one can say for
sure if the system will eventually die or crash.
Very often, we ask a question because we badly
need help and we know there are many knowledgeable
and competent people here. Thanks again for your
time.
>
His answer is wrong. (See below.)
>
>
>
>
On Oct 4, 4:34 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Keith Thompson said:
>
>
<snip>
>
>
It is a very simple, but two-fold, answer - YES, you can be sure that
writing into memory you don't own causes memory corruption; and NO, you
can't be sure that the effect of this corruption will always be
noticeable. When you write into memory you don't own, the behaviour of
your program is undefined, and the rules of C no longer apply - so
anything can happen, including (but by no means limited to) what you
expected to happen.
>
Why Tea <ytl...@gmail.c omwrites:
<snip>
>
I understood it's bad and dangerous
programming, but the question is can we be 100% sure that an
existing code like that causes memory corruption? Based on the
little I know, I don't think it's a simple answer of a YES or NO.
programming, but the question is can we be 100% sure that an
existing code like that causes memory corruption? Based on the
little I know, I don't think it's a simple answer of a YES or NO.
It is a very simple, but two-fold, answer - YES, you can be sure that
writing into memory you don't own causes memory corruption; and NO, you
can't be sure that the effect of this corruption will always be
noticeable. When you write into memory you don't own, the behaviour of
your program is undefined, and the rules of C no longer apply - so
anything can happen, including (but by no means limited to) what you
expected to happen.
asking for more questions. But I really would like to get to the
bottom of this.
If the memory is corrupted, wouldn't the system eventually crash
if you run it long enough? If so, then we can be sure that the
corruption will be noticeable.
I went back to c-faq to read 2.6 many times again. I'll paste
the code here for easy reference.
#include <stdlib.h>
#include <string.h>
struct name {
int namelen;
char namestr[1];
};
struct name *makename(char *newname)
{
struct name *ret =
malloc(sizeof(s truct name)-1 + strlen(newname) +1);
/* -1 for initial [1]; +1 for \0 */
if(ret != NULL) {
ret->namelen = strlen(newname) ;
strcpy(ret->namestr, newname);
}
return ret;
}
Although not specifically stated, padding is likely
to occur for namestr. So strcpy must have written into
the padding bytes. The faq says "... has deemed that
it is not strictly conforming with the C Standard,
although it does seem to work under all known
implementations ...".
I know it's bad and it shouldn't be done. But when
I look at tens of thousands of lines of code written
by someone else and many of them make use of this
hack. What can we conclude? Perhaps it does work,
just like the faq says.
I appreciate all of you who took the time to answer
my questions. Not all of us work with C everyday,
that's why we ask question here. Of course we try
to google and ask our colleagues before turning to
the group, but it doesn't always work - as this
"struct hack" indicated. It doesn't help to answer
the question with an almighty attitude, again as
this "struct hack" has indicated, although the
consensus is not to do it, but no one can say for
sure if the system will eventually die or crash.
Very often, we ask a question because we badly
need help and we know there are many knowledgeable
and competent people here. Thanks again for your
time.
That's why I think Antoninus has given a more accurate answer.
His answer is wrong. (See below.)
>
can't swear by the bible
"It's not OK at all"
"It's not OK at all"
Um, did you read my answer, in which I specifically acknowledged that
you can probably get away with accessing padding bytes but also
explained why it's a bad idea to depend on it?
you can probably get away with accessing padding bytes but also
explained why it's a bad idea to depend on it?
There is nothing "pragmatic" about encouraging you to assume that
there are a certain number of padding bytes at the end of a structure,
and that you can safely use them for whatever you want.
there are a certain number of padding bytes at the end of a structure,
and that you can safely use them for whatever you want.
Comment