lvalue cast

P

pinkisntwell

int a, *pa;
pa = &a;
*((double*)pa)=100;

Since double holds more space than int, does the above mean that I'm
writing beyond the memory allocated for the program? What exactly is
happening in the above snippet?
 
N

Nick Keighley

int a, *pa;
pa = &a;
*((double*)pa)=100;

Since double holds more space than int, does the above mean that I'm
writing beyond the memory allocated for the program? What exactly is
happening in the above snippet?

I find your terminology a bit odd. double /uses/ or /occupies/ more
space than int. A rather, it does on many implementations. As they say
where I come from "you can't get a quart in a pint pot". In general
using a cast to stuff a value of one type into another is a Bad Idea.

int i = 99;
int *pi = &i;
double d = 77.77;
double *pd = &d;

*((double*)pi) = d; /* Bad */
*((int*)pd) = i; /* also bad */
i = d; /* loses fractional part but well defined
behaviour */
d = i; /* ok */

The exception to don't-stuff-a-value-of-one-type-into-another is that
any type can be converted into an array of unsigned char (array of
bytes)

unsigned char *pb = malloc (sizeof(double)); /* check malloc worked
*/
memcpy (pb, &d, sizeof(double));
for (i = 0; i < sizeof(double); i++)
printf ("%.02X", pb & 0xff);
printf ("\n");

or avoid the malloc

unsigned char *pb = (unsigned char*)&d;
for (i = 0; i < sizeof(double); i++)
printf ("%.02X", pb & 0xff);
printf ("\n");



code not tested or even compiled...
 
P

Paul N

int a, *pa;
pa = &a;
*((double*)pa)=100;

Since double holds more space than int, does the above mean that I'm
writing beyond the memory allocated for the program? What exactly is
happening in the above snippet?

It's perhaps worth pointing out that, not only does a double (usually)
take up more space than an int, but also it (usually) stores the value
in a different way. So your line above may not store anything anywhere
that looks like the way it would store an int of 100. By contrast, if
you cast to (long *), the chances are it will store, somewhere,
something that looks like the way it stores an int of 100 - depending
on whether your system is "big-endian" or "little-endian" it might set
the value of a to 100 and stomp over the value of something else, or
it might set the value of a to 0 and stomp over something else. But
technically it's undefined behaviour, which means anything could
happen.
 
R

Richard Tobin

pinkisntwell said:
int a, *pa;
pa = &a;
*((double*)pa)=100;

Since double holds more space than int, does the above mean that I'm
writing beyond the memory allocated for the program?

Perhaps, though more likely you are writing into memory allocated
to the program but used for something else. In fact, it's quite
likely that you would be overwriting the pointer pa itself, since
it might well be allocated immediately after a. Or you might
be overwriting the function's return address, or an unused bit of
memory (in which case it might seem to work).

As a pedant, I can't help pointing out that your subject line isn't
quite right: it's not significant that the cast is applied to an
lvalue. It would be just as bad with any other pointer expression.

-- Richard
 

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

Similar Threads

Logic Problem with BigInteger Method 2
Questions about pointer comparisons 58
cast musings 19
Cannot convert (double) to (double*) 1
Lvalue 16
Lvalue Required(wierd)... 20
receiving lvalue as rvalue 3
TF-IDF 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top