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: Problem sending binary file via socket. - comp.unix.programmer ...Hi all, My client code are: send(fd, argv[2], strlen(argv[2]) + 1, 0); fp = fopen(argv[2], "rb"); while (bytes_read = fread(buf, sizeof(char), MAX_D... Size of String Tables - comp.lang.cI want to go through a string table (char **str). Every string in it has a different size. My idea for getting the size was size = sizeof(str)/sizeo... Compiler for 16bit architecture - comp.compilers.lccThe obvious answer for a word addressable target (as Jacob posted) is to set sizeof(char)=sizeof(int)...=1, but this makes access to packed char arrays (e.g. strings ... How to send binary or hex data in Socket using C ? - comp.unix ...... ptr++; return(ptr);} then you can fill a byte buffer with the three integer values: unsigned char bytes[sizeof(words)]; unsigned char* ptr=bytes; for(i=0;i<sizeof ... Getting scsi serial number of a device in Solaris - comp.unix ...... if (s1!=NULL && *s1!='\0') { > write(2,s1,strlen(s1)); > write(2,colon_space,sizeof colon_space); > } > perror(s2); > } > > int > main(int argc, char ... read() error - Bad Address - comp.unix.programmerSorry, it was sizeof(char). And I'll keep that second point in mind. > mesg currently points to the literal string you just assigned to it. > Many systems use write ... SOLUTION: compile time array size using type only - comp.lang.c++ ...... const char (&_bounds_fn(const T (&)[N]))[N]; static const T * _arr; public: enum { value = sizeof(_bounds_fn(*_arr)) }; }; int length = cx_array_length<char[3 ... Calculating member offset at runtime - comp.lang.c++.moderated ...return(msg - msg_); } void unpack(char *msg) { Fieldset fs; memcpy(&fs, msg, sizof(fs)); msg += sizeof(fs); #undef X ... Timestamp Conversion - comp.protocols.time.ntp... struct NTP { unsigned char LI_VER_MODE; unsigned char STRATUM; unsigned char POLL ... Problem in Creating Socket\n"); return iFailure; } bzero(&clientSock,sizeof ... Overloading operators new and delete - comp.lang.c++.moderated ...void *CH::operator new(size_t size) { return new char[size]; } void CH::operator ... code: > > void * > > CLASS_A::operator new( size_t size ){ > > return Stack<sizeof ... portability of integral types in C++ on UNIX - comp.unix ...... include <string.h> #define SIGNED 1 #define UNSIGNED 0 char * getMatchingType(int bytes, int signed_type) { if (signed_type) { if (sizeof(signed char ... Only number input thru scanf() - comp.lang.c... 3D KEY_ESCAPE) ? kbesc() : c; } char *inputfn(size_t size, int (*fn)(int)) { int c, pos =3D 0, len =3D 0; char *entry; entry =3D calloc(size + 1, sizeof(char ... Problem with gettimeofday() function - comp.unix.programmer ...... UDP class instance. > ofi_net_udp *udp_recv = new ofi_net_udp(); > > //Allocate memory for recv packet buffers. > buf = (char*)malloc(buflen*sizeof(char ... Newbie cache alignment question - comp.lang.c++.moderated ...... get an instance of this that's 32-byte aligned, you will need to do something along these lines: enum { ALIGNMENT = 32, MASK = ALIGNMENT - 1 }; char buf[sizeof ... An example multithreading puzzle - comp.programming.threads ...... to execute this code for(int i = 0; i < MAX; ++i) arr[i] = arr[i] / normalize1[i % 64]; Then, Part 2 { char modulo[64]; memzero(modulo, 64*sizeof(char ... sizeof(char) is 1 « code monk2007-04-08 at 21:32:51. Just conceivably, and trying to look on these code fragments as generously as possible, some of these programmers know what they ... Talk:Sizeof - Wikipedia, the free encyclopediaIn the above example, since the size of the argv array is indeterminate, sizeof(argv) will be equivalent to sizeof(char **) — in other words, the size of the pointer type ... 7/30/2012 2:42:54 AM
|