www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/06/19/12:55:21

Date: Mon, 19 Jun 1995 17:36:12 +0100
From: "Juan I. Perez" <jips AT sol DOT unizar DOT es>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Counting Memory Problem

I've got a problem with the dynamic allocation of structures. I've written
a program in which I reserve memory for 4 structures (struct s_elto).
The problem comes when I compare the memory I wanted to reserve, and the
memory free of the computer (with the dpmi functions). This is the code:


#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <dpmi.h>
#include <dos.h>
#include <go32.h>

#define tam_memo 10240

struct s_elto
{
	int memo[tam_memo];
};

struct s_elto *elto_crea(void);
void elto_destruye(struct s_elto *elto);

#define num 4
struct s_elto *elto[num];
int num_eltos=0;

void main(void)
{
	int i, pm;
	_go32_dpmi_meminfo meminfo;

	for(i=0;i<num;i++)
	{
		elto[i]=elto_crea();
		printf("num_eltos=%d\n",num_eltos);
		_go32_dpmi_get_free_memory_information(&meminfo);
		printf("available physical memory = %d\n", meminfo.available_physical_pages * 4096);
		pm = _go32_dpmi_remaining_physical_memory();
		printf("remaining physical memory = %d\n", pm);
	}

	{
		printf("sizeof(struct s_elto)=%d\n",sizeof(struct s_elto));
		printf("sizeof(elto[i]->memo)=%d\n",sizeof(elto[i]->memo));
		printf("sizeof(elto[i]->memo[0])=%d\n",sizeof(elto[i]->memo[0]));
	}

	for(i=0;i<num;i++)
	{
		elto_destruye(elto[i]);
	}
}

void outmem(void *p)
{
	if (p==NULL)
	{
		printf("Out of memory\n");
		exit(-1);
	}
}

struct s_elto *elto_crea(void)
{
	struct s_elto *elto;

	elto=(struct s_elto *)malloc(sizeof(struct s_elto));
	outmem(elto);
	num_eltos++;
	return(elto);
}

void elto_destruye(struct s_elto *elto)
{
	free(elto);
	num_eltos--;
}


And this is the result my PC(486DX66, 4MB) gives:

num_eltos=1
available physical memory = 2748416
remaining physical memory = 2748416
num_eltos=2
available physical memory = 2744320
remaining physical memory = 2744320
num_eltos=3
available physical memory = 2740224
remaining physical memory = 2740224
num_eltos=4
available physical memory = 2736128
remaining physical memory = 2736128
sizeof(struct s_elto)=40960
sizeof(elto[i]->memo)=40960
sizeof(elto[i]->memo[0])=4

As it can be seen, the struct elto needs 40960 bytes for been allocated,
but the physical memory decrements only in 4092 bytes. Why?

I'd be very grateful if anybody could help me. Thanks in advance.

And another question: When I free memory, the physical memory available
of dpmi function doesnït increase. Could anyone tell me why, please? Thanks
again.

	*---------------------------*
	* Juan I Perez              *
	* Zaragoza (Spain)          *
	* e-mail:jips AT sol DOT unizar DOT es *
	*---------------------------*

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019