about sizeof(char)

  • Thread starter =?iso-8859-1?q?Jos=E9_de_Paula?=
  • Start date
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

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

Tom St Denis

José de Paula said:
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
 
M

Mike Wahler

José de Paula said:
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
 
E

E. Robert Tisdale

José de Paula said:
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.
 
P

Peter Nilsson

José de Paula said:
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 *.
 
E

E. Robert Tisdale

Mike said:
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));
 
A

Allin Cottrell

E. Robert Tisdale said:
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?
 
M

Mike Wahler

E. Robert Tisdale said:
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
 
M

Mac

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
 
M

Martin Ambuhl

E. Robert Tisdale said:
Better yet

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

To the OP <[email protected]>: 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.
 
M

Mike Wahler

Except to the insecure.
To the OP <[email protected]>: 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
 
C

Christian Bau

José de Paula said:
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.
 
C

Christian Bau

"E. Robert Tisdale said:
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.
 
R

Richard Bos

E. Robert Tisdale said:
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
 
E

Emmanuel Delahaye

E. Robert Tisdale said:
Better yet

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

Better in what? More typing? More keyboards sales?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,344
Latest member
KamCrowthe

Latest Threads

Top