473,480 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create 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 2224
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*********@esat.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...@esat.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...@esat.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
19454
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...
18
2146
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
3149
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?...
12
10667
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...
39
7977
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...
23
4836
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...
318
12692
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...
73
7332
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...
89
5637
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...
27
2314
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...
0
7046
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,...
0
6908
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...
0
7048
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,...
1
6741
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...
0
4485
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...
0
2997
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...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...
0
183
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...

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.