help! arrays of pointers 
Author Message
 help! arrays of pointers

I want to create an array with row and column size dynamically allocated.
The array element contain pointers to a structure type.
The size of the rows and columns is variable like this, for example:

*
**
***
****
*****

where * is a pointer to a structure type
row 1 has  1 column, row 2 has 2 columns and so on.
Here's the gist of my code (simplified):
____________________________________________
//the structure are two arrays, both dynamically allocated
typedef struct{
short int *a;
char *b;

Quote:
}stu;

//the main()
stu **s; //a 2-D array of pointers to stu
row=5;

s = calloc(row,sizeof(s *)); //allocate 5 rows
for (i=0;i<row; i++)  //allocate the column size
     s[i] = calloc(i+1,sizeof(s));

//now initialize each structure
for (i=0;i< row; i++)
 for (j=0;j<i;j++)
  s[i][j]=  create_stu();

//the create_stu function:
stu create_stu(void)
{
 stu ss;
 ss.a = malloc(sizeof(short)*100);
 ss.b = malloc(sizeof(char)*100);
 /*some code here to write values into a and b*/
 return ss;

Quote:
}

_________________________________________

Compiled using MS Visual C++.
Problem: It seems to work ok, except it is using more memory than my
calculations
(i expected my program, for the part above, to use about 20 MB memory, but
Windows Task Manager reported about 200MB increase in memory usage in
between entering and finishing the part above) .
Thanks!
-CB
--



Mon, 24 Oct 2005 11:15:29 GMT  
 help! arrays of pointers

Quote:

> I want to create an array with row and column size dynamically allocated.
> The array element contain pointers to a structure type.
> The size of the rows and columns is variable like this, for example:

> *
> **
> ***
> ****
> *****

> where * is a pointer to a structure type
> row 1 has  1 column, row 2 has 2 columns and so on.
> Here's the gist of my code (simplified):
> ____________________________________________
> //the structure are two arrays, both dynamically allocated
> typedef struct{
> short int *a;
> char *b;
> }stu;

> //the main()
> stu **s; //a 2-D array of pointers to stu
> row=5;

> s = calloc(row,sizeof(s *)); //allocate 5 rows
> for (i=0;i<row; i++)  //allocate the column size
>      s[i] = calloc(i+1,sizeof(s));

> //now initialize each structure
> for (i=0;i< row; i++)
>  for (j=0;j<i;j++)

 for (j=0;j<=i;j++)
            ^

- Show quoted text -

Quote:
>   s[i][j]=  create_stu();

> //the create_stu function:
> stu create_stu(void)
> {
>  stu ss;
>  ss.a = malloc(sizeof(short)*100);
>  ss.b = malloc(sizeof(char)*100);
>  /*some code here to write values into a and b*/
>  return ss;
> }
> _________________________________________

> Compiled using MS Visual C++.
> Problem: It seems to work ok, except it is using more memory than my
> calculations
> (i expected my program, for the part above, to use about 20 MB memory, but
> Windows Task Manager reported about 200MB increase in memory usage in
> between entering and finishing the part above) .

I wonder how you calculated that; I'd rather expect about 4.5 KiB.
--



Tue, 25 Oct 2005 07:03:41 GMT  
 help! arrays of pointers

Quote:
>//the structure are two arrays, both dynamically allocated
>typedef struct{
>short int *a;
>char *b;
>}stu;

a and b are pointers, not arrays.

Quote:
>//the main()
>stu **s; //a 2-D array of pointers to stu
>row=5;

>s = calloc(row,sizeof(s *)); //allocate 5 rows

sizeof *s

I usually write:

p = malloc(size * sizeof *p);

Quote:
>for (i=0;i<row; i++)  //allocate the column size
>     s[i] = calloc(i+1,sizeof(s));

sizeof *s[i]

Quote:
> ss.b = malloc(sizeof(char)*100);

sizeof(char) is one, so it can be left out.

Quote:
>Problem: It seems to work ok, except it is using more memory than my
>calculations
>(i expected my program, for the part above, to use about 20 MB memory, but
>Windows Task Manager reported about 200MB increase in memory usage in
>between entering and finishing the part above) .

Hard to know how it interpreted sizeof(s *).  I am surprised that it
compiled.

Best wishes,

Bob
--



Tue, 25 Oct 2005 07:03:53 GMT  
 help! arrays of pointers

Quote:
> //the structure are two arrays, both dynamically allocated
> typedef struct{
> short int *a;
> char *b;
> }stu;
> //the main()
> stu **s; //a 2-D array of pointers to stu

That comment is bogus.  This is a 2-D array of structs "stu", not of
pointers to it.  You use up two '*' for the 2-D array, so there is
none left for the element type to be a pointer to something.

Quote:
> row=5;
> s = calloc(row,sizeof(s *)); //allocate 5 rows

                 ^^^^^^^^^^^

This is your problem.  It should read either "sizeof (* s)", i.e. "the
size of the thing pointed to by s", or sizeof (stu *) as in "size of a
pointer to stu", which is what "* s" happens to be.

Quote:
> for (i=0;i<row; i++)  //allocate the column size
>      s[i] = calloc(i+1,sizeof(s));

                         ^^^^^^^^^
This is wrong for the same reasons.  Make that either "sizeof (**s)"
or "sizeof(stu)".

I'm somewhat surprised that this even compiled --- or did it?

Quote:
> //now initialize each structure
> for (i=0;i< row; i++)
>  for (j=0;j<i;j++)
>   s[i][j]=  create_stu();

This fails to initialize all array elements you allocated.  You
allocated (i+1) elements, s[i][0] to s[i][i], but you don't initialize
s[i][i].

--

Even if all the snow were burnt, ashes would remain.
--



Tue, 25 Oct 2005 07:04:24 GMT  
 help! arrays of pointers

Quote:

> Compiled using MS Visual C++.
> Problem: It seems to work ok, except it is using more memory than my
> calculations
> (i expected my program, for the part above, to use about 20 MB memory, but
> Windows Task Manager reported about 200MB increase in memory usage in
> between entering and finishing the part above) .

It would be easier to answer if you would deside to what group to post
your question. ( If you can't select one single group where to post, you
better not post anywhere before doing more research about the groups ).

It also would be easier to answer if you would have provided a fully
compilable source code ( As short and simple as possible ) so people
here could test it.

Followups set to alt.comp.lang.learn.c-c++
--



Tue, 25 Oct 2005 07:04:41 GMT  
 help! arrays of pointers

Quote:
> I want to create an array with row and column size dynamically allocated.
> The array element contain pointers to a structure type.
> The size of the rows and columns is variable like this, for example:

> *
> **
> ***
> ****
> *****

> where * is a pointer to a structure type
> row 1 has  1 column, row 2 has 2 columns and so on.

As a complement to previous replies, I'd like to point out
that you could easily use a 1-dimension array for this
table:
  stu* createTriangularTable( unsigned rowCount )
  {
     unsigned itemCount = (rowCount*(rowCount+1))/2;
     stu* table = malloc( itemCount * sizeof(stu) );
     for( unsigned i = 0 ; i!=itemCount ; ++i )
       init_stu( table+i );
     return table;
  }
Then to access an item:
  stu* getTriangularTableItem( stu* table
                             , unsigned row, unsigned col )
  {
    return table + (row*(row+1))/2 + col;
  }

This will reduce the size of the memory to be allocated,
and can speed-up several operations over the table's contents.

Quote:
> typedef struct{
> short int *a;
> char *b;
> }stu;
....
> Windows Task Manager reported about 200MB increase in memory usage in
> between entering and finishing the part above) .

But what portion of this memory usage is due to the contents
of each stu instance being allocated ?

hth,
--
 Ivan Vecerina, Dr. med.  <>  http://www.post1.com/~ivec
 Soft Dev Manger, XiTact  <>  http://www.xitact.com
 Brainbench MVP for C++   <>  http://www.brainbench.com
--



Mon, 31 Oct 2005 12:33:17 GMT  
 help! arrays of pointers

Quote:

> I want to create an array with row and column size dynamically
> allocated. The array element contain pointers to a structure type.
> The size of the rows and columns is variable like this, for example:

> *
> **
> ***
> ****
> *****

> where * is a pointer to a structure type
> row 1 has  1 column, row 2 has 2 columns and so on.
> Here's the gist of my code (simplified):
> ____________________________________________
> //the structure are two arrays, both dynamically allocated
> typedef struct{
> short int *a;
> char *b;
> }stu;

Assuming "sizeof(*)=4" then 8 bytes.

Quote:
> //the main()
> stu **s; //a 2-D array of pointers to stu
> row=5;

> s = calloc(row,sizeof(s *)); //allocate 5 rows

5 * 8

Quote:
> for (i=0;i<row; i++)  //allocate the column size
>      s[i] = calloc(i+1,sizeof(s));

1 * 8
....
5 * 8
=120

Quote:
> //now initialize each structure
> for (i=0;i< row; i++)
>  for (j=0;j<i;j++)
>   s[i][j]=  create_stu();

s[0][0] ?what about 'j' loop on 'i=0'?

300 bytes per call.

Quote:
> //the create_stu function:
> stu create_stu(void)
> {
>  stu ss;
>  ss.a = malloc(sizeof(short)*100);

Assuming sizeof(short)=2
2*100

Quote:
>  ss.b = malloc(sizeof(char)*100);

Assuming "sizeof(char)=1 byte"
1 * 100

Quote:
>  /*some code here to write values into a and b*/
>  return ss;

300 (by value s[i][j]=)?

Quote:
> }
> _________________________________________

