473,385 Members | 1,486 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,385 software developers and data experts.

about sizeof(char)

Is it a given that sizeof(char) always yields 1, no matter the
implementation? I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);

--
Quidquid latine dictum sit altum viditur

Nov 14 '05 #1
17 9439

"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
Is it a given that sizeof(char) always yields 1, no matter the
implementation? I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);


It should be

char *ptr = malloc(sizeof(ptr[0]) * NUM);

Tom
Nov 14 '05 #2
"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
Is it a given that sizeof(char) always yields 1, no matter the
implementation?

Yes.
ISO/IEC 9899:1999(E)

6.5.3.4 The sizeof operator

3 When applied to an operand that has type char, unsigned char,
or signed char, (or a qualified version thereof) the result
is 1. When applied to an operand that has array type, the result
is the total number of bytes in the array. When applied to an
operand that has structure or union type, the result is the total
number of bytes in such an object, including internal and trailing
padding.
I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);


Yes, imo the 'sizeof(char)' is simply unnecessary clutter.

But I don't like either one of those. I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)

-Mike
Nov 14 '05 #3
"José de Paula" <jo***********@ig.com.br> wrote in message
news:pa****************************@ig.com.br...
Is it a given that sizeof(char) always yields 1, no matter the
implementation?
So long as the implementation claims conformance, yes.
I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);


char *ptr = malloc(NUM * sizeof *ptr);

It's a style issue.

sizeof(T) has potentially more problems than sizeof *P, where P
is a pointer to T. Consider the case where the pointer type
might change. Similarly, a raw malloc(NUM) has the same problem,
consider if you might ever change ptr to type wchar_t *.

--
Peter
Nov 14 '05 #4
José de Paula wrote:
Is it a given that sizeof(char) always yields 1,
no matter the implementation?
I ask because I saw some critics against char* ptr = malloc(sizeof(char)*NUM); in favor of simply char* ptr = malloc(NUM);


It is a matter of style. I prefer

char* ptr = malloc(sizeof(char)*NUM);

simply because it is consistent with the normal pattern

T* ptr = malloc(sizeof(T)*NUM);

where T is any other type besides [un]signed char.

Nov 14 '05 #5
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)


Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));

Nov 14 '05 #6
E. Robert Tisdale wrote:
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)

Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


Anyone ever wondered why he's commonly referred to as 'Trollsdale'
in this newsgroup?

--
Allin Cottrell
Department of Economics
Wake Forest University, NC
Nov 14 '05 #7

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov...
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)


Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


I certainly do not consider casting 'malloc()'s
return value to be 'better'.

About the typedef: Not needed if you use what
I recommended: sizeof *ptr


-Mike
Nov 14 '05 #8
On Sun, 25 Jan 2004 16:52:06 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)


Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


There you go, wetting your pants in public again.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #9
Mac
On Sun, 25 Jan 2004 16:45:21 +0000, E. Robert Tisdale wrote:
José de Paula wrote:
Is it a given that sizeof(char) always yields 1,
no matter the implementation?
I ask because I saw some critics against
char* ptr = malloc(sizeof(char)*NUM);

in favor of simply

char* ptr = malloc(NUM);


It is a matter of style. I prefer

char* ptr = malloc(sizeof(char)*NUM);


I would prefer this:
char* ptr = malloc(sizeof *ptr * NUM);

On this newsgroup, it is typical for people to do this with types other
than char, and I think it is acceptable when done as above, also. It
guards against the case where the type of ptr is changed for some reason.

simply because it is consistent with the normal pattern

T* ptr = malloc(sizeof(T)*NUM);

where T is any other type besides [un]signed char.


Mac

Nov 14 '05 #10
E. Robert Tisdale wrote:
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)

Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


To the OP <jo***********@ig.com.br>: As a rule of thumb, ignore *anything*
posted by E. Robert Tisdale. Some recent posts from him have raised the
expectation that he might be becoming a literate C programmer, but he
dashed these hopes almost immediately. Extraneous casting -- especially
when it can hide errors -- is stupid; gratuitous typedefs are just a waste
of time, usually meant to make maintenance as difficult as possible.

--
Martin Ambuhl
Nov 14 '05 #11

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:tu******************@newsread1.news.atl.earth link.net...
E. Robert Tisdale wrote:
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)

Except to the insecure.
Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));

