473,545 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size_t in c++?

In a file I have made:

size_t bb;
bb = 3u;
printf("bb %d\n");

which prints:

bb 2280640

But what is 'u' and why does it print the above number?
Aug 1 '08 #1
7 2226
saneman wrote:
In a file I have made:

size_t bb;
bb = 3u;
printf("bb %d\n");

which prints:

bb 2280640

But what is 'u' and why does it print the above number?

Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);

ismo
Aug 1 '08 #2
saneman wrote:
In a file I have made:

size_t bb;
bb = 3u;
printf("bb %d\n");

which prints:

bb 2280640

But what is 'u' and why does it print the above number?
'u' is a suffix to make your literal *unsigned*. It prints what you see
by *pure chance*. You provided the format specification (%d) but no
corresponding argument, so the behaviour of your program is undefined,
it can do whatever it wants. Search the web for "nasal demons" (with
quotes).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '08 #3
In article <8U************ *@read4.inet.fi >, Ismo Salonen
<no****@another .invalidwrote:
saneman wrote:
size_t bb;
bb = 3u;
printf("bb %d\n");
...

Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);
That prints 0 on one of my machines (big-endian, 16-bit int). %d specifies
an int, but size_t isn't necessarily an (unsigned) int. Since the subject
and group is C++, not C, the way to print it is

std::cout << bb;

If printf must be used for some reason, cast bb to an int, or better, use
unsigned long:

printf( "bb %lu\n", (unsigned long) bb );
Aug 1 '08 #4
Hi

Ismo Salonen wrote:
saneman wrote:
>In a file I have made:

size_t bb;
bb = 3u;
printf("bb %d\n");

Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);
Even then, %d and size_t don't match. Use %zu for size_t, as it might be
larger than an int.

Markus

Aug 1 '08 #5
On 2008-08-01 09:17:53 -0400, Markus Moll
<ma*********@es at.kuleuven.ac. besaid:
Hi

Ismo Salonen wrote:
>saneman wrote:
>>In a file I have made:

size_t bb;
bb = 3u;
printf("bb %d\n");

Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);

Even then, %d and size_t don't match. Use %zu for size_t, as it might be
larger than an int.
Well, yes, non-portably. "%zu" is new with C99, and the C++ standard is
based on C90. The next version of the standard will provide "%zu".

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 1 '08 #6
On Aug 1, 6:17*pm, Markus Moll <markus.m...@es at.kuleuven.ac. be>
wrote:
Hi

Ismo Salonen wrote:
saneman wrote:
In a file I have made:
size_t bb;
bb = 3u;
printf("bb %d\n");
Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);

Even then, %d and size_t don't match. Use %zu for size_t, as it might be
larger than an int.

Markus
excuse me Markus whats %zu? whats the z stand for?
Aug 2 '08 #7
Anarki wrote:
On Aug 1, 6:17 pm, Markus Moll <markus.m...@es at.kuleuven.ac. be>
wrote:
>Hi

Ismo Salonen wrote:
>>saneman wrote:
In a file I have made:
size_t bb;
bb = 3u;
printf("bb %d\n");
Prints garbage from stack, there is no argument given for %d.
Should be : printf("bb %d\n",bb);
Even then, %d and size_t don't match. Use %zu for size_t, as it might be
larger than an int.

Markus

excuse me Markus whats %zu? whats the z stand for?
It's the C99 format specifier for size_t.

--
Ian Collins.
Aug 2 '08 #8

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

Similar topics

11
19470
by: javadesigner | last post by:
Hi: I am a bit new to C programming and am trying to write a wrapper around malloc. Since malloc takes size_t as it's parameter, I want to be able to see if my function recieved an argument less than or equal to the max size_t type. So:
18
2153
by: rayw | last post by:
I used to believe that size_t was something to do with integral types, and the std. Something along the lines of .. a char is 8 bits, a int >= a char a long >= int
5
3153
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct? Does this mean that if I need to compare two variables, one of the size_t type, should the other also be a size_t variable? There could be...
12
10681
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
39
7995
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using it otherwise is confusing. I also thought that size_t could be signed but it seems I was wrong on that one. So if you were to see code iterating...
23
4853
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about wasted space, you can minimize the effects of padding by ordering the members of a structure based on their base types, from largest to smallest."
318
12759
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people started to try to find possible errors, a harmless passtime they seem to enjoy. One of their remarks was that I used "int" instead of "size_t"...
73
7358
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
89
5657
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
27
2316
by: mike3 | last post by:
Hi. I can't believe I may have to use an array here. I've got this bignum package I was making in C++ for a fractal generator, and tried an approach that was suggested to me here a while back, about using a "stack of vectors" instead of a static array. Anyway, I put the suggestion into effect, and it seems the routine that uses the...
0
7487
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...
0
7420
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...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
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...
0
7778
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...
0
6003
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...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
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...
0
731
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...

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.