about sizeof(char)

  • Follow


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

0
Reply jose_de_paula (59) 1/26/2004 12:10:05 AM

"Jos� de Paula" <jose_de_paula@ig.com.br> wrote in message
news:pan.2004.01.26.00.10.03.394664@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


0
Reply tomstdenis3 (162) 1/26/2004 12:14:04 AM


"Jos� de Paula" <jose_de_paula@ig.com.br> wrote in message
news:pan.2004.01.26.00.10.03.394664@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


0
Reply mkwahler (3821) 1/26/2004 12:45:20 AM

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.

0
Reply E.Robert.Tisdale (2031) 1/26/2004 12:45:21 AM

"Jos� de Paula" <jose_de_paula@ig.com.br> wrote in message
news:pan.2004.01.26.00.10.03.394664@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


0
Reply airia (1802) 1/26/2004 12:50:09 AM

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

0
Reply E.Robert.Tisdale (2031) 1/26/2004 12:52:06 AM

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
0
Reply cottrell (169) 1/26/2004 1:40:29 AM

"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:401464B6.5050204@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


0
Reply mkwahler (3821) 1/26/2004 1:57:15 AM

On Sun, 25 Jan 2004 16:52:06 -0800, "E. Robert Tisdale"
<E.Robert.Tisdale@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/~ajo/docs/FAQ-acllc.html
0
Reply jackklein (3932) 1/26/2004 3:05:57 AM

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

0
Reply foo752 (203) 1/26/2004 4:43:21 AM

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 <jose_de_paula@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
0
Reply mambuhl (2201) 1/26/2004 5:22:33 AM

"Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
news:tu1Rb.25460$i4.13751@newsread1.news.atl.earthlink.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 <jose_de_paula@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


0
Reply mkwahler (3821) 1/26/2004 6:28:41 AM

In article <pan.2004.01.26.00.10.03.394664@ig.com.br>,
 Jos� de Paula <jose_de_paula@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.
0
Reply christian.bau (880) 1/26/2004 7:58:37 AM

In article <401464B6.5050204@jpl.nasa.gov>,
 "E. Robert Tisdale" <E.Robert.Tisdale@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.
0
Reply christian.bau (880) 1/26/2004 8:03:50 AM

"E. Robert Tisdale" <E.Robert.Tisdale@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
0
Reply rlb (4118) 1/26/2004 10:20:30 AM

In 'comp.lang.c', "Tom St Denis" <tomstdenis@iahu.ca> wrote:

> It should be

r/should/could
 
> char *ptr = malloc(sizeof(ptr[0]) * NUM);

-- 
-ed- emdelYOURBRA@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/
0
Reply emdelYOURBRA (457) 1/26/2004 9:33:01 PM

In 'comp.lang.c', "E. Robert Tisdale" <E.Robert.Tisdale@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- emdelYOURBRA@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/
0
Reply emdelYOURBRA (457) 1/26/2004 9:34:43 PM

On 26 Jan 2004 21:33:01 GMT
Emmanuel Delahaye <emdelYOURBRA@noos.fr> wrote:

> In 'comp.lang.c', "Tom St Denis" <tomstdenis@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.
0
Reply spam331 (4024) 1/28/2004 4:07:27 PM

17 Replies
35 Views

(page loaded in 0.196 seconds)

Similiar Articles:


















7/30/2012 2:42:54 AM


Reply: