473,763 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof( int ) != sizeof( void * )

I read somewhere that standard Ansi C on 32 bit architecures assumes a
lot of things which lead to poor code.

For example
sizeof( int ) = sizeof( void * )
sizeof( long ) = sizeof( int )

Why is this poor code?
Am i missing something?

Thank you

May 22 '06 #1
2 3664
blufox said:
I read somewhere that standard Ansi C on 32 bit architecures assumes a
lot of things which lead to poor code.
"Ansi C on 32 bit architectures" doesn't really mean a great deal. If you
mean "programs supposedly written in ANSI C but relying in practice on
non-portable assumptions that happen to be true on the development
platform", then that is indeed meaningful.
For example
sizeof( int ) = sizeof( void * )
sizeof( long ) = sizeof( int )

Why is this poor code?


If sizeof(int) is not the same as sizeof(void *), then it's a bad idea to
assume it is. And of course the same applies to your second example.

Even if these assumptions happen to be true on the machine we are working on
today, they might not be true on the machine the boss suddenly decides to
buy tomorrow. And since he's spent all that money on supposedly portable C
programs, he's going to want to run those programs on that machine,
tomorrow. Writing the programs correctly in the first place is far easier
than explaining to your boss why you didn't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 22 '06 #2

"Richard Heathfield"
blufox said:
I read somewhere that standard Ansi C on 32 bit architecures assumes a
lot of things which lead to poor code. "Ansi C on 32 bit architectures" doesn't really mean a great deal. If you
mean "programs supposedly written in ANSI C but relying in practice on
non-portable assumptions that happen to be true on the development
platform", then that is indeed meaningful. ANSI C on a 32 bit architecture probably means less to Richard than me, as I
struggle with producing something ISO with an OS that emulates 32 bit
processing on a 64 bit chip.
For example
sizeof( int ) = sizeof( void * )
sizeof( long ) = sizeof( int )

Why is this poor code?

In the stated environment and with vendor-dependent implementation, that
would mean for me in both statements:
2 = 4
without the necessary semicolon to notify your compiler that you're making a
different error than you think.
If sizeof(int) is not the same as sizeof(void *), then it's a bad idea to
assume it is. And of course the same applies to your second example.

Even if these assumptions happen to be true on the machine we are working
on
today, they might not be true on the machine the boss suddenly decides to
buy tomorrow. And since he's spent all that money on supposedly portable C
programs, he's going to want to run those programs on that machine,
tomorrow. Writing the programs correctly in the first place is far easier
than explaining to your boss why you didn't.


This last statement is true in a better world. joe
May 22 '06 #3

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

Similar topics

6
9145
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h> #include<string.h> #include<stddef.h> struct node
12
4081
by: Sweety | last post by:
plz reply, thanks in advance. bye,
8
431
by: Sweety | last post by:
hello, how to use macro to calculate the sizeof some data types. plz explain . thanks in advance. bye,
16
2693
by: Martin Roos | last post by:
hy ppl, i'm trying to create some multiplatformed software here and i'm very curious about the sizes of common variables on different machines. if you are running something different than a 32-bit x86 box under your desk, please check what this program outputs. //-- program starts -- #include <stdio.h> #define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));
10
3407
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
7
5668
by: rahul8143 | last post by:
hello, why following 2 codes does not give same result that is 10? 1) #include <stdio.h> #define DIM(array) sizeof(array)/sizeof(int) void main() { int arr; printf("The dimension of the array is %d", DIM(arr)); }
90
8468
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
11
4717
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing some more research, that what I really need is known in C as a "dynamic array". Basically, you surpass the array notation and use pointers with memory obtained with malloc() or calloc(). I think this will do just what I needed. That has brought...
5
2900
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof before runtime ? I also have: { void check_foo_size(void);
17
4110
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.