about *long

S

sam

Hi,
I want to ask question about following:-
#define SIZE 6000
long ret=0x00001515;
char buf[SIZE+5];
*(long*)&buf[strlen(buf)]=ret;
what exactly happenning at the last line of code.
 
G

Gianni Mariani

sam said:
Hi,
I want to ask question about following:-
#define SIZE 6000
long ret=0x00001515;
char buf[SIZE+5];
*(long*)&buf[strlen(buf)]=ret;
what exactly happenning at the last line of code.

That is a type cast, dereference and assignment. Very non-portable code
BTW - on some machines it will crash and on some it will arrange bytes
differently.

Assuming that between the definition of buf and the last line, there is
an initialization of buf taking place, what is happening is that it is
placing at the end of the null terminated string in buf a long.

e.g. if buf contained the sequence 'a', 'b', 'c', '\0', x, x, x, y then,
assuming long is a 4 byte type, the nul and the locations marked as 'x'
will be replaced with the contents of "ret" which could be 0x15, 0x15,
0, 0 -or- 0, 0, 0x15, 0x15 or some other machine dependant format of
"long" (could be 64 bit or 16 bit, you jusy don't know).
 
J

James Kanze

sam wrote:
I want to ask question about following:-
#define SIZE 6000
long ret=0x00001515;
char buf[SIZE+5];
*(long*)&buf[strlen(buf)]=ret;
what exactly happenning at the last line of code.

A programmer is proving himself incompetent?
That is a type cast, dereference and assignment. Very
non-portable code BTW - on some machines it will crash and on
some it will arrange bytes differently.

Most importantly, here: the cast is a reinterpret_cast, which
always depends somewhat on the implementation. (Why the
original author chose to hide this critical information from us
by using a C style cast is another question.)
Assuming that between the definition of buf and the last line, there is
an initialization of buf taking place, what is happening is that it is
placing at the end of the null terminated string in buf a long.
e.g. if buf contained the sequence 'a', 'b', 'c', '\0', x, x, x, y then,
assuming long is a 4 byte type, the nul and the locations marked as 'x'
will be replaced with the contents of "ret" which could be 0x15, 0x15,
0, 0 -or- 0, 0, 0x15, 0x15 or some other machine dependant format of
"long" (could be 64 bit or 16 bit, you jusy don't know).

Just a nit, but strlen() returns the length of the string
without the final '\0', so the final '\0' will be overwritten.
And long can't be 16 bits, since LONG_MAX must be at least
2147483647.

But those are just nits---as you say, you just don't know.

FWIW, on my machine, if buf is initialized with "abc" (e.g. by
string copy), the code crashes. (long is 8 bytes, and requires
8 byte alignment.)
 
J

Jack Klein

Hi,
I want to ask question about following:-
#define SIZE 6000
long ret=0x00001515;
char buf[SIZE+5];
*(long*)&buf[strlen(buf)]=ret;
what exactly happenning at the last line of code.

As someone else said, an ignorant programmer thinking he was writing
"cool" code.

If it so happens that the address buf + strlen(buf) is not properly
aligned for a long, the behavior is undefined. On some architectures,
such as ARM, that could result in a trap causing the operating system
to terminate the program.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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
474,183
Messages
2,570,967
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top