Hi

I'm working with a structure like this:

struct table{
            int id, rank, hop_sup;
            float dist,bs_dist, res_enrg, total; 
            };
struct nd{
       float x,y, enrg, bsdist;
       int id,my_rank, hop;
       int neighbor[50], prfrdnbor;
       float nbordist[50];
       struct table nbr[25];       
          }node[127];

Do you think this would cause memory problem?

In my program, in one code segment, the program looks like this:

int po,to=0,mo=0, fl;
 for(po;po<127;po++)   
  {
  if(node[po].neighbor[to]!=0)
  while(node[po].neighbor[to]!='\0')
   {
    fl=node[po].neighbor[to];
    node[po].nbr[mo].id=fl;
    node[po].nbr[mo].rank=node[fl-1].my_rank;
    node[po].nbr[mo].res_enrg=node[fl-1].enrg;
    node[po].nbr[mo].dist=copy_dist[po][fl-1];
    node[po].nbr[mo].bs_dist=node[fl-1].bsdist;
    node[po].nbr[mo].total=pow(node[po].nbr[mo].res_enrg,-1)+ node[po].nbr[mo].dist+node[po].nbr[mo].bs_dist;
    mo++;   
    to++; 
    }//eo while
    mo=0;
    to=0;
  }//eo for

I already initialized x,y,enrg, bsdist,id,my_rank etc in structure nd by some calculation. But the above code segment producing wrong and strange result. Can u guess whts happenning?

>Do you think this would cause memory problem?
Well, that actually depends on how much RAM there's inside your computer, whether you're planning to create arrays of structures, etc. ...

You can quickly check how much bytes a structure occupies in your computer's memory by using the sizeof operator.

You could use the following program to check the memory occupied by the structures:

#include <stdio.h>

// Your structures are declared here
struct table{
  int id, rank, hop_sup;
  float dist,bs_dist, res_enrg, total; 
};

struct nd{
  float x,y, enrg, bsdist;
  int id,my_rank, hop;
  int neighbor[50], prfrdnbor;
  float nbordist[50];
  struct table nbr[25];       
} node[127];

int main()
{
  // Store the size (in bytes)
  size_t sz_nd = sizeof(struct nd);
  size_t sz_table = sizeof(struct table);
  size_t sz_node127 = sizeof(node);
  
  // Output the size to the screen
  printf("%s%u%s\n", "Size of a \'nd\' structure: ", sz_nd, " bytes.");
  printf("%s%u%s\n", "Size of a \'table\' structure: ", sz_table, " bytes.");
  printf("%s%u%s\n", "Size of \'node[127]\': ", sz_node127, " bytes.");
  
  return 0;
}

/* My output:
Size of a 'nd' structure: 1132 bytes.
Size of a 'table' structure: 28 bytes.
Size of 'node[127]': 143764 bytes.
*/

Please note that the output can differ from implementation to implementation.
Well, you can decide for yourself whether this occupies much memory or not, you'll have to compare it with the amount of RAM inside the computer where you're planning to run your program on.

This article has been dead for over six months. Start a new discussion instead.