473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Modifying through pointer-to-const

Hello all,

In the code below, I am able to modify data through the pointer-to-const
DynGapRec parameter. The last two lines show this. I didn't want to create
a huge message, so there's a *lot* of context that's missing. If this is
not enough to work with, I will, of course, understand. My question is: Is
there *any* circumstance where this should be legal, or do I definitely have
a compiler bug (VC++ 7.1)?

Thanks,
Dave

void CInstrProcEngine::AggregateDynamicGapRecords(const CAccount* iAcct,
const SingleInstr* pInstr,
const int* evPoints,
const CGapInt* pGap,
const DynGap* DynGapRec,
CDate forecastDate,
int RateType,
int startPd,
int endPd)
{
if (pGap->i_NumGapIntervals <= 0)
return;

int index = 0;
int NumEvPts = 0;
int currentEvPt = 0;
double nMoROBal;

double* SaleAmt = pInstr->SaleAmt;
double SaleAdjustment[MAX_PERIOD + 2] = {0.0};

while (currentEvPt < LAST_PERIOD && (index < DynGapRec->mTotPds))
{
if (evPoints[currentEvPt])
{
BOOL reprFlag = FALSE;

NumEvPts++;

CDate tempDate;
tempDate = forecastDate;
tempDate.AddMonths(currentEvPt);

SaleAmt = pInstr->SaleAmt;
if(!pInstr->SrvcFlag)
{
double notUsed = 0;

CalcSalesAdjustment(
currentEvPt,
endPd + 1,
pInstr->UnModContractualRo,
pInstr->OvPrepayments,
pInstr->EvInterestCashflow,
pInstr->SaleAmt,
forecastDate,
SaleAdjustment,
notUsed,
false
);

SaleAmt = SaleAdjustment;
}

// calculate gap buckets
for (int gapRecCnt=0; gapRecCnt < pGap->i_NumGapIntervals;
gapRecCnt++)
{
DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!
DynGapRec->GapInteval[index] = pGap->i_GapEndMt[gapRecCnt]; //
ILLEGAL!!!

.. . .
.. . .
.. . .
.. . .
}
Jul 22 '05 #1
5 1504
On Mon, 19 Apr 2004 13:52:29 -0700, "Dave" <be***********@yahoo.com>
wrote:
(snip)
DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!
DynGapRec->GapInteval[index] = pGap->i_GapEndMt[gapRecCnt]; //
ILLEGAL!!!


It looks like MyPeriod and GapInterval are pointers, not arrays. In
this case they are const (because of your passing a const DynGap*
DynGapRec), but what they point to is not.
Jul 22 '05 #2
Dave wrote:

In the code below, I am able to modify data through the pointer-to-const
DynGapRec parameter. The last two lines show this.
Is there any circumstance where this should be legal, or
do I definitely have a compiler bug (VC++ 7.1)?

void CInstrProcEngine::AggregateDynamicGapRecords(const CAccount*
iAcct, const SingleInstr* pInstr,
const int* evPoints,
const CGapInt* pGap,
const DynGap* DynGapRec,
CDate forecastDate,
int RateType,
int startPd,
int endPd)
{ [. . .] DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!
DynGapRec->GapInteval[index] = pGap->i_GapEndMt[gapRecCnt]; //
ILLEGAL!!!


Dave,

This would be legal if MvPeriod and GapInteval are pointers to non-const
data. Assuming this is the case, you're changing the values of data being
pointed to, not the values of the pointers per se. Since the pointers are
members of DynGap, and the data being pointed to are not, the assignments
don't affect the state of DynGapRec (as far as the compiler is concerned,
anyway).

If this is not what you want, you might consider hiding MvPeriod and
GapInterval, and providing non-const member functions to alter the data they
point to.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #3


Russell Hanneken wrote:
Dave wrote:
In the code below, I am able to modify data through the pointer-to-const
DynGapRec parameter. The last two lines show this.
Is there any circumstance where this should be legal, or
do I definitely have a compiler bug (VC++ 7.1)?

void CInstrProcEngine::AggregateDynamicGapRecords(const CAccount*
iAcct, const SingleInstr* pInstr,
const int* evPoints,
const CGapInt* pGap,
const DynGap* DynGapRec,
CDate forecastDate,
int RateType,
int startPd,
int endPd)
{


[. . .]
DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!
DynGapRec->GapInteval[index] = pGap->i_GapEndMt[gapRecCnt]; //
ILLEGAL!!!

Dave,

This would be legal if MvPeriod and GapInteval are pointers to non-const
data. Assuming this is the case, you're changing the values of data being
pointed to, not the values of the pointers per se. Since the pointers are
members of DynGap, and the data being pointed to are not, the assignments
don't affect the state of DynGapRec (as far as the compiler is concerned,
anyway).


I've tested in 7.1 and this is the case. If the member is declared
const, then the compiler will balk.

Best, Dan.

--
http://lakeweb.net
http://ReserveAnalyst.com
No EXTRA stuff for email.

Jul 22 '05 #4
On Mon, 19 Apr 2004 22:00:11 GMT, Dan Bloomquist
<EX***********@lakeweb.com> wrote:
I've tested in 7.1 and this is the case. If the member is declared
const, then the compiler will balk.


Don't confuse a const pointer member with a member which is a pointer
to const, i.e., T* const p (a const pointer) with const T* p (a
pointer to const). Anyhow, if T* m is a member of a class C which is
passed by a const C* c, c->m is treated as being of type T* const, not
of type const T*

Jul 22 '05 #5


Walter Tross wrote:
On Mon, 19 Apr 2004 22:00:11 GMT, Dan Bloomquist
<EX***********@lakeweb.com> wrote:
I've tested in 7.1 and this is the case. If the member is declared
const, then the compiler will balk.

Don't confuse a const pointer member with a member which is a pointer
to const, i.e., T* const p (a const pointer) with const T* p (a
pointer to const). Anyhow, if T* m is a member of a class C which is
passed by a const C* c, c->m is treated as being of type T* const, not
of type const T*


Hi Walter,
Thank you.

I tested first as the op posted:
DynGaRec->MvPeriod[index] = currentEvPt; // ILLEGAL!!!

So he was not modifying the pointer.

As you say:
struct TS {
int* test;
};

void test( const TS* test )
{
test->test= NULL; //const error.
*test->test= 2; //ok.
}

also:
struct TS {
const int* mtest;
};

void test( TS* test )
{
*test->mtest= 2; //const error.
test->mtest= NULL; //ok.
}

and if:
struct TS {
int* const test;
};
void test( TS* test )
{
*test->mtest= 2; //ok.
test->mtest= NULL; //const error.
}

It appears 7.1 is working properly.

Best, Dan.

--
http://lakeweb.net
http://ReserveAnalyst.com
No EXTRA stuff for email.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
11
by: Jason Heyes | last post by:
Look at my code: void modify_element(BigType &x) { /* not shown */ } BigType modify_element_copy(BigType x) { modify_element(x);
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
13
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
2
by: Pankajmani das | last post by:
Dear Sir/Madam, I have developed a QUIZ game program using VB 6.0 and Access as back end. Sir/Madam, please let me know, how can I protect the user by using VB code for modifying that perticular...
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
2
by: anujzcool | last post by:
Is there any way of accessing and modifying configuration properties at runtime.
3
by: =?Utf-8?B?Q2hhcmxlcw==?= | last post by:
Hi: I'm looking for an example of modifying the registry of a remote system. Thanks much! Charles
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.