Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Next stupid cast question

Reply
Thread Tools

Re: Next stupid cast question

 
 
Thomas Matthews
Guest
Posts: n/a
 
      08-13-2003
Dopey wrote:

> This is not homework. GNU gcc 3.3, SuSE 8.2, Intel P4
>
> Referencing the code (see below).
>
> I'm trying to learn how to make an assignment via a cast in an (1)
> accurate and (2) efficient way. What I'm trying to get out of the
> assignment is a 'struct someints' of { 1, 2, 3, 4 }. I'm getting
> something REALLY different, which is discouraging because it means
> I'm even more clueless than I thought.
>
> OUTPUT:
> si1: 808464433 808464434 808464435 808464436
> si2: 808464433 808464434 808464435 808464436

Looks like addresses or locations.
Could be the zipcodes of Nasal Demons too.



> I've tried stuffing buf with "0001000200030004" as well. Here's that
> output:
>
> si1: 808464432 808464433 808464434 808464435
> si2: 808464432 808464433 808464434 808464435
>
> (I see the pattern there, but I haven't been able to figure out what
> it means yet.)
>
> I've also tried malloc()'ing 4 + 1 bytes to buf, and assigning it
> "1111" and "1234" but those results are whacked as well.
>
> I'm reading K&R 2nd Ed and Koenig's "C Traps..." but apparently I
> need a little less...subtle...an explanation. (If anyone has a good
> reference or link that helps illuminate this degree of intellectual
> density, I'd sure love to see it.)
>
> Anyway, the code:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> struct someints
> {
> int a, b, c, d ;
> } ;
>
> char * buf ;
>
> struct someints * si1 ;
> struct someints si2 ;

Note that "si1" is a pointer, "si2" is a whole structure.



> int
> main(void)
> {
> buf = malloc( sizeof(struct someints) + 1 );

There is no need for the "+1".
Also you need to check "buf" for NULL. The malloc() function
returns NULL for problems.



> /* 16 */
> printf("sizeof(struct someints) = %d\n", sizeof(struct someints));

Where is the line containing "OUTPUT:\n"? Doesn't match your
given output above.

>
> strcpy( buf, "00001000200030004" );

Valid, if and only if size of "00001000200030004" is less than
sizeof(struct someints). {Why does "00001" have four zeros and
"0002" have only three?)

> si1 = (struct someints *)buf ;

Evil. Bad. Sets out a call for Nasal Demons.
Execute this snippet:
printf("sizeof integer variable: %d\n", sizeof(int));
printf("sizeof textual integer 00001: %d\n", sizeof("00001"));

> printf("si1: %d %d %d %d\n", si1->a, si1->b, si1->c, si1->d);

This statement starts the ritual summoning those Nasal Demons.
See reason below.

>
> si2 = *si1 ;

This starts the ritual drums to beat faster.

> printf("si2: %d %d %d %d\n", si2.a, si2.b, si2.c, si2.d);

And this unleashes those demons.

> return 0 ;
> }

You are confusing a textual integer with an integer variable.
The fundamental theorem is '1' != 1. {Or for those nitpickers,
a character representation of the value 1 may not be equal
to the internal value).

int value;
char text[64] = {'\0'};
value = 1;
sprintf(text, "%05d", value);

Mixing the textual representation with the internal
representations of integers, floats and doubles will
start the summoning ritual of Nasal Demons.

--
Thomas Matthews
Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

 
Reply With Quote
 
 
 
 
jemma
Guest
Posts: n/a
 
      08-13-2003
On Wed, 13 Aug 2003 21:26:21 GMT, Thomas Matthews
<(E-Mail Removed)> wrote:

>Dopey wrote:
>
>> OUTPUT:
>> si1: 808464433 808464434 808464435 808464436
>> si2: 808464433 808464434 808464435 808464436

>
>Looks like addresses or locations.
>Could be the zipcodes of Nasal Demons too.
>


Could be, but contrary to grouplore, nasal demons are far rarer
than one might think.

808464433 is 0x30303031,
808464434 is 0x30303032,
etc

Which is not suprising considering the ascii value of the character
string the OP used and the code posted. In spite of the fact the OP
committed a serious error by allocating too small of a buffer and
hence invoked undefined behaviour, there was no drum, ritual, or nasal
demons: the output that the OP saw would have been exactly the same
even if the buffer was allocated properly.

--Jemma

 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      08-13-2003
On Wed, 13 Aug 2003 22:49:11 GMT, in comp.lang.c , jemma
<jemmaq99@hotmail!.com> wrote:

>On Wed, 13 Aug 2003 21:26:21 GMT, Thomas Matthews
><(E-Mail Removed)> wrote:
>
>>Dopey wrote:
>>
>>> OUTPUT:
>>> si1: 808464433 808464434 808464435 808464436
>>> si2: 808464433 808464434 808464435 808464436

>>
>>Looks like addresses or locations.
>>Could be the zipcodes of Nasal Demons too.
>>

>
>Could be, but contrary to grouplore, nasal demons are far rarer
>than one might think.


Demons were however present in this case.

>In spite of the fact the OP
>committed a serious error by allocating too small of a buffer and
>hence invoked undefined behaviour, there was no drum, ritual, or nasal
>demons: the output that the OP saw would have been exactly the same
>even if the buffer was allocated properly.


Nasal demons did however appear, and their manifestation took the
form of suppressing the OS's warning that you'd just trampled on
memory belonging to your antivirus software which had consequently
failed and offered you a unique chance to learn more about MSBlast.

Nasal Demons need not be loud and vocal you know...

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Re: Next stupid cast question goose C Programming 0 08-14-2003 07:02 AM



Advertisments