Programatically read 'size' shown in top?

How can the 'size' of a process, that is shown by 'top', be read programatically?

I'm fixing a memory leak in a large (20,000 lines) program. (The main.cpp is itself 7400 lines!).

Can you please elaborate the question better ?

What do you mean by SIZE ? memory ?

Through which program you want to read, through ( within ) the same CPP program or using some scripts ?

Use the right tool for the job.

If this is a commerical application, a real memory-management tool like Purify is well worth the cost. How many hours have you spent trying to find this bug? Stumbling around looking here and there, hoping to find it?

No one takes their cars to be fixed by a mechanic who uses only a pair of pliers and one thirty-year-old bent screwdriver, do they? Why do they expect programmers to do their job without tools, then?

And why do too many programmers not want to use the proper tools?

There are quite a lot of free and efficient tools too allowing to track down memory leaks under Solaris, including dbx run time checking.
http://www.unix.com/sun-solaris/73873-how-find-memory-leak-solaris.html\#post302216415

Back to the original question, here is a sample code to monitor a process size an rss:

#include <procfs.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  char psinfo[32];
  struct psinfo p;
  int fd;
  pid_t pid;
  if(argc>1)
    pid=atoi(argv[1]);
  else
    exit(1);
  sprintf(psinfo,"/proc/%ld/psinfo",pid);
  fd=open(psinfo, O_RDONLY);
  if(fd!=-1)
  {
    read(fd,&p,sizeof(p));
    printf("size=%d rss=%d\n", p.pr_size, p.pr_rssize);
    close(fd);
  }