First of all, I found this from the software I'm working on. It's not
written by me, so do not ask me why I wrote this.
The code on the OP is just something I written from scratch to illustrate
the whole thing. I don't want to copy the exact code, because it's not
important and it might be distracting, and will be a long post (seems
unavoidable now

). So do not tell me the correction of code like Stephen
and Darrell did. thanks anyway.
I did made a mistake in the code (maybe is this causing the confusion,
really sorry about that) which I intended to printf the oneChar instead of
buf on the last line. as follows:
printf("%c\n", mStruct.oneChar[i];
the purpose is to tell you that I can access all the data copied from
tenChar to oneChar. so the print out will be 10 ones as "1111111111".
I put char *buf just to illustrate this pointer has NOT been corrupted due
to stamping over by that "overflow copy". If I re-write it again, it will be
like this (the data vars before the main() are the same):
main()
{
struct myStruct mStruct;
char tenChar[10];
int d;
mStruct.buf = &d;
printf("%x\n", mStruct.buf); // let's say this will print "801a34bc"
memset( tenChar, 1, 10);
memcpy( mStruct.oneChar, &tenChars, 10);
for (i=0; i<10; i++)
printf("%c\n", mStruct.oneChar[i]; // this will print "1111111111"
printf("%x\n", mStruct.buf); // this will print "801a34bc"
}
As i said, I'm working for other people, and I can't tell them this is a bug
because it doesn't do anything wrong. so I need to know how I can generate a
fault with this, so that I can flag this as a bug and request a fix. that's
why I was asking what are the potential problems, as I can use these to
generate some faults. hopefully.
I will answer each post separately.
IMPORTANT BIT:
Remember the whole idea/point/question is copying a larger array to a
smaller one, what's potential problems?
give some examples or code so that i can reproduce, be it crashing, data
corruptions, whatever. Saying "undefine behaviour" is not what I lookin for,
because I know that.
"john" <john2000(n.o---s.p.a.m)@blueyonder.co.uk> wrote in message
news:%r6xc.32548$...
> I have a similar code to the following, here is the simplified version. I
> didn't compile and run the following code, so don't worry about the
syntax.
> My question is I memcpy a larger data (10Byte) to a smaller one (1B),
> however i can still access all the data (ref. to printf in the code). I
> suppose the stack size is usually big enough for this to happen (tell me
> otherwise). But what's the potential problem here?
> I thought it might screw up the pointer, mStruct.buf. But it's not. Is
this
> legal? if not, why it's compliable? any problem here? thanks a lot.
>
> char tenChars[10];
> struct myStruct
> {
> int a;
> char oneChar[1];
> char * buf;
> }
>
> main()
> {
> struct myStruct mStruct;
> char tenChar[10];
>
> memset( tenChar, 1, 10);
> memcpy( mStruct.oneChar, &tenChars, 10);
> for (i=0; i<10; i++)
> printf("%c\n", buf[i];
> }
>
>
> ----------------------------------
> remove "(n.o---s.p.a.m)" to reply
>
>
>
>