Re: pointer q



Joe Smith wrote:
"RSoIsCaIrLiIoA" <zz@xxxx> wrote in message news:9odg62tc13dv6m60mlsq19h4caf5ff06ij@xxxxxxxxxx
On Sun, 14 May 2006 08:48:41 -0400, Eric Sosman
<esosman@xxxxxxxxxxxxxxxxxxx> wrote:
Joe Smith wrote:
[motivation]
Assume sizeof(int) < sizeof(long), and consider

long l = 42;
int *ip = &l; /* illegal, but Let's Pretend */
*ip = 76;

What value is now stored in `l'?

That's why int* and long* aren't interchangeable.
and so where is the problem?
the problem is different cpus store data in different ways

here

#include <stdio.h>

int main ( void )
{int l = 42;
short *ip = &l; /* illegal, but Let's Pretend */

*ip = 76;
printf(" sizeof(short)==%u sizeof(int)==%u \n",
(unsigned) sizeof(short), (unsigned) sizeof(int));
printf("l==%d\n", l);
return 0;
}

sizeof(short)==2 sizeof(int)==4
l==76

all ok

On your system, but not on mine. I regularly work on both big and little endian systems and if you fix the above so it actually compilers you will get very different results.

the problem is "it is easy to obfuscate and make difficult
what it is easy" and it is in the cpu design

another example could be why not exchange "str* and sprintf, etc"
with a secure
int s?prinf(char* strig, int size_of_string, char* fmt, ...);?

because it is easier "to do difficult what is easy" then
Bugtraq is full of error for the C language

It is full of errors for programs written in all sorts of languages. Part of the reason so many are for programs written in C is because so many programs are written in C.

#include <stdio.h>

int main ( void )
{long l = 420000;
long *lp = &l;

short *sp;
*sp = (short)*lp; /* ill-advised, but Let's Pretend */

This is very different. If you were trying to fix the code further up so it would compile what you wanted was:
short *sp = (short*)*lp; /* ill-advised, but Let's Pretend */

printf(" sizeof(short)==%u sizeof(int)==%u \n",
(unsigned) sizeof(short), (unsigned) sizeof(int));
printf("in order, ell then the short %ld %hdh\n", l, *sp);
return 0;
}
/* end source */
Now that you bring this snippet up again, I think this code shows why it's usually a bad idea to do things along this line. I'm not sure that this is what Mr. Sosman was getting at.

Try it on both big and little endian systems, you can even do it with a pencil and paper if you don't have both, you will then see the very different results.

As a general rule casting is a bad idea. There are a few places where it is required,

> As to the syntax on the printf, I've done
everything short of Dedekind cuts on table B-1 and am still pulling out my hair.

printf is relatively simple, it is scanf that is tricky! If you want to ask specific questions about printf where you can't understand your text book I'm sure someone will answer them.

> I'm afraid that I don't know what Bugtraq is ... probably at my own
peril.

Basically a database of bugs.

> Am I correct to think that a type of masking occurs here? joe

No, not masking. More a case of only reading part of the data with my "fix". Or with your code writing to an uninitialised pointer and causing half of your socks to migrate to Alaska.

BTW, I would not place too much face in anything posted by RSoIsCaIrLiIoA. A Google search and seeing how often it has posted assembler will show you why many people probably have it kill filed.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.



Relevant Pages

  • Re: printf headache
    ... Joe Smith wrote: ... int main ... K&R print flags to %% and am out of guesses. ... Apparently noone has bothered to notice that you are calling gcd ...
    (comp.lang.c)
  • Re: printf headache
    ... Joe Smith wrote: ... [Mr. Collins' reasonable objection to the tone] ... int", %ul prints an "unsigned int" followed by the l character, it is ... Keith and pete were discussing how to write the actual name ...
    (comp.lang.c)
  • Re: printf and extended chars
    ... printf when extended chars are used? ... int main ... how can I fix this ...
    (comp.lang.c)
  • Re: printf and extended chars
    ... printf when extended chars are used? ... int main ... how can I fix this ...
    (comp.lang.c)
  • Re: printf headache
    ... Joe Smith wrote: ... int main ... You are using %f (float) to print an unsigned long. ... Ian Collins. ...
    (comp.lang.c)