delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/26/21:31:21

From: fred AT genesis DOT demon DOT co DOT uk (Lawrence Kirby)
Newsgroups: comp.os.msdos.djgpp,comp.lang.c
Subject: Re: Functions in struct's... possible? How?
Date: Fri, 22 Aug 97 14:42:37 GMT
Organization: none
Message-ID: <872260957snz@genesis.demon.co.uk>
References: <33FCDA5C DOT 2353659F AT execulink DOT com> <01bcaea8$2e22c2a0$e3441ed1 AT crhodes DOT flash DOT net>
Reply-To: fred AT genesis DOT demon DOT co DOT uk
Lines: 42
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <01bcaea8$2e22c2a0$e3441ed1 AT crhodes DOT flash DOT net>
           crhodes AT flash DOT net "Cliff Rhodes" writes:

>Jeff Weeks <pweeks AT execulink DOT com> wrote
>
>> I'd like to have a function in a struct that I've created.  I realise I
>> can't just stick a functions in there, so I used a pointer to a
>> function. 
>
>That's the right approach.
>
>> char *virt = (char *)malloc(640*480*2);
>
>Did you ever check the return value from malloc()? I think you will have a
>surprise.

Maybe. Since the original article was cross-posted to comp.os.msdos.djgpp
it might be reasonable to assume that the original poster is using DJGPP
which has 32 bit ints. That means that 640*480*2 evaluates to 614400
which DJGPP shouldn't have too much difficulty allocating if the system
has enough memory. However it is certainly the case that the return value
of malloc()/calloc()/realloc() should *always* be tested and it would have
provided a further piece of evidence here. If the compiler happened not
to be DJGPP but rather one where int is 16 bits wide then there will be a
problem in the expression 640*480*2 since 614400 won't fit in a 16 bit int.
In that case you have undefined behaviour, i.e. anything at all can happen.
One of the more likely outcomes is that the value corresponds to the
low order 16 bits of 614400 which is the value 24576. In that case malloc()
would probably allocate 24576 bytes and return a non-null pointer to
that memory. 

>> driver.blit(virt);

And this would then probably crash because the allocated memory isn't as
big as the function required.

-- 
-----------------------------------------
Lawrence Kirby | fred AT genesis DOT demon DOT co DOT uk
Wilts, England | 70734 DOT 126 AT compuserve DOT com
-----------------------------------------

- Raw text -


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