Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Allocation of memory for Array of Pointers

Reply
Thread Tools

Allocation of memory for Array of Pointers

 
 
Chuck F.
Guest
Posts: n/a
 
      12-27-2005
chandan wrote:
>
> Forgot to mention that u must typecast the calloc function as
> its return type is void * (however some compiler automatically
> return pointer type of the type_of_its_second_ argument.)


Please do not top-post, do not use silly confusing abbreviations
(such as "u" and the ilk), and do include context.

In addition your advice is wrong. There is never any need to cast
the return values from calloc, malloc, and friends. There IS a
need to #include the appropriate header, such as stdlib.h.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

 
Reply With Quote
 
 
 
 
alice
Guest
Posts: n/a
 
      12-27-2005

smartbeginner wrote:
> main()
> {
> int i;
> int *a[2];
> a=calloc(4,sizeof(*a));
> /* The above code I know will not compile[Lvalue required] .But why
> cant I allocate space
> for all 4 integers i need to store */
> for(i=0;i<2;i++)
> a[i]=calloc(2,sizeof(*a));
> /* this works fine I know?But why not the earlier one */
> }


The First one won't compile because a is an array and is NOT a pointer.

 
Reply With Quote
 
 
 
 
Charles Richmond
Guest
Posts: n/a
 
      12-27-2005
"Chuck F." wrote:
>
> chandan wrote:
> >
> > Forgot to mention that u must typecast the calloc function as
> > its return type is void * (however some compiler automatically
> > return pointer type of the type_of_its_second_ argument.)

>
> Please do not top-post, do not use silly confusing abbreviations
> (such as "u" and the ilk), and do include context.
>

....and do *not* generally be an asshole in any other way.
>
> In addition your advice is wrong. There is never any need to cast
> the return values from calloc, malloc, and friends. There IS a
> need to #include the appropriate header, such as stdlib.h.
>

True, you don't need the cast. But if you are stubborn, the cast
is *not* prohibited by the C standard.

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      12-28-2005
i wrote:
> Forgot to mention that u must typecast the calloc function as its
> return type is void * (however some compiler automatically return
> pointer type of the type_of_its_second_ argument.)


i am sorry for the silly abbreviations, i had used. Thanks, for the
advice.

Martin Ambuhl wrote:
>Pay no attention to chandon; he knows nothing.


I never told that i know everything. i am a beginner to C and, really,
i don't know anything, as such, about C. I got my first lesson and I am
eager to learn more.

Keith wrote:
>No. calloc() returns a result of type void*, but it's implicitly
>converted to the target pointer type. An explicit cast is unnecessary
>and can mask errors (such as forgetting the required "#include <stdlib.h>"
>or compiling C code with a C++ compiler).


In my case, i was trying to compile this code with a C++ compiler. But,
please, can you elaborate how it masks the error and how stdlib.h helps
in conversion of type void * to target pointer type? i mean, how it
determines the target pointer type, if i am using a C compiler.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2005
chandan said:

> Keith wrote:
>>No. calloc() returns a result of type void*, but it's implicitly
>>converted to the target pointer type. An explicit cast is unnecessary
>>and can mask errors (such as forgetting the required "#include <stdlib.h>"
>>or compiling C code with a C++ compiler).

>
> In my case, i was trying to compile this code with a C++ compiler.


That's always a mistake. You wouldn't try to compile Algol with a Pascal
compiler, so why compile C with a C++ compiler?

> But,
> please, can you elaborate how it masks the error and how stdlib.h helps


It provides a full prototype for calloc, thus telling the compiler that
malloc returns void *.

> in conversion of type void * to target pointer type?


By telling the compiler that calloc returns void *, the prototype assists in
the generation of correct code. Without the prototype, the compiler would
be forced to assume that calloc returns int (which calloc doesn't in fact
return).

In the following example, I use malloc rather than calloc, for the simple
reason that experienced C programmers hardly ever use calloc as its side
effect is generally (but not quite always) unnecessary, but the reasoning
is identical.

> i mean, how it
> determines the target pointer type, if i am using a C compiler.


#include <stdio.h> /* for printf prototype */
#include <stdlib.h> /* for malloc prototype */

typedef int T; /* any object type will do here */

int main(void)
{
T *p; /* compiler now knows p has type T *, so all is well */
size_t n = 42; /* compiler now knows n has type size_t, so all is well */
p = malloc(n * sizeof *p); /* malloc returns void *, and the compiler
* knows that p has type T *, and the
* compiler knows how to convert
* between void * and T *, so there is
* no problem here. */
if(p != NULL)
{
printf("Yes, I got the memory. It's at %p.\n", (void *)p);
free(p);
}

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-28-2005
chandan wrote:
> i wrote:
>
>>Forgot to mention that u must typecast the calloc function as its
>>return type is void * (however some compiler automatically return
>>pointer type of the type_of_its_second_ argument.)

>
>
> i am sorry for the silly abbreviations, i had used. Thanks, for the
> advice.
>
> Martin Ambuhl wrote:
>
>>Pay no attention to chandon; he knows nothing.

>
>
> I never told that i know everything.


*Everything* you wrote was wrong. It is not a question of your knowing
everything: everything you "know" is wrong and you have no business
inflicting your ignorance on others as advice.

 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      12-28-2005
Richard wrote:
>/* malloc returns void *, and the compiler
> * knows that p has type T *, and the
> * compiler knows how to convert
> * between void * and T *, so there is
> * no problem here. */


Richard, i got your point. But, still I have a little confusion.

In section 7.8.5 of K&R, they mention that

The pointer returned by malloc or calloc has the proper alignment for
the object in question, but it must be cast into the appropriate type,
as in
int *ip;
ip = (int *) calloc(n, sizeof(int));

Is the above given statement is no longer valid? And, if it is valid
statement, then which point i am missing, now?

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2005
chandan said:

> Richard, i got your point. But, still I have a little confusion.
>
> In section 7.8.5 of K&R, they mention that
>
> The pointer returned by malloc or calloc has the proper alignment for
> the object in question, but it must be cast into the appropriate type,


Yes, K&R got it wrong. The cast is not required. The following quote is from
Dennis Ritchie's "errata" site:

"142(6.5, toward the end): The remark about casting the return value of
malloc ("the proper method is to declare ... then explicitly coerce") needs
to be rewritten. The example is correct and works, but the advice is
debatable in the context of the 1988-1989 ANSI/ISO standards. It's not
necessary (given that coercion of void * to ALMOSTANYTYPE * is automatic),
and possibly harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended error. On
the other hand, pre-ANSI, the cast was necessary, and it is in C++ also."

Taken from: <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      12-28-2005
Martin Ambuhl wrote:
>*Everything* you wrote was wrong. It is not a question of your knowing
>everything: everything you "know" is wrong and you have no business
>inflicting your ignorance on others as advice.


Cool down, man!! Why, you are showing so much aggression?

>everything: everything you "know" is wrong.


No, not now.... . Feeling happy.

 
Reply With Quote
 
chandan
Guest
Posts: n/a
 
      12-28-2005
Richard wrote:
>Taken from: <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html>


Thank you, very much, for the link.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
problem with pointers in c. and dynamic memory allocation manoharyes@gmail.com C Programming 1 05-23-2006 03:39 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
classes, pointers, vectors, and memory allocation mosfets@gmail.com C++ 2 02-06-2005 07:51 PM
strings, arrays, pointers and dynamic memory allocation swarsa@msn.com C Programming 5 12-24-2004 06:59 PM



Advertisments