delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/20/07:45:07

Xref: news2.mv.net comp.os.msdos.djgpp:6186
From: "Bob Platko" <platko AT ix DOT netcom DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: A problem with pointers???
Date: 19 Jul 1996 00:18:32 GMT
Organization: Netcom
Lines: 140
Message-ID: <01bb7507$de159d80$c4c5b7c7@platko.ix.netcom.com>
NNTP-Posting-Host: clv-oh6-04.ix.netcom.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I been having some problems with pointers in DJGPP.  Some functions 
work while others crash the computer, athough they use the same syntax.

It's hard to explain, but maybe some code will help:

I have a bunch of graphic fuctions which the 
first argument to these fuctions points to 
where the fuction will 'draw' to.  i.e.

void put_pixel(char * buffer, unsigned short x...
void h_line(char * buffer, unsigned short y...
void circle(char * buffer, unsigned short x...
void bit_blit(char *buffer);

and so on.

Now, the these work just fine in the main() function:

i.e.

void main(void)
{
	char *Vid_Buffer;
	
	Vid_Buffer = (char *)malloc(307200)

	if(Vid_Buffer == NULL)
	{
	printf("Oops, not enough ram!");
	exit(1);
	}

	
	Set_640x480;  /* sets our VESA screen */
	
	/* this works just fine here */
	h_line(Vid_Buffer,10,0,639,15);
	
	circle(Vid_Buffer,320,240,100,15);

	bit_blit(Vid_Buffer);
}


Got this so far?  Okay.

Now I take all the graphic stuff after "this works 
just fine here" and move it to a function like this:

void draw_stuff(char *buffer)
{
	h_line(buffer,10,0,639,15);
	
	circle(buffer,320,240,100,15);

	bit_blit(buffer);
}

Now our main function reads:

void main(void)
{
	char *Vid_Buffer;
	
	Vid_Buffer = (char *)malloc(307200)

	if(Vid_Buffer == NULL)
	{
	printf("Oops, not enough ram!");
	exit(1);
	}

	
	Set_640x480;  /* sets our VESA screen */
	
	/* this works */
	
	draw_stuff(Vid_Buffer);
}

Still got me?  Okay.

Now, I write another function:

void draw_multi_stuff(char *buffer)
{
	unsigned short i;

	for (i=0; i<100; i++)
	{
		draw_stuff(buffer);
	}
}


And I change the main again to this:

void main(void)
{
	char *Vid_Buffer;
	
	Vid_Buffer = (char *)malloc(307200)

	if(Vid_Buffer == NULL)
	{
	printf("Oops, not enough ram!");
	exit(1);
	}

	
	Set_640x480;  /* sets our VESA screen */
	
	/* this DOES NOT works */
	
	draw_multi_stuff(Vid_Buffer);
}


The compiler compiles, but the computer crashes when
I execute the program.  I don't want to use a global
Vid_Buffer, since I have multiple buffers to write 
to.  And I need to have multi fuctions feeding the
video buffer from a fuction within a funtion, within
another fuction to keep the main() code size under
250 lines.  What went wrong?  Why does this only 
happen in DJGPP?  How do I correct this?

Keep note this is a simple example, since some of the 
things in it are unpractible.  All graphics codes do
work, and the Vid_Buffer is allocated.  Also, I am using
near pointers, and I kept the __djgpp_nearptr_enable(); and
__djgpp_nearptr_disable(); out of this code to make it 
more easy to read, but __djgpp_nearptr_enable(); is the
first statement after the varible declariation and
__djgpp_nearptr_disable(); is the last statement in the
main() function.

Thanks.
BP

- Raw text -


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