|
Hi,
I have the following situation.
class CPCntPPg : public CPropertyPage
{
DECLARE_DYNCREATE(CPCntPPg)
// Construction/destruction.
public:
CPCntPPg();
~CPCntPPg();
// Dialog Data.
//{{AFX_DATA(CPCntPPg)
enum { IDD = IDPP_PCNT };
CVQProgress m_ctlVQProgress; // CStatic derived class.
//}}AFX_DATA
CEdit m_ctlVQ[VQPNUM];
CSpinButtonCtrl m_ctlVQSpin[VQPNUM];
int m_nVQ[VQPNUM];
:
// Overrides.
// ClassWizard generate virtual function overrides.
//{{AFX_VIRTUAL(CPCntPPg)
public:
virtual BOOL OnApply();
protected:
virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support.
//}}AFX_VIRTUAL
// Implementation.
protected:
void InitPPgData(); // Initializes the property page.
void InitSpinButtonCtrls();
:
// Generated message map functions.
//{{AFX_MSG(CPCntPPg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
afx_msg void OnChangeVQ(UINT nID);
:
DECLARE_MESSAGE_MAP()
};
// EN_CHANGE handler. When the contents of an edit control is changed by the user I want the
// m_ctlVQProgress control to redraw itself to reflect the changes.
void CPCntPPg::OnChangeVQ(UINT nID)
{
int nVQ; // Contents from changing edit control retrieved by GetWindowText() and
// _ttoi().
:
m_ctlVQProgress.SetVQ(nIndex, nVQ, TRUE /*Redraw*/); // This will initialize a data
// member and redraw the control.
}
The problem is when the edit control is set to an initial value by DDX/DDV in OnInitDialog
the OnChangeVQ handler gets also called causing the m_ctlVQProgress control to draw itself. This
happens before the OnPaint handler of the m_ctlVQProgress control is invoked. Is there any way to prevent this from happening ?
TIA
|
|
|
|
|
What's different about the initial state of the progress control compared to when a change is
made to the edit control later by the user?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
What is cache hit rate in inling and why does too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of
instruction fetch from that of cache memory to that of primary memory.
|
|
|
|
|
Where did that question come from?
Are you talking about registers?
Inlining is basically like macros, where it injects the instructions into the code instead of calling the actual function, which could produce bloating, but will reduce the cost of calling a function.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Steve Echols wrote: Where did that question come from?
The same place that all of his questions come from. There seems to be no rhyme or reason behind them, other than for the sake of asking a question.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
"Cache hit rate inlining"
I would describe it as a technique used to avoid conditional jump instructions and reducing function call overhead.
Here are some reasons how/why __inline functions can possibly make code faster.
1.) In machine code when you conditionally jump from one address to another, the cpu branch predictor may choose the wrong path and will not prefetch the correct instructions. This is known as a cache miss.
More on cpu branch prediction:http://en.wikipedia.org/wiki/Branch_prediction[^]
2.) There 2 types of function linkages in machine code. The first is a leaf function. A leaf function is a function that does not call any other function. Its the end of the line or 'A leaf' like on a tree. This function may (possibly)be linked differently than other functions by the compiler (it may be inlined depending on the choice of the compiler). The second type of function is a non-leaf function which is calling other functions inside its body. It looks visually more like a tree branch with other branches or leaves atatched. (At least in my twisted mind!)
Now... When you call a non-leaf function linkage (most leaf too), the first thing that happens in __asm code is the function-prologue. The stack and registers are prepared for usage. http://en.wikipedia.org/wiki/Function_prologue[^]
As you can imagine... this requires some cpu cycles... which = time.
After the function has executed... the function must restore the stack and registers to its previous state... so the function who called it can continue processing! Otherwise the function which called function 2 would be completely confused. All of its registers and the stack would be different!
Here is an example from a project I am currently working on demonstrating a prologue and epilogue in a function.
A function which has been __inlined (if it was actually inlined) does not have this prologue/epilogue overhead.
Now you also asked how __inlining functions can cause increased cache misses. Well, __inline functions make code much larger. The increase in code size may cause a small, critical section of code to no longer fit in the cache, and this will cause cache misses!
Hope this helps,
-Randor (David Delaune)
|
|
|
|
|
Why is writing a function that returns a dereferenced pointer is a memory leak just waiting to happen. The function here initializes a reference to the object returned. In that case is the only alternative left is to return objects by value. When is it that objects are returned by reference and when is it that they are returned by value.
|
|
|
|
|
first of all, you never return a reference to an auto object( one created on the stack) because once you leave that scope, the object self destructs and you're left with a reference to nothing!
also you should not create an object on the heap and then return a reference to that object, because then you will have a memory leak because the allocated memory is never freed, the object done not self destruct when the reference goes out of scope!!!
would be cool if it did, but it doesn't.
so return a object on the stack, make sure the object can copy itself correctly if you're going to return objects by values, that is make sure the object has a copy constructor that does all the right things if you're doing anything specialized with that object, otherwise the complier will try to do a good job in trying to copy only auto class members, for pointer only the what the pointer points to will get copied but not what is pointer do! this is call a shallow copy....a deep copy is something that makes a copy of the pointer to things, but you will not have to worry about this when you're returning objects by value.
you will have to worry about this if you're want to clone an object, then you need to do a deep copy!
Yours Truly, The One and Only!
|
|
|
|
|
Why is a reference then returned in case of assigment operator where a dereferenced pointer is returned?
|
|
|
|
|
good question, in this case you're either dealing with the same object or an object that has been passed in. The main point is this object existed before it enter the function scope!
think about it!;P
Yours Truly, The One and Only!
|
|
|
|
|
tom groezer wrote: Why is a reference then returned in case of assigment operator where a dereferenced pointer is returned?
Bad example. That's not required in an assignment operator - it's customary, so you can use
the result on the right side of another assignment operator.
From the docs:
"The operator returns the object to preserve the behavior of the assignment operator, which
returns the value of the left side after the assignment is complete."
Assignment operators for a class can be overloaded to take whatever argument type you want and
return any type. For example:
class A
{
int integer;
public:
A() {integer = 0;}
int GetInt() {return integer;}
A &operator=( A &a )
{
integer = a.integer;
return *this;
}
int operator=( int i )
{
integer = i;
return i;
}
};
class B
{
double dbl;
public:
B() {dbl = 0.0;}
B &operator=( double newdbl )
{
dbl = newdbl;
return *this;
}
B *operator=( B *b )
{
dbl = b->dbl;
return this;
}
B &operator=( A &a )
{
dbl = (double)a.GetInt();
return *this;
}
};
...
A a1;
A a2;
B b1;
B b2;
a2 = 3;
a1 = a2;
int i = a1 = 4;
b1 = 1.25;
b2 = 2.50;
B* bp = b1 = &b2;
b2 = a1;
With power, comes responsibility
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
In your second exapmle u r returning a reference to a object allocated on the heap. IS it correct. Will it be correct if I retruned the same way from other functions of a class or is it that we could this only in assignment and copy constructor. what is the bottomline?
|
|
|
|
|
I use 4 objects, all are on the stack. No objects are heap allocated.
tom groezer wrote: Will it be correct if I retruned the same way from other functions of a class or is it that we could this only in assignment and copy constructor.
The same scoping and memory management rules apply as anywhere. It's up to us as programmers to
keep track of that.
Returning a reference or pointer to an automatic (stack) variable isn't going to work if the
caller tries to use it. The object has gone out of scope so it doesn't exist.
Any objects created in a function and returned to the caller need to be cleaned up somehow.
The assignment operator example was meant to show that you can use any argument or return value.
Doing so beyond a normal assignment can make code hard to read. An assignment operator for a
window class that takes a document object and returns a file pointer isn't going to make much
sense, but you can do it. You can also do anything you want in the implementation - you don't
have to actually assign anything. Again, it would add confusion - we normally expect '=' to
do an assignment.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
Hi all. Im trying to make something that will have a random word every time it starts up. I made a two word array and its not working like i had hoped.
<br />
#include <iostream><br />
using namespace std;<br />
<br />
char* randomlist[]={<br />
"Test", "Testing"<br />
};<br />
<br />
<br />
<br />
int main(){<br />
cout << "Random list is: " << randomlist << endl;<br />
system("pause");<br />
return 0;<br />
}<br />
With the output of:
Random list is: 00475DC0
Press any key to continue . . .
Hardly what im trying to do. Any suggestions? Thanx in advance!
|
|
|
|
|
I'm a C programmer rather than a C++ programmer (so bear with me :p)... but wouldn't you have to specify in cout which array element you would like to print.. eg randomlist[0] or randomlist[1].. because at the moment I assume that it is just printing the pointer to randomlist to the screen..
Hope this helps!
--PerspX
"Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates
|
|
|
|
|
What you're seeing is the address to the first element in the array. You need to use array indexes like randomList[0] , etc...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
What is wrong with the following?
long value;
value &= 0xFFFF;
|
|
|
|
|
tom groezer wrote: 0xFFFF
Only covers 16 bits, long is 32 bits so it ought to be 0xFFFFFFFF
Since the statement before is long value; , who knows what it is initialized to.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
"value" isn't initialized before masking the low-order 16 bits.
Otherwise, nothing.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
also...be careful with the definition of "long". We get used to it being a 32 bit integer when
Win32 programming but the language definition does NOT require a long to be 32 bits.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the number 3
|
|
|
|
|
Mark Salsbery wrote: language definition does NOT require a long to be 32 bits.
That is true. Doing a quick sizeof doesn't hurt to do from time to time. :->
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
"value" is uninitialized. Givce it a value of 0, and you should be fine.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Thnkx a lot for "floating point comparisions".............
Does this mean IEEE32/64 arent good enough to store FPs accurately AND wat when we use the same number to print on screen......i mean how is that FP printed accurately?
Spread wat u Know!
|
|
|
|
|
Read What Every Computer Scientist Should Know About Floating Point Arithmetic[^].
A binary floating-point number can only store numbers that are sums of powers of two. The digits to the right of the point are sums of negative powers of two. Just as 1/3 is not accurately representable in decimal (0.33333.... etc), 1/5 is not accurately representable in binary. The more operations you perform on a limited precision representation, the more representation error builds up. That's why it's rarely a good idea to directly compare two floating point numbers.
I don't think there are any values representable in binary that are not representable in decimal, because two divides evenly into ten. However, when displaying a floating point result, it's common to only show some decimal places, displaying a rounded result.
If you want decimal values, with a fixed decimal point, to be stored and computed accurately, you should use a scaled integer. This is not supplied by standard C++ so you'll have to find an implementation, e.g. here[^].
|
|
|
|
|
Is there anything wrong with this program
T *p = 0;
delete p;
Something to do with overloaded delete operator?
|
|
|
|