To the OP <jo***********@ig.com.br>: As a rule of thumb, ignore *anything*
posted by E. Robert Tisdale. Some recent posts from him have raised the
expectation that he might be becoming a literate C programmer, but he
dashed these hopes almost immediately.


Yes, I think of him as "Mr. YoYo".
Extraneous casting -- especially
when it can hide errors -- is stupid;
gratuitous typedefs are just a waste
of time, usually meant to make maintenance as difficult as possible.


That's called 'job security', dontcha know? :-)

-Mike
Nov 14 '05 #12
In article <pa****************************@ig.com.br>,
José de Paula <jo***********@ig.com.br> wrote:
Is it a given that sizeof(char) always yields 1, no matter the
implementation? I ask because I saw some critics against

char * ptr = malloc (sizeof(char) * NUM);
in favour of simply
char * ptr = malloc (NUM);


sizeof (char) is absolutely always equal to 1.

Multiplying by sizeof (char) is redundant. But sometimes it is the right
thing to write code that is redundant if it makes more clear what you
mean.

"char" is often used as an integer type in its own right, in places
where you would use "short short int" if such a type existed, and then
multiplying by sizeof (char) would be the right thing to do. If you want
to allocate n elements of type X then you call malloc (n * sizeof (X)).
For example

// I need three arrays of hundred elements to store data
char* array1 = malloc (100 * sizeof (char));
short* array2 = malloc (100 * sizeof (short));
double* array3 = malloc (100 * sizeof (double));

But quite often "char" is used just as the unit of storage; you
calculate a number of bytes and then you allocate that many bytes.
Multiplying (number of bytes) by (sizeof (char)) seems to indicate that
the author doesn't quite understand what is going on.
Nov 14 '05 #13
In article <40**************@jpl.nasa.gov>,
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
Mike Wahler wrote:
I recommend this way:

char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)


Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


In a post thirty seconds ago I accussed you of giving ridiculous advice
to newcomers...

I hadn't read this post at that time, but what a confirmation.
Nov 14 '05 #14
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
José de Paula wrote:
Is it a given that sizeof(char) always yields 1,
no matter the implementation?
I ask because I saw some critics against

char* ptr = malloc(sizeof(char)*NUM);

in favor of simply

char* ptr = malloc(NUM);


It is a matter of style. I prefer

char* ptr = malloc(sizeof(char)*NUM);


Since when? You normally advocate something equivalent to

#define mytype char
char* ptr = (void* )(mytype *)(void * ) malloc(sizeof(char)* NUM);

Richard
Nov 14 '05 #15
In 'comp.lang.c', "Tom St Denis" <to********@iahu.ca> wrote:
It should be
r/should/could
char *ptr = malloc(sizeof(ptr[0]) * NUM);


--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #16
In 'comp.lang.c', "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
char *ptr = malloc(NUM * sizeof *ptr);

This makes the statement still work if the pointer type is
later changed. Less maintenance is always a Good Thing(tm) :-)


Better yet

typedef char type;
type* ptr = (type*)malloc(NUM*sizeof(type));


Better in what? More typing? More keyboards sales?

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #17
On 26 Jan 2004 21:33:01 GMT
Emmanuel Delahaye <em**********@noos.fr> wrote:
In 'comp.lang.c', "Tom St Denis" <to********@iahu.ca> wrote:
It should be


r/should/could


Well, what was posted was almost the form recommended by a lot of the
knowledgeable regulars around here.
char *ptr = malloc(sizeof(ptr[0]) * NUM);


char *ptr = malloc(NUM * sizeof *ptr);

is shorter and, to me, more obvious.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.
Nov 14 '05 #18

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

Similar topics

5
by: nsgi_2004 | last post by:
I have a C book that says the sizeof a char is one byte. Is this true for C++?
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
5
by: LaBird | last post by:
Dear all, I'd like to ask for the following C code segment: #include <stdio.h> int main() { char *a = "abc"; /* Line 1 */ printf("%s", a); /* Line 2 */ a = '1'; /* Line...
9
by: Sunner Sun | last post by:
Hi, all FAQ of comp.lang.c said "In C, type char is defined as occupying one byte, so it is usually 8 bits". (http://www.eskimo.com/~scs/cclass/notes/sx2a.html) But I found in C99 that "An...
0
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
1
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); ...
2
by: arnaudk | last post by:
Why is sizeof(char*) = 4? I would have expected 1... Is it because a pointer is stored as an int ?
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.