473,411 Members | 2,186 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,411 software developers and data experts.

Which style is better?


#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );

or

(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I think the (2) is better, because sometimes I have to change the type

of array, I just change the declaration of array, the malloc will get the

right size without change.

--
|
 ___
(-_-)
<¡ä¡ä>¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w ªÅ´ßªª³õ shepjeng.twbbs.org ¢w¢w¢w
¡þ plum.cs.nccu.edu.tw
Nov 13 '05 #1
8 1628
In article <48********@shepjeng.twbbs.org>, ³á³á³á³á wrote:
#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );
(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I think the (2) is better, because sometimes I have to change the type
of array, I just change the declaration of array, the malloc will get the
right size without change.

Both alternatives are perfectly valid.

If you know you have to change the type of the object, then use
the second alternative, of course, that's why the syntax is
allowed.

If you know for sure that you won't, then use the first.

--
Andreas Kähäri
Nov 13 '05 #2
> #include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );

or

(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I think the (2) is better, because sometimes I have to change the type

of array, I just change the declaration of array, the malloc will get the
right size without change.


A different solution could be to use a typedef to define the type of your
array.

Regards,
Nathan
Nov 13 '05 #3
Andreas Kahari wrote:

In article <48********@shepjeng.twbbs.org>, ³á³á³á³á wrote:
#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );
(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I think the (2) is better, because sometimes I have to change the type
of array, I just change the declaration of array, the malloc will get the
right size without change.


Both alternatives are perfectly valid.

If you know you have to change the type of the object, then use
the second alternative, of course, that's why the syntax is
allowed.

If you know for sure that you won't, then use the first.


I always use the second way,
without even thinking about it.

--
pete
Nov 13 '05 #4
Nathan wrote:
#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );

or

(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I think the (2) is better, because sometimes I have to change the type

of array, I just change the declaration of array, the malloc will get

the

right size without change.


A different solution could be to use a typedef to define the type of your
array.


Different, but suboptimal. Always use the second form. That way, you
never have to worry about the type of array.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 13 '05 #5
[unprintable] wrote:

#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );

or

(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.
I disagree.
I think the (2) is better


I agree.

Case closed.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6
Richard Heathfield wrote:
[unprintable] wrote:

#include <stdlib.h>
int *array;

I want to use malloc.

(1) array = malloc( sizeof( int ) * size );

or

(2) array = malloc( sizeof( *array ) * size );

My book use the (1) style, it has more clarity.

I disagree.

I think the (2) is better

I agree.

Case closed.

But it would be better to write it as:

array = malloc( sizeof *array * size );

as the parentheses are not necessary when using the `sizeof'
operator on an object (as opposed to a fully parenthesized type).

HTH,
--ag
--
Artie Gold -- Austin, Texas

Nov 13 '05 #7
Artie Gold wrote:

<snip>

But it would be better to write it as:

array = malloc( sizeof *array * size );

as the parentheses are not necessary when using the `sizeof'
operator on an object (as opposed to a fully parenthesized type).


Oh, absolutely. I only said (2) was better, not that it was perfect. :-)

(A review of my articles on the subject will, I think, bear this out.)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #8
Richard Heathfield wrote:
Artie Gold wrote:

<snip>
But it would be better to write it as:

array = malloc( sizeof *array * size );

as the parentheses are not necessary when using the `sizeof'
operator on an object (as opposed to a fully parenthesized type).

Oh, absolutely. I only said (2) was better, not that it was perfect. :-)

(A review of my articles on the subject will, I think, bear this out.)

Of course. 'Twas posted for benefit of the OP.

--ag
--
Artie Gold -- Austin, Texas

Nov 13 '05 #9

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

Similar topics

34
by: Mark Moore | last post by:
It looks like there's a pretty serious CSS bug in IE6 (v6.0.2800.1106). The HTML below is validated STRICT HTML 4.01 and renders as I would expect in Opera, FrontPage, and Netscape. For some...
7
by: Bura Tino | last post by:
Hi, Going forward, what's better to use <td height="20"> or <td style="height:20;">
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
39
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A...
3
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes...
13
by: Stevo | last post by:
I've found that for IE6+, if you add the property text-align:center to a DIV, then *anything* inside it gets centered. That can be a table, an object/embed, another DIV, an image, or some text. ...
14
by: Astley Le Jasper | last post by:
I'm still learning python and would like to know what's a good way of organizing code. I am writing some scripts to scrape a number of different website that hold similar information and then...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.