What is the difference of pointers to char on type alisa?

F

fl

Hi,

The following is except from C usage in TI DSP. It mentions memory alias, pointer-to-char. I do not understand what is special for the point-to-char. Could you give me some explanations on it?


Thanks,



.................................................
With packed data processing, it is desirable to access multiple elements of a
given type with a single de-reference as shown in Example 2-9. Normally,
de-referencing a pointer-to-type returns a single element of that type.
Furthermore, the ANSI C standard states that pointers to different types are
presumed to not alias (except in the special case when one pointer is a
pointer-to-char).
 
E

Eric Sosman

Hi,

The following is except from C usage in TI DSP. It mentions memory alias, pointer-to-char. I do not understand what is special for the point-to-char. Could you give me some explanations on it?


Thanks,



................................................
With packed data processing, it is desirable to access multiple elements of a
given type with a single de-reference as shown in Example 2-9. Normally,
de-referencing a pointer-to-type returns a single element of that type.
Furthermore, the ANSI C standard states that pointers to different types are
presumed to not alias (except in the special case when one pointer is a
pointer-to-char).

The in-memory representation of any[*] C object's value
can be viewed as a sequence of bytes, that is, as an array of
`char'. C permits you to inspect those bytes individually,
and even to change them (although this may mess up the value
of the containing object). This ability to treat the object
either as its whole self or as an array of individual bytes is
so ingrained in C that the aliasing rules make a special
exception to ensure it works as expected. Example:

void func(int *count, double *ratio) {
*count = 42;
*ratio = 0.5;
*count += 1;
}

Since `int' and `double' are not compatible types, the compiler
can assume that the two pointers aim at different objects. That
means that the assignment to `*ratio' doesn't affect the value
found at `*count', so the optimizer can streamline the code:

void func(int *count, double *ratio) {
*ratio = 0.5;
*count = 43;
}

Of course, if `count' and `ratio' somehow *do* point to the same
place this rewrite would change the behavior: Storing to `*ratio'
would change the value found at `*count', so the `+= 1' calculation
cannot be predicted and simplified. The usual aliasing rules say
that the problem is your fault: You're not supposed to aim at
something with a pointer of the wrong type,[**] so the compiler's
rewrite is allowed.

But since a `char*' could legitimately point at a constituent
byte of some other object, things are different with this function:

void bunk(int *count, char *where) {
*count = 42;
*where = 19;
*count += 1;
}

This time the compiler is not allowed to streamline the code; it
must make things work correctly even if `*where' is one of the
bytes of `*count'. That's the "special case" you asked about.

[*] A struct or union may contain "bit-fields," which can
have weird sizes and cannot be treated as sets of bytes. Even
so, the containing struct or union can still be viewed as an
array of char.

[**] There are additional special rules dealing with unions,
which I'm just ignoring here.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top