🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3748

Re: Basic C++ Question...

#3748
From: Alan Bellingham
Date: Thu, 04 May 2006 14:17
56 lines
2640 bytes
pnunn <pnunn@infoteq.com.au> wrote:

>Folks.. I'm stuck on a problem that should be easy to sort out but I 
>can't get to work.
>
>I have a base class defined that has a pointer to a structure in it.
>
>struct stateTable *table;
>
>and a public function setTable that takes a passed in pointer and sets 
>table to it...
>
>In the derived class I have (in the constructor)
>
>    struct stateLine binTable[] =
>    {
>	   //current state, A0, A1, next state
>	   {1, High, Low, 2},
>	   {1, High, High, 4},
>	   {1, Low, High, 5},
>	   {2, Low, Low, 1},
>	   {2, High, High, 4},
>	   {4, High, Low, 3},
>	   {3, Low, Low, 1},
>	   {3, Low, High, 4},
>	   {5, High, High, 4},
>	   {5, Low, Low, 1}
>    };
>
>    setTable(binTable);

Now, if the definition of this function is

  void setTable(stateTable * newTable)
  {
     table = newTable;
  }

then you are rightly screwed on two accounts. The first is that your
table in the constructor is on the stack, and the moment the constructor
completes, that stack memory gets used by other things.

Secondly, you are passing a pointer to stateLine to a function that
expects a pointer to stateTable. Given that this compiles, I expect that
a stateLine is derived from a stateTable. This is fine, ONLY so long as
you are passing a pointer to a single element, because with a single
element, you know better than to try indexing on that pointer. In your
case, I expect the users of table expect it to be a pointer to an array.
Bad news, because the pointer arithmetic assumes the base class.

Oh, failure three - how does the code know how many elements in the
array?

Alan Bellingham
-- 
ACCU Conference 2007 - venue to be determined.
Technical Details
Message-ID: <kr2k525hn6ec8f3f8htar80mcd6d9r9d8r@4ax.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <445a02bf$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f15d7bde0eba2566
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Basic C++ Question...
Date: Thu, 04 May 2006 15:17:45 +0100
Reply-To: alanb@episys.com
Message-ID: <kr2k525hn6ec8f3f8htar80mcd6d9r9d8r@4ax.com>
References: <445a02bf$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 3.3/32.846
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 193.129.251.253
X-Trace: newsgroups.borland.com 1146752265 193.129.251.253 (4 May 2006 07:17:45 -0700)
Lines: 56
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail