473,394 Members | 1,951 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,394 software developers and data experts.

Different ways to cast?

JS
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:
ptr = (*int) malloc(sizeof(int)*N)
But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));
why does the example cast like this (*int)

and when I compile I need to write:

(int *)

JS
Nov 14 '05 #1
6 1641


JS wrote:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:
ptr = (*int) malloc(sizeof(int)*N)
But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));
why does the example cast like this (*int)

and when I compile I need to write:

(int *)


Because the example is wrong, R-O-N-G, wrong. Even
after fixing the typographical error (as you did), the
example demonstrates poor technique; a far better idiom is

ptr = malloc(N * sizeof *ptr);

"Why is that better?" you ask. First, the cast is
unnecessary: malloc() returns a `void*' value, and this
type converts automatically to and from any data pointer
type. Second, `sizeof *ptr' is better than `sizeof(int)'
because it is guaranteed to be the proper size. That is,
it avoids errors like

short **p = malloc(N * sizeof(short));

.... where `sizeof(short)' should have been `sizeof(short*)'.
This latter error is more insidious when multiple types
are flying around:

struct query_s { ... } *p;
struct response_s { ... } *q;
...
p = malloc(sizeof(struct query_s));
FillInQueryData(p);
q = malloc(sizeof(struct query_s)); /* oops! */
GetResultsOfQuery(p, q);

--
Er*********@sun.com

Nov 14 '05 #2
JS wrote:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:
ptr = (*int) malloc(sizeof(int)*N)
But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));
why does the example cast like this (*int)

and when I compile I need to write:

(int *)
...


Firstly, it is an obvious typo in the example. It was definitely
supposed to be '(int *)'.

Secondly, in ANSI/ISO C in this context you don't need to use a cast at
all. Moreover, to apply an explicit cast to the result of 'malloc' is a
bad practice. The tutorial you are using is helplessly outdated (unless
you were specifically looking for a tutorial on K&R C).

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
JS wrote:

On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:

ptr = (*int) malloc(sizeof(int)*N)


If it is about C it is wrong, there should be no cast. If it is
talking about C++ it is off-topic in c.l.c.

For c the correct operation is:

if (!(ptr = malloc(sizeof(*ptr)))) /* no memory action */;
else {
/* got the memory, carry on */
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4
On Thu, 24 Mar 2005 10:46:08 -0800, Andrey Tarasevich
<an**************@hotmail.com> wrote:
JS wrote:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:
ptr = (*int) malloc(sizeof(int)*N)
But when I try to compile it in a main method I need to write:

int *ptr;
ptr = (int *) malloc(sizeof(int));
why does the example cast like this (*int)

and when I compile I need to write:

(int *)
...
Firstly, it is an obvious typo in the example. It was definitely
supposed to be '(int *)'.

A highly consistent typo - the same error appears throughout the
article, along with a statement that malloc needs to know "what the
type of the pointer variable is". A call which allocates memory for a
structure has the same error.

I didn't look further in the tutorial, but I'd be very wary of it.
Secondly, in ANSI/ISO C in this context you don't need to use a cast at
all. Moreover, to apply an explicit cast to the result of 'malloc' is a
bad practice. The tutorial you are using is helplessly outdated (unless
you were specifically looking for a tutorial on K&R C).


But it's marked "Last updated: 09 July 2001 09:59".

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
Alan Balmer wrote:
<an**************@hotmail.com> wrote:
.... snip ...
I didn't look further in the tutorial, but I'd be very wary of it.
Secondly, in ANSI/ISO C in this context you don't need to use a
cast at all. Moreover, to apply an explicit cast to the result of
'malloc' is a bad practice. The tutorial you are using is
helplessly outdated (unless you were specifically looking for a
tutorial on K&R C).


But it's marked "Last updated: 09 July 2001 09:59".


Sounds like something of which Schildt could be proud.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
"JS" <dsa.@asdf.com> wrote:
On this page under "Malloc":

http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

I found this:

ptr = (*int) malloc(sizeof(int)*N) why does the example cast like this (*int)


Because the author of those pages is rather deceived as to what is good
C - or indeed, as to what is C at all. In the references section, he
recommends two books by Herbert Schildt. That says enough.

Richard
Nov 14 '05 #7

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

Similar topics

7
by: Charles Law | last post by:
As far as I can tell the following are equivalent Dim i As Integer i = Convert.ToInt32(1) i = CInt(1) i = CType(i, Integer) Is there any particular reason why we should have all these ways...
17
by: tuvok | last post by:
How can objects of different types be stored in a collection? For example: struct S1 { /*...*/ }; struct S2 { /*...*/ }; struct Sn { /*...*/ }; template<typename T> class X { public:
1
by: Johann Uhrmann | last post by:
Hello, are there any experiences about the performance of indices with different data types. How do the performance of an index that consists of - an integer field - a varchar() field - a...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
2
by: Chetan Raj | last post by:
Hi, What are the different ways in which a virtual function can be implemented by the C++ compiler? I know VTABLE and VPOINTER is only one of the way. What are the other ways? What are the pros...
6
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.