473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

char * can point to any byte?!

On the following sites faq

http://c-faq.com/strangeprob/ptralign.html

"By converting a char * (which can point to any byte) to an int * or
long int *, and then indirecting on it, you can end up asking the
processor to fetch a multibyte value from an unaligned address, which
it isn't willing to do. "

How can char * point to any byte, but int * can't? Can someone clarify
this?

Thanks
Chad

Mar 18 '06 #1
10 1987
Chad a écrit :
How can char * point to any byte, but int * can't? Can someone clarify
this?


char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.
Mar 18 '06 #2

loufoque wrote:
Chad a écrit :
How can char * point to any byte, but int * can't? Can someone clarify
this?


char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.


So in other words, char * could point to the 2nd or 3rd byte in int?

Mar 18 '06 #3
"Chad" <cd*****@gmail.com> writes:
On the following sites faq

http://c-faq.com/strangeprob/ptralign.html

"By converting a char * (which can point to any byte) to an int * or
long int *, and then indirecting on it, you can end up asking the
processor to fetch a multibyte value from an unaligned address, which
it isn't willing to do. "

How can char * point to any byte, but int * can't? Can someone clarify
this?


For example, on some systems an int is 4 bytes long, and can only be
stored at an address that's a multiple of 4. In that case, an int*
cannot legally point to an odd address.

The details will vary from one system to another. On some systems, an
int* has a different representation than a char*, and an int* isn't
even physically capable of pointing to a single byte.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 18 '06 #4
Chad a écrit :
So in other words, char * could point to the 2nd or 3rd byte in int?


Yes.
Mar 18 '06 #5
"Chad" <cd*****@gmail.com> writes:
loufoque wrote:
Chad a écrit :
> How can char * point to any byte, but int * can't? Can someone clarify
> this?


char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.


So in other words, char * could point to the 2nd or 3rd byte in int?


Yes (assuming an int is at least 3 bytes long).

In fact, any object of any type can be treated as an array of unsigned
char.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 18 '06 #6
Chad wrote:
loufoque wrote:
Chad a écrit :
How can char * point to any byte, but int * can't? Can someone clarify
this?


char* is a pointer to a char, which is the size of a byte.
an int may be defined by multiple bytes.


So in other words, char * could point to the 2nd or 3rd byte in int?


Correct (well.. some systems have 2 byte ints..).

On some systems like x86 this is not a problem since memory is byte
aligned. But on other systems like ARM (iPaq, Palm, Smartphone etc)
this is a problem because memory access is word aligned (in the case of
ARM it is 32bit aligned). So the compiler will need to generate special
instructions like shifts to access bytes in the word.

Using char* tells the compiler that you want byte access to memory.
Using int* the compiler may not be able to access unaligned ints in
memory. This is the memory alignment 'bug' that people refer to when
porting badly written Linux programs to the ARM CPU.

Mar 18 '06 #7
slebetman wrote:
Chad wrote:
So in other words, char * could point to the 2nd or 3rd byte in int?


Correct (well... some systems have 2 byte ints).


Some even have 1-byte ints.
Mar 18 '06 #8
On 2006-03-18, Chad <cd*****@gmail.com> wrote:
On the following sites faq

http://c-faq.com/strangeprob/ptralign.html

"By converting a char * (which can point to any byte) to an int * or
long int *, and then indirecting on it, you can end up asking the
processor to fetch a multibyte value from an unaligned address, which
it isn't willing to do. "

How can char * point to any byte, but int * can't? Can someone clarify
this?


Hmmmm . . .

Because char * is a pointer to char, and int * is a pointer to int?

It's a surprisingly difficult question because it winds up being
different on every platform.

There are a great many platforms--including ones found in many homes and
schools--where you can blithly point a char * at most anything at all
and it Just Works the way one might wish.

Then you get a job programming a device that only has 32-bit data types
but it has to communicate with an 8-bit device that's not mapped to a
32-bit boundary.

It boils down to a question of the device you're programming and how it
deals with unaligned memory access. The best source of information is
going to be your chip vendor's documentation.

If you'd like an example, take a look at the C Language Reference
manuals for two TI families of DSP's. The "All 32-bits all the time"
TMS320c4x versus the "Oh this is almost like a PC" TMC320c67.

I'm afraid beyond the FAQ it gets implementation-specific rather
quickly.

Mar 18 '06 #9
Grumble wrote:
slebetman wrote:
Chad wrote:
So in other words, char * could point to the 2nd or 3rd byte in int?


Correct (well... some systems have 2 byte ints).


Some even have 1-byte ints.


They may have 1 byte words, which is what 8 bit systems have, but here
in c.l.c they cannot have 1 byte ints. By definition, ints are at least
16 bits.

Oh wait, I forgot... systems can have 16 bit bytes as well... so you
are right ;-)

Mar 19 '06 #10
On 2006-03-19, sl*******@yahoo.com <sl*******@gmail.com> wrote:
Grumble wrote:
slebetman wrote:
> Chad wrote:
>
>> So in other words, char * could point to the 2nd or 3rd byte in int?
>
> Correct (well... some systems have 2 byte ints).


Some even have 1-byte ints.


They may have 1 byte words, which is what 8 bit systems have, but here
in c.l.c they cannot have 1 byte ints. By definition, ints are at least
16 bits.

Oh wait, I forgot... systems can have 16 bit bytes as well... so you
are right ;-)


Plus, you have to make a judgement call over how non-conforming an
implementation can be before it's "not C". I've heard rumors of "C"
implementations for embedded systems that had 8-bit ints, or 16-bit ints
but no longs. There's also the question of "primordial" implementations,
that is, those that are prior to K&R1, or contemporary with it but
substantially different. It's hard to argue that the dialect described
in http://www.lysator.liu.se/c/bwk-tutor.html isn't C.
Mar 19 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
11
by: QQ | last post by:
I know a char is 2 bytes, the conversion is like byte byte_array = new byte; //Allocate double mem as that of char then for each char do byte = (byte) char & 0xff byte = (byte)( char >> 8 &...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
43
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize =...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
7
by: daniel | last post by:
Hello , I always had the feeling that is better to have char arrays with the size equal to a power of two. For example: char a_str; // feels ok char b_str; //feels not ok.
3
by: diadomraz | last post by:
Hi, This is a question about declaring a signed 8bit numeric type in C++ that prints like a numeric variable and not like a char variable. I would like to declare a type with name 'byte' which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.