malloctests.c

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bellow is a simple program wich test how malloc
allocates memory.  On my systems glibc malloc starts with 128K RAM

[diet -Os ] gcc -Os -Wall -W malloctest.c
usage:
	./a.out 1
	./a.out 4046
	./a.out 140000

	strace ./a.out 22000  2>&1 | sed -e 1,/ZYX4132/d
	strace ./a.out 200000 2>&1 | sed -e 1,/ZYX4132/d

In my next post I'll describe why I wrote this program!
It is designed to be as simple as possible...

Nikola

/* -- malloctest.c -- */
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  unsigned long u;
  unsigned char c, *x, *a, *b, buf[3*sizeof(unsigned long)];

  if (argc<2) { u=1; goto ready; }
  x = (unsigned char *)argv[1];
  for (u=0; (c=(*x - '0')) < 10; x++) u = u*10 + c;

 ready:
  write(2, "ZYX4132\n", 8);

  a = sbrk(0);
  x = malloc(u);
  if (x) free(x);
  b = sbrk(0);

  u = b-a;
  *--x = '\n';
  do { *--x = '0'+u%10; u/=10; } while(u);
  write(2, x, (buf + sizeof(buf)) -x);

  return 0;
}
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux