How to use Pointer inside a Structure

Feb 8, 2011 at 2:51am
I hav made a structure in my C program:

1
2
3
4
typedef struct species
{ int Trophic;
  int Niche[10000];
} species;


So each species has a Trophic value and a Niche array.
The Niche array can at maximum carry 9999 elements (0 to 9999). But in reality, MOST of the species take JUST 2 or 3 elements in their Niche array. Just 1 or 2 species use a large number of Niche elements. But still I am having to assign a size of 10000 to every species' Niche array. That must be taking a lot of memory.

Now the number of Niche elements that a specie occupies in its niche array (i.e how many elements of the niche array are occupied) is decided as the program runs. But once decided, it remains constant.

So I am trying to use Dynamic Memory Allocation for the Niche array so that the species that take a few Niche elements are assigned a small size to their Niche array and the species that take a larger number of Niche elements get larger size to their Niche array. But I am not able to use a pointer inside structure.

I am very new to pointers etc. But still have studied & practiced a bit of them but not able to use it in structures.

Can someone please guide me.

Regards
Alice
Feb 8, 2011 at 3:05am
Have you considered writing this in C++, using a vector instead of an array?
Feb 8, 2011 at 3:11am
Hi PanGalactic

Thanx a ton for replying.

No, I dont know vectors much. Can I use it in C and can it solve my problem of Dynamic Memory Allocation??

Regards
Alice
Feb 8, 2011 at 4:44am
C != C++

Vectors are C++ only.

Vectors were designed to solve exactly the problem you face with dynamic memory allocation.
Feb 8, 2011 at 9:45am

Thanx PanGalactic
But my whole program is in C. Still thanx alot for taking time for me & trying to help. :-)

Can someone help with Pointers in Structures for Dynamic Memory allocation in C

Examples will be deeply appreciated.

Regards
Alice
Feb 8, 2011 at 11:49am
I haven't done much C, but this is somewhere along the lines of what I would try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Species
{ 
  int trophic;
  unsigned int numNiches;
  int* niche;
};

int constructSpecies(Species& s, int trophic, unsigned int numNiches){
  s.trophic = trophic;
  s.numNiches = numNiches;
  s.niche = (int*) malloc(numNiches*sizeof(int));
  if(s.niche == NULL){
    return some error value;
  }
  return some ok value;
}

void destructSpecies(Species& s){
  free (s.niche);
}


I've probably made a mistake somewhere, but that might help. So for example:

1
2
3
4
5
6
7
8
9
10
11
12

Species s;
int ret = constructSpecies(s, 1, 50);
if(ret != OK){ do something. }
for(unsigned int i = 0; i < s.numNiches; i++){
  s.niche[i] = some int;
}
//... Do whatever

//Don't forget to destruct!

destructSpecies(s);




Feb 9, 2011 at 5:42am
Thanx a ton NickPaul

Learning from your code, I have developed a short code (using calloc). Can u please check it carefully if I have made any mistakes ,like [COLOR="red"]memory leaks[/COLOR] etc.. (although I have checked it by printing the outputs, but am not shure about [COLOR="red"]memory leaks[/COLOR])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
typedef struct species
{
    int * niche;
} species;
 
void main()
{
  const int TotalSpecies = 3;
  species s[TotalSpecies];
  int i,j,nichesize;

  for( i = 0; i < TotalSpecies; i++)
   {
	  printf("enter the niche size of specie %d	\n",i);
	  scanf("%d",&nichesize);

	  s[i].niche = calloc(nichesize,sizeof(int));

	  printf("enter the niche value(s) of specie %d\n",i);

  	  for(j=0;j<nichesize;j++)
	   	  scanf("%d",&s[i].niche[j]);
   }


   for( i = 0; i < TotalSpecies;i++)
		 free(s[i].niche);

}


Thanx a lot
Regards
Alice
Feb 9, 2011 at 6:11am
Looks good to me! In C++ there are a number of programming practices which significantly reduce the likely-hood of memory leaks - I don't really know how you should go about it in C. However from what I can see you are memory leak free :)


Topic archived. No new replies allowed.