www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/29/01:20:25

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: PROBLEM WITH ARRAY IN STRUCT
Date: Sun, 29 Mar 1998 01:14:46 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 114
Message-ID: <351DE6D6.6EEE@cs.com>
References: <351D93C1 DOT 60A91521 AT mail DOT ucsm DOT edu DOT pe>
NNTP-Posting-Host: ppp245.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

rpinnell AT characato DOT ucsm DOT edu DOT pe wrote:
> 
> Hi I am trying to write a program using DJGPP and the Allegro graphics
> library.  I am having a problem with an array inside a struct.  Here is
> example of the type of thing I am doing.

You're not allocating memory for the pointer variables.  Defining a
pointer variable creates a place to hold an address; you have to supply
it with an address that means something - preferably a block of memory.

> struct my
> {
> char block[336];
> }*pt;

Here, you define a global pointer variable to struct my, but there is
never any memory allocated for it to point to.  Since it's global, and
therefore static, its value is initialized to 0, or NULL.  So later in
your code, you try to write to a NULL pointer and the program crashes. 
At least, it crashes if you run it under plain DOS with cwsdpmi.  Under
Windows, it goes on merrily writing to invalid memory locations and
hoses your program's code space.

> /* START OF MAIN */
> int main()
> {
> int i=0;
> DATAFILE *df;
> char *buf;

Again, you create a pointer to char, but don't assign it to anything. 
When you call get_filename(buf) later in your code, you're passing it a
garbage pointer.  It could come up with anything; why it doesn't crash
95% of the time is a mystery to me unless you are running under
Windows...

>    /* INITIALISE ALLEGRO */
>    allegro_init();
>    install_keyboard();
> 
>     /* LOAD DATAFILE */
>    strcpy(get_filename(buf), "my.dat");

I don't understand this line at all.  Not only is buf uninitialized, but
it looks like you're trying to retrieve a filename from it, and then
copy "my.dat" to the resulting pointer.  I can imagine all sorts of
consequences from such a call, none of them pleasant.

>    df = load_datafile(buf);

Why not just 
    df = load_datafile( "my.dat" );
?

>    /* SET GRAFICS MODE */
>    set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);
>    set_palette(df[mypal].dat);
> 
>    /*   FILL  BLOCK WITH ZEROS  */
>    while(i<336)                  /* THIS SEEMS TO CAUSE LOTS OF PROBS */
> 
>       pt->block[i++]=0;         /* WHY? WHY? WHY?                    */

The problem occurs way before this line ever gets executed.  For your
information, there is a construct in C called a for loop that is far
nicer for what you've written here than a while loop.  Try this instead:

   for ( i = 0; i < 336; i++ )
      pt->block[i] = 0;

Of course, you still have to point pt to something other than NULL,
preferably with code like the following:

   pt = malloc( sizeof(struct my) );
   if ( pt == NULL )
   {
      printf( "Unable to allocate memory; exiting.\n" );
      exit( 0 );
   }

The same goes for buf; but it looks like the use of buf is completely
unnecessary in your code.

> readkey();
> return 0;
> }
> 
> The above code does nothing of course it is just to demonstrate, it
> compiles ok.  

It does not compile ok.  When I tried it (with the '-Wall' and '-O'
flags), I got the following warnings:

[WIN] D:\TEMP>gcc -Wall -O -g -o test.exe test.c -lalleg
test.c: In function `main':
test.c:17: warning: `buf' might be used uninitialized in this function

Actually, this only points out the problem with 'buf'; the problem with
'pt' is invisible since it's declared static and is therefore
automatically initialized.

I suggest you pay a bit more attention to the ways in which pointers
work in C.  It's not a simple topic, but it follows absolutely strict
rules.

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Animals can be driven crazy by pla- |
|       aka Fighteer I       | cing too many in too small a pen.    |
|   mailto:fighteer AT cs DOT com   | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself."   |
---------------------------------------------------------------------

- Raw text -


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