Article View: borland.public.cppbuilder.nativeapi
Article #3755Re: Basic C++ Question...
From: pnunn
Date: Fri, 05 May 2006 04:18
Date: Fri, 05 May 2006 04:18
77 lines
3370 bytes
3370 bytes
Alan Bellingham wrote:
> 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
Yes, thats basically what the function does.
>
> 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.
Ok, that's the conclusion I came to last night.
>
> 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.
Bugger, missed that one.. No, its not a derived class at all, its an
array of stateLine elements. Wonder why it compiled at all.
>
> Oh, failure three - how does the code know how many elements in the
> array?
Ah, I used sizeof(table)/sizeof(*table);
>
> Alan Bellingham
So... what should I be doing? Guess I've got to use new to make the
table and the elements, then pass this pointer. Is that right? How
should I go about this?
Ta.
Peter.
Technical Details
Message-ID:
<445ad225@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.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!news.germany.com!news.musoftware.de!news.albasani.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mailReferences:
<445a02bf$1@newsgroups.borland.com> <kr2k525hn6ec8f3f8htar80mcd6d9r9d8r@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f15d7bde0eba2566 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 05 May 2006 14:18:45 +1000 From: pnunn <pnunn@infoteq.com.au> User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Basic C++ Question... References: <445a02bf$1@newsgroups.borland.com> <kr2k525hn6ec8f3f8htar80mcd6d9r9d8r@4ax.com> In-Reply-To: <kr2k525hn6ec8f3f8htar80mcd6d9r9d8r@4ax.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 144.137.61.115 Message-ID: <445ad225@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1146802725 144.137.61.115 (4 May 2006 21:18:45 -0700) Lines: 77 Path: arc1-ca.pugleaf.net!g2news2.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!news.germany.com!news.musoftware.de!news.albasani.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail