#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.
--
[1;30m|[m
[32m ___[m
[32m(-_-)[m
[32m<¡ä¡ä>[1;30m¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w [1;37mªÅ´ßªª³õ [0;37mshepjeng.twbbs.org [1;30m¢w¢w¢w
[1;30m¡þ[m [36mplum.cs.nccu.edu.tw[m 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
> #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
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
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
[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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Bura Tino |
last post by:
Hi,
Going forward, what's better to use
<td height="20">
or
<td style="height:20;">
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |