www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000224

When Created: 06/13/1998 14:08:04
Against DJGPP version: 2.01
By whom: skweints@softhome.net
Abstract: Passing a pointer to a function results in the pointer being a little off
I'm not sure if this is my fault (mainly because I'm new to C, and I'm young. ;-),
but when passing a pointer to a function, the pointer passed to the function is
a couple bytes off. For example:

function1(pbuffer2)
short *pbuffer2;
{
 //Function code here...

 // pbuffer would = 0x05c2c on my system (I will include the real source
 // at the bottom)
}

main()
{
 short *vrtbuffer;

 if((vrtbuffer = (short *)malloc(BUFFERLENGTH)) == NULL) //which ='s 0x05c30 on my system
 {
  printf("Error: Not enough memory to allocate virtual video buffer!\n");
  printf("Error: Free up some more memory!\n");
  exit(0);
 }

 //More code down here
}

I don't know if this is me, and I'm sure there's a work around for my situation,
but for those unavoidables (if any exist), I'd just like to point this out.

______________
David Pejinsky
skweints@softhome.net

Here's the source of my program, which is (sort of) a simple star simulator,
much like the Windows 95 Screen Saver.

------------------cut here--------------------
//+----------------------+
//|    Star Simulator    |
//|-=-=-=-=-=--=-=-=-=-=-|
//| By David C. Pejinsky |
//+----------------------+

#include <conio.h>
#include <dos.h>
#include <go32.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <sys/movedata.h>

//Misc definitions
#define MAXSTARS                200
#define MAXZPOS                 0xFFFF

//Video definitions
#define VIDEOMEM                0xA0000
#define BUFFERLENGTH            64000

//Macros
#define WaitRetrace()           while(!(inportb(0x3DA) & 8))

struct star
{
 int xpos;
 int ypos;
 int zpos;
 int opos;
 int color; //Not used as of yet
} stars[MAXSTARS];

void init_mcga(void)
{
 union REGS r;

 r.x.ax = 0x0013;
 int86(0x10, &r, &r);
}

void init_text(void)
{
 union REGS r;

 r.x.ax = 0x0003;
 int86(0x10, &r, &r);
}

// NewX = MaxZ * X / (MaxZ - Z) + X
// NewY = MaxZ * Z / (MaxZ - Z) + Y
// (Note: Those formula's are NOT right! ;-)

// Coord = X + (Y * 320)
// *((Buffer) + Coord) = Color

void update_stars(buffer) //Same pointer, different function (see main())
short *buffer;
{
 short vidbuffer;
 int coord;
 int i, x, y, z, newx, newy;

 for (i = 0; i < MAXSTARS; i++)
 {
  *((buffer) + stars[i].opos) = 0;
  x = stars[i].xpos;
  y = stars[i].ypos;
  z = stars[i].zpos;
  if (x >= 320 || x <= 0 || y >= 200 || y <= 0 || z <= 0)
     newstar(i);
  newx = x / (MAXZPOS - z) + x;
  newy = y / (MAXZPOS - z) + y;
//  newx = randnum(320);
//  newy = randnum(200);
  coord = newx + (newy * 320);
  *((buffer) + coord) = 30; // Color White
  stars[i].zpos -= 16;
  stars[i].opos = coord;
  vidbuffer = __dpmi_segment_to_descriptor(0xA000);
  movedata(_my_ds(), &buffer, vidbuffer, 0x0000, BUFFERLENGTH);
 }
// printf("Function \'update_stars()\': *vrtbuffer located at 0x%x\n", &buffer);
}

void generate_stars()
{
 int i;
 srandom(time(0));

 for (i = 0; i < MAXSTARS; i++)
 {
  stars[i].xpos = randnum(320) - 160;
  stars[i].ypos = randnum(200) - 100;
  stars[i].zpos = MAXZPOS - 1;
  stars[i].opos = 0;
 }
 printf("Init: Generated stars.\n");
}

void newstar(i2)
int i2;
{
  stars[i2].xpos = randnum(320) - 160;
  stars[i2].ypos = randnum(200) - 100;
  stars[i2].zpos = MAXZPOS - 1;
  stars[i2].opos = 0;
}

//I ripped this off someone, so the credit goes to them.
int randnum(max)
{
 float n=(float)rand()/((float)(RAND_MAX));
 int val=(int)floor(n*max);

 if (val>=max)
  val=max-1;
 return(val);
}

main()
{
 short *vrtbuffer;
 int i;

 if((vrtbuffer = (short *)malloc(BUFFERLENGTH)) == NULL)
 {
  printf("Error: Not enough memory to allocate virtual video buffer!\n");
  printf("Error: Free up some more memory!\n");
  exit(0);
 }
 printf("Video: *vrtbuffer located at 0x%x\n", &vrtbuffer);
 printf("Video: Press any key to continue...\n");
 while(!kbhit());
  ; //do nothing
 memset(&vrtbuffer, 0, BUFFERLENGTH);
 generate_stars();
 init_mcga();
 do
 {
  update_stars(&vrtbuffer); //Here's where the pointer passing begins.
  WaitRetrace();
 }
 while (!kbhit());
 init_text();
 printf("Exit: *vrtbuffer address = 0x%x\n", &vrtbuffer); //Same pointer, same function
 free(vrtbuffer);
 printf("Exit: Allocated memory freed.\n");
}
------------------cut here--------------------

Note added: 04/14/1999 08:00:30
By whom: broeker@physik.rwth-aachen.de
No DJGPP problem: the program shown is buggy. Passes a
(short *) variable to a function accepting (short *), but says
function(&variable). Same error in the memset() call as well.

Closed on 04/14/1999 08:00:54: Non-bug: error in user code
By whom: broeker@physik.rwth-aachen.de



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010