Dynamically allocated array of pointers 
Author Message
 Dynamically allocated array of pointers

I have a little problem,
why does table[i] = NULL generate an error?
Must the i'th element be allocated before referenced/assigned?

void function(point p[], int n)
{
 int i;
 node    **table;
 table = (node**)malloc(n);
 for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
....

thanks
/Niclas



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
On Mon, 6 Mar 2000 00:50:21 +0100, "Niclas Larsn"

Quote:
> I have a little problem,
> why does table[i] = NULL generate an error?
> Must the i'th element be allocated before referenced/assigned?

> void function(point p[], int n)
> {
>  int i;
>  node    **table;
>  table = (node**)malloc(n);

Don't cast the value returned by malloc() in C, it is not necessary
and can hide errors if you forget to include <stdlib.h>.  And always
check the return value of malloc() because it will be NULL if the
allocation fails.

Quote:
>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
> ....

> thanks
> /Niclas

When you call malloc(n) successfully it returns a pointer to n bytes
of space, enough room to hold n chars.  On most implementations the
size of a pointer is larger than a char, so you n pointers overflow
the space for n chars.

Try this:

table = malloc(n * sizeof *table);

This always gives you the right size for object your pointer will
point to.

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://marshall-cline.home.att.net/cpp-faq-lite/
alt.comp.lang.learn.c-c++
http://www.raos.demon.co.uk/acllc-c++/faq.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
say that the size of  "node" is 1 byte, just to clear out any
missunderstanding.
Quote:

> void function(point p[], int n)
> {
>  int i;
>  node    **table;
>  table = (node**)malloc(n);
>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
> ....



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers


Quote:
> I have a little problem,
> why does table[i] = NULL generate an error?
> Must the i'th element be allocated before referenced/assigned?

You don't indicate whether you have this line in your code:

#include <stdlib.h>

It would be good to check that you do.

Quote:

> void function(point p[], int n)
> {
>  int i;
>  node    **table;
>  table = (node**)malloc(n);

No need to cast malloc. And n is wrong. Assuming you want n node pointers,
here's how you do it:

table = malloc(n * sizeof *table);

if(table != NULL)
{

Quote:
>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?

This'll be fine now.

Quote:
}

else
{
  memory allocation failed. Deal with this in some intelligent way.

Quote:
}

Good luck. :-)

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

comp.lang.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
On 6 Mar 2000 01:08:30 GMT, "Richard Heathfield"

Quote:

>table = malloc(n * sizeof *table);

People keep saying this, but my compiler (Visual C++ 5) complains
about missing brackets. What gives? sizeof(**table) is ok, but not
sizeof *table...

Quote:

>if(table != NULL)
>{

>>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?

>This'll be fine now.

>}
>else
>{
>  memory allocation failed. Deal with this in some intelligent way.
>}

>Good luck. :-)

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
On Mon, 06 Mar 2000 00:25:24 +0000, Mark McIntyre

Quote:
> On 6 Mar 2000 01:08:30 GMT, "Richard Heathfield"

> >table = malloc(n * sizeof *table);

> People keep saying this, but my compiler (Visual C++ 5) complains
> about missing brackets. What gives? sizeof(**table) is ok, but not
> sizeof *table...

Then your compiler is wrong.  The type of table is pointer to pointer
to node.  The type of *table is pointer to node, and the type of
**table is a node.  Are you sure you have the definition correct in
your code?  If it's a compiler quirk, try parenthesizing:

node **table;
table = malloc(n * sizeof (*table));

Jack Klein
--
Home: http://jackklein.home.att.net



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
On Mon, 6 Mar 2000 00:50:21 +0100, "Niclas Larsn"

Quote:

> I have a little problem,
> why does table[i] = NULL generate an error?

Probably because you didn't #include any headers.

NULL is not a constant known to the compiler but a macro #defined in
standard headers like <stdlib.h> <stddef.h> <stdio.h>.

If you #include any one of them then NULL will be defined.

Regards
Horst



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers

Quote:

> say that the size of  "node" is 1 byte, just to clear out any
> missunderstanding.

> > void function(point p[], int n)
> > {
> >  int i;
> >  node    **table;
> >  table = (node**)malloc(n);
> >  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
> > ....

You have made a potential fatal error. You did not check the value of table.
The value returned by function malloc will return NULL if there is a space was
not allocated. Trying to assign to NULL will give you an ERROR.

Try
if((table = malloc(n * sizeof(*table))) == NULL)
   exit(EXIT_FAILURE); /* Allocation failed. Exiting .   */
for(i = 0; i <n;i++) table[i] = NULL;

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
Initially you've declared table as a pointer to any one 'node' pointer
i.e.     node **table;
Now you intend to allocate memory to store 'n' number of 'node' pointers,
and that would be,
table=(node **) malloc(n*sizeof(node *));
You must mention the size of a node pointer and because the size of the
'node' pointer would vary with different compilers we make use of the
sizeof() function to find the exact size of a 'node' pointer and multiply it
with the desired number of 'node' pointers to be stored. The rest of the
program is fine.

-CSK.
http://snap.to/csk


Quote:
> I have a little problem,
> why does table[i] = NULL generate an error?
> Must the i'th element be allocated before referenced/assigned?

> void function(point p[], int n)
> {
>  int i;
>  node    **table;
>  table = (node**)malloc(n);
>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
> ....

> thanks
> /Niclas



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
Oh and one more thing, declaring

node **table;
table=(node **) malloc(n*sizeof(node *));

is not equivalent to

node *table[n];

The latter can be used for the dynamic allocation of an array of pointers,
as in;

node *table[n];
for(i=0;i<n;i++)
    table[i]=(node *) malloc(m*sizeof(node));

The former can be used for the dynamic allocation of an 'array of pointers',
but using it would be meaningless, as retrieval of the data is a real pain.
( ...figure it out!!! :) )

-CSK
http://snap.to/csk


Quote:
> I have a little problem,
> why does table[i] = NULL generate an error?
> Must the i'th element be allocated before referenced/assigned?

> void function(point p[], int n)
> {
>  int i;
>  node    **table;
>  table = (node**)malloc(n);
>  for(i=0; i < n; i++) table[i]=NULL;        //   <-------------ERROR!!?
> ....

> thanks
> /Niclas



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
could you tell me what does it mean?i dun understand!
Quote:

> On Mon, 6 Mar 2000 00:50:21 +0100, "Niclas Larsn"

> > I have a little problem,
> > why does table[i] = NULL generate an error?

> Probably because you didn't #include any headers.

> NULL is not a constant known to the compiler but a macro #defined in
> standard headers like <stdlib.h> <stddef.h> <stdio.h>.

> If you #include any one of them then NULL will be defined.

> Regards
> Horst



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers
On Mon, 06 Mar 2000 23:36:36 +0800, /*Pingu*/

Quote:


> > On Mon, 6 Mar 2000 00:50:21 +0100, "Niclas Larsn"

> > > I have a little problem,
> > > why does table[i] = NULL generate an error?

> > Probably because you didn't #include any headers.

> > NULL is not a constant known to the compiler but a macro #defined in
> > standard headers like <stdlib.h> <stddef.h> <stdio.h>.

> > If you #include any one of them then NULL will be defined.

[quotes rearranged]

Quote:
> could you tell me what does it mean?i dun understand!

This translation unit (file)

Quote:
>---------------------------<

void f()
{
   char*p;
   p=NULL;

Quote:
}
>---------------------------<

yields an error message because the compiler doesn't know what 'NULL'
is - unlike in the Pascal language where 'NIL' (which serves the same
purpose as a null pointer value) is a built-in constant.

In order to get 'NULL' known to the compiler you have to include one
of the headers <stdlib.h> <stdio.h> <stddef.h> all of which contain
the definition of NULL.

Regards
Horst



Fri, 23 Aug 2002 03:00:00 GMT  
 Dynamically allocated array of pointers

Quote:

>Oh and one more thing, declaring

>node **table;
>table=(node **) malloc(n*sizeof(node *));

>is not equivalent to

>node *table[n];

>The latter can be used for the dynamic allocation of an array of pointers,
>as in;

>node *table[n];
>for(i=0;i<n;i++)
>    table[i]=(node *) malloc(m*sizeof(node));

>The former can be used for the dynamic allocation of an 'array of pointers',
>but using it would be meaningless, as retrieval of the data is a real pain.
>( ...figure it out!!! :) )

What are you talking about?!?!!
Are you suggesting that the following code is invalid, or in any way more a
a "real pain" than your code sample?

 node **table;
 table = malloc(n * sizeof(*table));
 for(i=0;i<n;i++)
     table[i] = malloc(m * sizeof(**table));

If you think there's anything wrong with this code, you're sadly mistaken
(except, of course, that in none of the code samples is the return value of
malloc checked for error).
"Retrieval of the data" uses exactly the same syntax whether table is a
pointer to pointer or an array of pointers.
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies



Fri, 23 Aug 2002 03:00:00 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Dynamically allocated array of pointers and their usage.

2. Dynamically allocated array of pointers

3. dynamically allocating an array of pointers

4. dynamically allocating an array of array of...

5. How to pass dynamically allocated arrays??

6. Dynamically allocate a character array

7. Dynamically allocating mem for 2-D char array

8. passing dynamically allocated arrays to functions

9. Question on dynamically allocating a 2 dim. array

10. Dynamically allocated array problem

11. Dynamically allocated arrays of strings

12. Free a "2D dynamically allocated array"

 

 
Powered by phpBB® Forum Software