473,732 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof stuff and declarations

I have two questions. I hope they are about C. I chose to post
here instead of comp.unix.progr ammer (or some other) because I do
think this is a C question.

Question 1:

%cat float.c
main() {
float **m = malloc(5 * sizeof(float*)) ;
m[0] = malloc( sizeof(float) );
m[0][0] = 1.2;
printf("%f\n",m[0][0]);
}

Is this legal? I never really declare and assign so fast, but I
was wondering if it works because I'm lucky or if it really is
legal. Gcc doesn't like it much.

%gcc -c float.c
float.c: In function `main':
float.c:2: warning: initialization makes pointer from integer
without a cast float.c:3: warning: assignment makes pointer from
integer without a cast

Question 2:

(gdb) p sizeof(int)
$1 = 4
(gdb) p sizeof(long)
$2 = 4

I was expecting to see long a little bigger.
Why are they the same size?

Thank you.
Nov 14 '05
12 1265
On 18 Mar 2005 01:24:10 GMT,
Chris Torek <no****@torek.n et> wrote:
[on machine X, "sizeof(int )" and "sizeof(lon g)" are the same]
On Thu, 17 Mar 2005 15:03:23 -0800,
Andrey Tarasevich <an************ **@hotmail.com> wrote:
Why not? This is perfectly legal in C. 'long' is notrequired> to be bigger than 'int'.


In article <20************ *************** @hotmail.com>
Kevin <ke***@hotmail. com> wrote:
But if they take the same amount of bytes, then how is it
possible that long can take bigger numbers?


Maybe it can't -- on machine X anyway.


Right.

An analogy might help. Suppose the motor-vehicle
administration( DMV or MVA or whatever it is called where you
are) says that you need a different kind of license to drive a
motorcycle vs a car vs a tractor-trailer. You only have a
"regular car" license. Suppose also that they say "a car must
hold two or more people side by side". If your vehicle holds
eight people, two in front seats and three in second and third
row seats, is it a car? (What if it holds two or so people but
can pull a triple trailer? :-) )
To be honest, I'm not sure how to answer, but here's my attempt:

Question 1: Is it a car? Yes. Because to be a car, must hold
two or more people side by side. My vehicle holds eight people
such that:

(a) two in front seats (respects the rule)
(b) three in second row (respect the rule: ``or more'')
(c) three in third row (respect the rule: ``or more'')

Therefore it's a car. I assume that the seats in each row are
really ``side by side''.

Question 2: What if it can pull a triple trailer?

I don't know what a triple trailer is... but as long as your car
respects the rule above, then it's a car. If a car couldn't pull
something, then it should be specified what it can't do. Is isn't
specified, so we are not breaking the rule.

I hope I got it :-)

The C standard says that an "int" variable must hold, at the
least, values in the range -32767 to +32767, and "long" has to
cover at least -2147483647 to +2147483647. If your "int" and
"long" both happen to hold values in the range -2147483648 to
+2147483647, is that sufficient?
Yes, it's sufficient. More than sufficient actually. It's
necessary for an int to allow -32767 to +32767, if you allow
more, than it's all good. Right?

It is not unusual for a standard (or a law) to set some sort of
"least quality required" limit, and for people to exceed that,
for whatever reason.


Yeah, I think I understand it now... I hope I do :-)

Thank you.
Nov 14 '05 #11
On modern systems, it's very common either for short and int to be the
same size, or for int and long to be the same size.

Roughly speaking:
char has to be at least 8 bits
short has to be at least 16 bits (and at least as wide as char)
int has to be at least 16 bits (and at least as wide as short)
long has to be at least 32 bits (and at least as wide as int)

There's often an advantage in having predefined integer types of sizes
8, 16, and 32 bits. Without defining additional types, there are
several ways to do this (assuming all sizes are powers of 2):
char 8 bits
short 16 bits
int 16 bits
long 32 bits
or
char 8 bits
short 16 bits
int 32 bits
long 32 bits
or
char 8 bits
short 16 bits
int 32 bits
long 64 bits

You're likely to see the latter only on 64-bit systems.

Even on 64-bit systems, int is likely to be only 32 bits, because
making it 64 bits leaves a hole in the type system (either there's no
16-bit type, or there's no 32-bit type); Cray systems, however, have
64-bit ints anyway.

None of this is guaranteed, and there's seldom any great need to
assume that it is. You can write code that doesn't make any
assumptions beyond what's guaranteed by the standard.

C99 introduces typedefs for exact-width types: int16_t, int32_t,
int64_t, and so forth.

(Above, I wrote "Roughly speaking" because what the standard
guarantees isn't these sizes. Instead, it guarantees certain
representable ranges; either -127..+127 or 0..255 for char,
-32767..+32767 for short and int, -2147483647..+21 47483647 for long.
These usually translate to the specified number of bits, but extra
padding bits are permitted. With padding bits, it's even possible to
have sizeof(short) > sizeof(int), but I don't think any real
implementations do this. Also, for a 16-bit type, the range is more
likely to be -32728..+32767 rather than -32767..+32767; the standard's
minimum ranges allow for sign-magnitude and one's-complement
representations , but two's-complement is much more common these days.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #12
This is off-topic but what the heck... :-)
On 18 Mar 2005 01:24:10 GMT,
Chris Torek <no****@torek.n et> wrote:
An analogy might help. ... motorcycle vs a car vs a tractor-trailer. ...
... is it a car? (What if it holds two or so people but
can pull a triple trailer? :-) )

In article <20************ *************** @hotmail.com>
Kevin <ke***@hotmail. com> wrote [in part]:I don't know what a triple trailer is... but as long as your car
respects the rule above, then it's a car.
If you are familiar with "18-wheelers" at all, you know what a
tractor cab is (e.g., a Peterbilt or a Mack). These tow trailers.
Trailers can be doubled-up or even tripled-up, so that one cab is
pulling three trailers. Some states put limits on the train-length;
I believe triples are outlawed in all of Caliornia, for instance.
If a car couldn't pull something, then it should be specified what
it can't do. Is isn't specified, so we are not breaking the rule.

I hope I got it :-)


Pretty much, yes. Sometimes the specifications are a bit lacking.
There is an enormous pickup truck (that does qualify as a "pickup
truck", despite being 21.5 feet long and 9 feet high) that I suspect
could pull trailers, and hence cross the somewhat fuzzy line between
"ordinary car" drivers' licenses and commercial truck drivers'
licenses. Of course, Hummers are already so heavy (over 6000 pounds
"gross vehicle weight") that they are illegal on most California
suburban streets -- but good luck finding anyone to enforce that
when Arnold Schwarzenegger is out for a drive. :-)

(The truck -- the International CXT -- has a GVWR of 25,999 lbs,
which is just under the line for requiring a Class B license.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #13

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

Similar topics

70
8876
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
7
4635
by: nzanella | last post by:
Hello, I just thought I would share the following observation with the rest of the group. The sizeof operator seems to act differently according to whether the number of elements in the array is known. Hence when passing arrays to functions the number of elements in the array must always be passed as an argument. If STL is available then people can just use vectors of course. Anyways, I guess this stuff is pretty standard. Well, have a...
7
1933
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
12
2412
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
9
6080
by: CptDondo | last post by:
I am missing something about structure declarations.... I am trying to get the size of a structure member using sizeof. my xml.h file (beware of line wrap): struct fieldSchedule_t { uint8_t action; uint16_t fromBearing, toBearing; };
40
3650
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long int) ? Same question for the corresponding unsigned types.
12
3641
by: aarklon | last post by:
Is y >(8 * (sizeof(int) -1)) portable expression to find The MSB of an unsigned integer y ?? will this work in all the cases, all three of the representations C allows: 1) two's complement 2) ones'complement 3) signed magnitude.
27
5601
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
2
1618
by: Fraser Ross | last post by:
class A{}; void func() { sizeof ( A (A()) ); sizeof ( ( A (A()) ) ); sizeof ( A ((A())) ); } The first operand is a type-id of a function. It doesn't compile with Comeau which is fine.
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9445
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
9234
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
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3259
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
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
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.