473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what I miss?

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

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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 #1
18 1382
"Parahat Melayev" <pa*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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?


Nope. C isn't a char, it's a pointer (to a char).

Alex
Nov 15 '05 #2
oh yeah that is right :) panic

tnx

Nov 15 '05 #3


Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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?
what is c ????
c is the pointer which is going to hold the address which is of the
char type.

malloc will return the address of the allocated memory.
and so c holds the address, which is int.

c = malloc(sizeof(unsigned char));

HTH
ranjeet

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


Nov 15 '05 #4


Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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?


Not very. I know of systems where this program
would report both sizes as zero -- a good deal stranger,
don't you think?

(Hint: What is the type of the result of `sizeof',
and what is the type expected by the "%d" conversion?)

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

Nov 15 '05 #5
thanks but problem is not at malloc.
it must be,

printf("size of *c: %d\n", sizeof(*c));

Nov 15 '05 #6
In article <11**********************@f14g2000cwb.googlegroups .com>,
Parahat Melayev <pa*****@gmail.com> wrote:
thanks but problem is not at malloc.
it must be,

printf("size of *c: %d\n", sizeof(*c));


Glad to see you've solved your own problem. Congratulations!

Nov 15 '05 #7
Some minor nits...

Alexei A. Frounze wrote:
"Parahat Melayev" <pa*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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?


Nope. C isn't a char, it's a pointer (to a char).


No, C is a programming language (hint - case sensitivity!), and c is not a
pointer to a char, but a pointer to an unsigned char.

Usual stuff follows:

A) many people prefer a full prototype for main(), as in:
int main(void)

B) using the template p = malloc(n * sizeof *p), we could improve the malloc
to:
c = malloc(sizeof *c);
(ignoring n on this occasion, since we appear only to want one object).

C) sizeof yields a size_t, which is an unsigned integer type of unknown
size (in C90), so we will need to cast it. Unsigned longs are good
for this, so make that:

printf("size of unsigned char: %lu\n",
(unsigned long)sizeof(unsigned char));

(note that this is now required to write 1 on stdout), and

printf("size of c: %lu\n", (unsigned long)sizeof c);

Superfluous parentheses removed. Note the updated format specifiers.

Did I miss anything?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #8
ra***********@gmail.com wrote:

Parahat Melayev wrote:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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?


what is c ????
c is the pointer which is going to hold the address which is of the
char type.

malloc will return the address of the allocated memory.
and so c holds the address, which is int.


The address is not int.
A pointer type is different from an int type.

--
pete
Nov 15 '05 #9
On 2005-07-15 09:24:30 -0500, "Parahat Melayev" <pa*****@gmail.com> said:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned char));
printf("size of unsigned char: %d\n", sizeof(unsigned char));
The size of an unsigned char is 1.

printf("size of c: %d\n", sizeof(c));
The size of a pointer is 4.

The size of the memory handled by the pointer is 1.

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) )


I suggest reading a basic C book. You must know the difference between
a variable and a pointer.

PS. Sizes of vars and ptrs may vary on different platforms.

--
Sensei <se******@tin.it>

cd /pub
more beer

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

int main()
{
unsigned char *c;
c = malloc(sizeof(unsigned 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(unsigned 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***@YOURBRAnoos.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_Keith) 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***@YOURBRAnoos.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***@YOURBRAnoos.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***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.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_Keith) 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.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :
"Emmanuel Delahaye" <em***@YOURBRAnoos.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.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAnoos.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.org> wrote in message
news:ln************@nuthaus.mib.org...
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
Keith Thompson wrote on 15/07/05 :

"Emmanuel Delahaye" <em***@YOURBRAnoos.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
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...
9
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...
19
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...
1
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:...
7
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...
98
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
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...
8
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...
21
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...
0
by: manikandan | last post by:
dont miss it just open dont miss it just open dont miss it just open #############################
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.