473,796 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what I miss?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(u nsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )

Nov 15 '05
18 1426
Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(u nsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char)); ^^
You have no reason to think that the size_t that sizeof() yields is a
signed int, as '%d' requires. In fact, we know that it is not, since a
size_t is by definition unsigned, and will often be of a wider type than
int.
printf("size of c: %d\n", sizeof(c));
return 0;
} When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?


No. c is a pointer-to-unsigned-char. Do you, for some reason, imagine
that a pointer-to-T should have the same size as a T?
Nov 15 '05 #11
Parahat Melayev wrote on 15/07/05 :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(u nsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
printf("size of c: %d\n", sizeof(c));
return 0;
}

When I execute this, it says that size of "unsigned char" is "1" & size
of "c" is "4". isn't that strange?

( gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) )


It's fine. c is a pointer. The sizeof of a pointer can be 4 (actually,
it's implementation-dependent).

Note that sizeof returns a size_t. You should use "%zu" as a formatter.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 15 '05 #12
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.

A reasonably safe way to print a size_t value, compatible with both
C90 and C99, is:

printf("%lu", (unsigned long)sizeof foo);

The only case where this can fail is if sizeof foo exceeds ULONG_MAX,
which can only happen if size_t is bigger than unsigned long, which
can't happen in C99. Even then, it can only happen if foo is a huge
object, 4 gigabytes or bigger.

If you're worried about that possibility, you can use the preprocessor
to check for __STDC_VERSION_ _ >= 199901L.

--
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 15 '05 #13
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 15 '05 #14
Emmanuel Delahaye wrote:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.

You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"


So what? The OP uses gcc on a Linux system and thus probably has an
appropriate glibc to do the trick.
I can have gcc 4.0.0 on my cygwin under windows yet the runtime
library still is upset about "%zu"...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #15
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a formatter.


You should use "%zu" only if you can depend on your runtime library to
implement it. "%zu" is a new feature in C99. Even if your compiler
supports C99 (or most of it), the runtime library may not.


From the OP : "gcc version 4.0.0"


That provides no clue about whether the runtime library supports "zu".

I have gcc 4.0.0 on three different systems. On one of them, the
following program:

#include <stdio.h>
int main(void)
{
size_t s = 42;
printf("s = %zu\n", s);
return 0;
}

prints "s = 42"; on the other two, it prints "s = zu". (I'd mention
which systems they are if it were relevant.)

--
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 15 '05 #16
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]
Note that sizeof returns a size_t. You should use "%zu" as a

formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.

Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?

Alex
Nov 15 '05 #17
Alexei A. Frounze wrote:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]

>Note that sizeof returns a size_t. You should use "%zu" as a

formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.


ITYM "unsigned long"/"%lu" -- this is the best bet under C89.
Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?


AFAIK, no.
There are length modifiers for ptrdiff_t("t"), intmax_t("j"),
char("hh"), long long("ll") and a conversion specifier for hex
fractions/floating point representation ("a","A").

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #18


Alexei A. Frounze wrote:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> writes:
[...]

>Note that sizeof returns a size_t. You should use "%zu" as a


formatter.

Wow, there's a new type specifier for size_t in *printf()? Great. I used to
use long for it and to make sure it's indeed long was casting to it.

Btw, how about pointers to functions and pointers to data? The two can be
different types (I mean not the C's idea about the types but rather the
effective size in bits of them). Are there any special specifiers for each?


Pointers to data can be converted to `void*' and printed
with "%p", just as in C89. Pointers to functions cannot be
printed except via subterfuges like printing the values of
their constituent bytes.

--
Er*********@sun .com

Nov 15 '05 #19

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

Similar topics

3
2248
by: F. GEIGER | last post by:
When I start a py2exe-ed application I get the error 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128) This is how I run py2exe: setup.py py2exe -O1 --packages encodings This is how the .po-file looks like:
9
13462
by: Nadav | last post by:
Hi, I am tring to pass messages between threads, using the good old C++ I would call the GetMessage/PostThreadMessage APIs, Now, I am using C# and i can't find any equivalenty for these calls, any Idea how to communicate between threads and access the threads message queue will be appriciated... -- Nadav http://www.ddevel.com
19
2900
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business layer, and the business layer is coupled to the data layer. So far so good. Suppose the data layer raises an event, and it passes Me (the sender) as an object, and e (MyEventArgs, a descendent of EventArgs) to the layer above (the business...
1
1532
by: kmounkhaty | last post by:
Hi Guru, My profiler trace does not display SP:CACHEMISS event, even thought I drop store proc, clear both data cache and buffer cache but still does not work. Every thing works fine like: cachehit, cacheinsert,cacheremove,executecontexthit etc... Is there any special option that I need to turn it on?
7
1212
by: jg | last post by:
I setup a new windwos application project but I found out the sub main was not called, I tried various form of Main (functions, subs) still no luck. What did I miss? Public Class SolverForm Dim icnt_pieces = 0, iCurrent_Row = 0, iExpected_pieces = 16, i As Integer Dim ipb_piece() As PictureBox Dim str_label() As String Dim chr_Atrribute(iExpected_pieces)() As Char
98
4628
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
28
1729
by: Useful Info | last post by:
Like on 9/11, the Federal Government apparently WANTED people to die at the hands of Cho at VA Tech, because they told campus police not to pursue Cho after the double homicide occurred. Story via http://Muvy.org
8
12913
by: anukedari | last post by:
Hi, Could any boby please help to get the answers for the following questions: Is Apache always sends "X-Cache:MISS" header even when caching is off (disable)? or Can we say that cache settings are enable if it sends "X-Cache:MISS" header in the response? Your help would be appreciated.
21
35541
by: Ram Prasad | last post by:
I am trying to write a simple libspf2 plugin code for my postfix ( milter) I am getting this unhelpful error message when I try to compile gcc -g1 -Wall -I/usr/local/include/spf2 -I. -c mfunc.c In file included from mfunc.c:1: mfunc.c:42: error: expected ')' before '*' token make: *** Error 1 my mfunc.c has on the line 42
0
1400
by: manikandan | last post by:
dont miss it just open dont miss it just open dont miss it just open #############################
0
9680
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
10455
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...
0
10228
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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
9052
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
6788
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();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.