> Compiled using MS Visual C++.
> Problem: It seems to work ok, except it is using more memory than my
> calculations
> (i expected my program, for the part above, to use about 20 MB memory,
> but Windows Task Manager reported about 200MB increase in memory usage
> in between entering and finishing the part above) .

It's not reliable though a discrepancy that large is difficult to
ignore. Something's amiss but with pointers it's best to post a
compilable snippet. Can't tell what's a typo or not!

--
Guy Harrison
--



Wed, 09 Nov 2005 02:34:23 GMT  
 help! arrays of pointers

Quote:

> I want to create an array with row and column size dynamically
> allocated. The array element contain pointers to a structure type.
> The size of the rows and columns is variable like this, for example:

> *
> **
> ***
> ****
> *****

> where * is a pointer to a structure type
> row 1 has  1 column, row 2 has 2 columns and so on.
> Here's the gist of my code (simplified):
> ____________________________________________
> //the structure are two arrays, both dynamically allocated
> typedef struct{
> short int *a;
> char *b;
> }stu;

Assuming "sizeof(*)=4" then 8 bytes.

Quote:
> //the main()
> stu **s; //a 2-D array of pointers to stu
> row=5;

> s = calloc(row,sizeof(s *)); //allocate 5 rows

5 * 8

Quote:
> for (i=0;i<row; i++)  //allocate the column size
>      s[i] = calloc(i+1,sizeof(s));

1 * 8
....
5 * 8
=120

Quote:
> //now initialize each structure
> for (i=0;i< row; i++)
>  for (j=0;j<i;j++)
>   s[i][j]=  create_stu();

s[0][0] ?what about 'j' loop on 'i=0'?

300 bytes per call.

Quote:
> //the create_stu function:
> stu create_stu(void)
> {
>  stu ss;
>  ss.a = malloc(sizeof(short)*100);

Assuming sizeof(short)=2
2*100

Quote:
>  ss.b = malloc(sizeof(char)*100);

Assuming "sizeof(char)=1 byte"
1 * 100

Quote:
>  /*some code here to write values into a and b*/
>  return ss;

300 (by value s[i][j]=)?

Quote:
> }
> _________________________________________

> Compiled using MS Visual C++.
> Problem: It seems to work ok, except it is using more memory than my
> calculations
> (i expected my program, for the part above, to use about 20 MB memory,
> but Windows Task Manager reported about 200MB increase in memory usage
> in between entering and finishing the part above) .

It's not reliable though a discrepancy that large is difficult to
ignore. Something's amiss but with pointers it's best to post a
compilable snippet. Can't tell what's a typo or not!

--
Guy Harrison
--



Tue, 15 Nov 2005 16:29:38 GMT  
 help! arrays of pointers

Quote:

> I want to create an array with row and column size dynamically allocated.
> The array element contain pointers to a structure type.
> The size of the rows and columns is variable like this, for example:

> *
> **
> ***
> ****
> *****

> where * is a pointer to a structure type
> row 1 has  1 column, row 2 has 2 columns and so on.
> Here's the gist of my code (simplified):
> ____________________________________________
> //the structure are two arrays, both dynamically allocated
> typedef struct{
> short int *a;
> char *b;
> }stu;

> //the main()
> stu **s; //a 2-D array of pointers to stu
> row=5;

> s = calloc(row,sizeof(s *)); //allocate 5 rows

"s *" ???
and this compiled, did it ? does your compiler
perhaps very dangerously supply a default value ?

perhaps you meant *s, not s* ??

Quote:
> for (i=0;i<row; i++)  //allocate the column size
>      s[i] = calloc(i+1,sizeof(s));

> //now initialize each structure
> for (i=0;i< row; i++)
>  for (j=0;j<i;j++)
>   s[i][j]=  create_stu();

> //the create_stu function:
> stu create_stu(void)
> {
>  stu ss;
>  ss.a = malloc(sizeof(short)*100);
>  ss.b = malloc(sizeof(char)*100);

why  multiply by 100 ?

Quote:
>  /*some code here to write values into a and b*/
>  return ss;
> }
> _________________________________________

> Compiled using MS Visual C++.
> Problem: It seems to work ok, except it is using more memory than my
> calculations

how is "okay" defined ? are you checking the results ?

Quote:
> (i expected my program, for the part above, to use about 20 MB memory,

hmm, my calculations come up to a fraction of that, how did you get
20MB ?

hth
goose
--



Wed, 16 Nov 2005 03:32:56 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. help! arrays of pointers

2. Pointer to an array of function pointers [HELP!]

3. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

4. Array of pointers, pointer to array...

5. arrays pointers array of pointers

6. array pointer/pointer array

7. Pointer of Pointers was Pointer of arrays...

8. Pointers: return of pointer to array of pointers to main

9. Help - Array of pointers to strings dynamically declared

10. Help: Pointer to 2-D array of structs.

11. need help with array of string pointers

12. HELP:Pointers/Arrays

 

 
Powered by phpBB® Forum Software