pointer to an array -Is there an out of bound access?

R

Richard Heathfield

Frederick Gotham said:
Richard Heathfield:
Frederick Gotham said:
What about the following, is it OK?

int arr[2][2];

int *p = *arr;

p[3] = 8;

No, for reasons that have already been explained ad nauseam.


Then how can we treat any chunk of memory as if it's an array of unsigned
char?

Because the Standard says we can.
 
J

Jordan Abel

2006-10-31 said:
Richard Heathfield:
Frederick Gotham said:
What about the following, is it OK?

int arr[2][2];
int *p = *arr;
p[3] = 8;

No, for reasons that have already been explained ad nauseam.

Then how can we treat any chunk of memory as if it's an array of unsigned
char?

Not to mention, there's not a whole heck of a lot of point to forbidding
arrays from having padding at the end other than to guarantee the above.

What else could array padding break?
 
D

Default User

Frederick said:
Richard Heathfield:
Frederick Gotham said:
What about the following, is it OK?

int arr[2][2];

int *p = *arr;

p[3] = 8;

No, for reasons that have already been explained ad nauseam.


Then how can we treat any chunk of memory as if it's an array of
unsigned char?


I don't see you doing that.



Brian
 
J

Jordan Abel

2006-10-31 said:
Frederick said:
Richard Heathfield:
Frederick Gotham said:
What about the following, is it OK?
int arr[2][2];
int *p = *arr;
p[3] = 8;

No, for reasons that have already been explained ad nauseam.

Then how can we treat any chunk of memory as if it's an array of
unsigned char?

I don't see you doing that.

OK, so is int *p = (int *)(unsigned char *)*arr; p[3] = 8; legal?
 
K

kuyper

I normally don't monitor comp.lang.c, but since you brought this issue
over to comp.std.c and comp.std.c++, and mentioned your discussions on
comp.lang.c, I thought I should check them out. Oddly enough, I found
that you had written something that has nothing to do with C, that
needs to be corrected. I apologize for the off-topic nature of this
message, but posting it to this newsgroup is the only way to ensure
that it is read by the same people who read the original comment, and
might have been mislead by it.

Frederick said:
Richard Heathfield: ....


Yes you're right. But how exactly do these multi-dimensional arrays work?

Do you think it's right to say that, other than the proton and the
electron, that there's a third kind of particle within the atom? Some would
say yes, the neutron. Others would say no, because a neutron is little more
than an electron and a proton bound together.

An electron is a lepton; neither protons nor neutrons contain any real
leptons. A proton consists of two up quarks and one down quark. A
neutron consists of one up quark and two down quarks. There is a
nuclear reaction that is incorrectly described as electron capture,
whereby an electron and a proton react to produce a neutron, but what
is incorrect about that description is that the process also produces a
neutrino, which is really just a different form of the same lepton that
was an electron coming into the reaction.

There are many complicated Feynman diagrams contributing to the cross
section for this reaction, but the dominant and simplest one can be
described as follows: an electron converts into a neutrino, giving off
a W- particle. The W- particle is the carrier for the weak nuclear
force, in the same sense that a photon is the carrier for the
electromagnetic force, but with the key difference that the W- particle
carries a charge. That W- particle is aborbed by an up quark,
converting it into a down quark, with the result that the proton is
converted into a neutron. That same diagram, with a different time
ordering, can also be described as follows: An up quark converts into a
down quark, giving off a W+ particle. That particle is absorbed by an
electron, converting it into a neutrino.

Similar comments apply to the reverse reaction, whereby a neutron
decays into a proton and an electron; what is missing from the
description is the fact that the decay also produces an anti-neutrino.
The dominant diagram for that decay involves a down quark converting
into an up quark, giving off a W- particle. The W- particle decays into
an electron and an anti-neutrino.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top