|
Hi,
Generally people use 'new' keyword to memory allocation. But I red that it will slow down the process or project. I also know that using virtual memory is one of solution to it. But the thing is, I am not getting how to implement this code in C++/MFC. Please help me to achieve this feat by providing any one of simple program.
Thanking you,
-Jagadeesh Musham
|
|
|
|
|
I will make a citation that I read somewhere (sorry, I don't remember from who it was):
"Early optimization is the root of all evil". Specially in your case if you don't know exactly what you are doing and you just heard that somewhere.
So, are you having troubles ? Is your program becoming slow ? Are you allocating/dealocating a lot (and when I say a lot, read it as really a LOT) of objects ?
If no, then you will probably make things a lot more complex and probably much buggy than they are. Because if you need to implement a system that manages memory allocation in a more efficient manner it's going to be quite complex.
Finally, I really would like to see where you read it and if you didn't interpret the thing out of context (or maybe the author is just plain stupid). C'mon you're not going to experience slowdown if you allocate a bunch of integers or objects.
|
|
|
|
|
Cedric Moonen wrote: I will make a citation that I read somewhere (sorry, I don't remember from who it was):
"Early optimization is the root of all evil".
Knuth: We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." (Knuth, Donald. Structured Programming with go to Statements, ACM Journal Computing Surveys, Vol 6, No. 4, Dec. 1974. p.268.)
BTW, my favorite one on the topic is:
“More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity.” - W.A. Wulf
|
|
|
|
|
Thanks. That's a nice one, I agree
|
|
|
|
|
Virtual memory is just a technique of pretending to have more RAM than you physically have. It's not a memory allocation technique for C++ objects, etc.
The main thing that will speed things up for you is to not do more allocation than you need - or repeat yourself if you need to do slow things, like connect to databases, etc...
These sorts of ideas are massively dependent on the details of your application.
But write it one way, and do some actual meaurements, and then try caching, delayed allocation, and all sorts of fun & games. Measure, then find out the chances are you've shaved some unnoticable milliseconds off your time.
Iain.
|
|
|
|
|
new is just fine.
Write the program, make it work, then optimize this stuff later.
I'd buy a better memory manager if you're not happy with the default one. (look at SmartHeap)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</A>
|
|
|
|
|
I have stored monochrome pixel values of an image in a buffer of type int**. Now I would like to create a bitmap out of it to use BitBlt() function to display it. How do I create a bitmap out of raw pixel values.
|
|
|
|
|
CreateBitmap()
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
You beat me to it - but I plugged your image viewer helper app!
Iain.
|
|
|
|
|
HBITMAP CreateBitmap(
int nWidth,
int nHeight,
UINT cPlanes,
UINT cBitsPerPel,
CONST VOID *lpvBits
);
Have a look at the CreateBitmap (shocking name, I know) command - you can specify a pointer to data which will make up the bitmap. There are some issues with it - you'll need the data to match the bits per pixel, etc.
Or CreateDIBitmap may be better for you.
You'll have to transform you data into a format those command will be happy with though - and how hard that is depends on how "friendly" your format is.
If you search codeproject by author for PJ Arends, you can use his Image Viewer article to help you. It's great if you're fiddling about with DCs, and Bitmaps - it's saved hours of development for me in the past.
Iain.
|
|
|
|
|
I create an abstract class and a class derived from it. I know that an abstract class's object cannot be created until all its pure virtual functions are redefined within their derived class. However, How about the constructor and destructor functions? As they are special class functions, they can not be redefined within their derived class.
class Abstract_base {
public:
virtual ~Abstract_base()=0;
virtual void interface1() const = 0;
virtual const char* mumber() const
{
return _mumble;
};
protected:
char *_mumble;
};
class Concrete_derived : public Abstract_base {
public:
Concrete_derived()
{
};
virtual void interface1() const {};
};
int main
{
Concrete_derived trouble;
return 0;
}
The code above has link eror.(error LNK2019)
If I change "virtual ~Abstract_base()=0;" to "virtual ~Abstract_base()=0; {}", the link error disappeared. Why shall we add "virtual" to the destructor function?
Can you give me some explanation?
many thanks
Tomorrow is another day!
|
|
|
|
|
You shouldn't put a =0 after your destructor. This means that the function is a pure virtual function: the function be implemented by all child classes. It is quite odd to put that on a destructor.
A destructor is always required (one is supplied by default if you don't write it yourself) so, you need to provide a body. Which you did by adding {} . This is just an empty body, but it is still a body.
If you don't provide a body for your destructor, you'll get a linker error.
|
|
|
|
|
Cedric Moonen wrote: It is quite odd to put that on a destructor.
What? It is exactly what any abstract base class should have. Did you ever read Effective C++ by Scott Meyers?
|
|
|
|
|
led mike wrote: Did you ever read Effective C++ by Scott Meyers?
Nope but I was planning to buy it soon
led mike wrote: It is exactly what any abstract base class should have.
Yes, I see. I never done it that way: I always used at least one pure virtual function.
|
|
|
|
|
richardye wrote: Can you give me some explanation?
You must provide a definition of the pure virtual destructor.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Destructors *are* different from normal memeber functions. If you have a normal member function, you can choose whether to call the ancestor's implementation or not.
eg:
void CMyDialog::DoDataExchange (CDataExchange *pDX)
{
....
CDialog::DoDataExchange (pDX);
}
While it's good practise to call the ancester method in the example above, it's not required to by C++ - so DoDataExchange could have been a virtual =0 member.
BUT...
Destructor's don't get the choice of whether to call the ancester destructor or not. When CMyDialog::~CMyDialog is finished, CDialog::~CDialog is run whether you want it or not. In which case, it needs to exist - even if it's very boring (eg { } ) as you've found out.
I hope that helps a little.
Iain.
|
|
|
|
|
You mean that no matter how I should define the pure destructor function.
But there is still something I cannot understand very well.
If in your class you have a pure member function, you must show that any class derived from it should implement your pure function. Isn't it?
But if you define a destructor function to be pure, you cannot implement it in your derived class and you have to add "{}" in your base class. I think it is meaningless. Why shall we use pure destructor function in a class?
Many thanks.
Tomorrow is another day!
|
|
|
|
|
Hi all.
In a program I am currently writing, I would initiate to add some functions based on a right-click of a particular edit control. However, CEdit catches the OnRClick event itself, in order to build a pop-up menu of its own (with stuff like "copy", "paste", etc). Is there a way in which I can add my own items to this pop-up menu? (and of course, respond to them within the CEdit's parent) If not, I would like to send a NM_RCLICK notify message to the parent, overriding the CEdit's OnNotify handler, so that the parent can build its own pop-up menu, and leave the CEdit's pop-up menu unused. However, when doing so, how do I get the control's id (since I need to put this in the NMHDR structure in order to identify the control that sent the notification message).
Any help would be very much appreciated.
Regards,
William
|
|
|
|
|
William Engberts wrote: However, when doing so, how do I get the control's id
GetDlgCtrlID() ?
|
|
|
|
|
|
|
Thanks a lot! I am continuing work based on your article.
Regards,
William
|
|
|
|
|
Hello,
I have Microsoft Excel 2003 and want to open in VC++ through CreateDispatch() method of _Application .
_Application app;
app.CreateDispatch("Excel.Application");
With the above I get an error.
What is the parameter that I should use in CreateDispatch() or what are the files that I should check in my directory which are necessary to run _Application.
Pritha
|
|
|
|
|
Hi Pritha,
You should preceed your code with "CoInitialize (NULL);". Next to this, it is better in my experience, to check is Excel is already running and use the running instance of excel if it is. Only start a new instance if no excel is running. I use the following routine:
BOOL OpenExcel (LPCTSTR szFilename)
{
_Application Excel;
BOOL bExcel = FALSE;
CLSID clsid ;
LPDISPATCH lpDispatch ;
LPUNKNOWN lpUnk ;
HRESULT hr;
COleException * Error = new COleException;
CoInitialize (NULL); // We must be initialized first!
//------------------------------------------------------------------------------------
// Check if Excel is already running
//
if (CLSIDFromProgID(OLESTR("Excel.Application"), &clsid) == NOERROR)
{
if (GetActiveObject (clsid, NULL, &lpUnk) == S_OK)
{
hr = lpUnk->QueryInterface(IID_IDispatch, (LPVOID*)&lpDispatch) ;
lpUnk->Release() ;
if (hr == NOERROR) // Yep, excel is already there
{
Excel.AttachDispatch (lpDispatch, TRUE) ;
bExcel = TRUE;
}
}
}
if (!bExcel) // Excel was not yet running
{
bCreated = TRUE; // we now created an instance of Excel ourselves
bExcel = TRUE; // start from a positive assumption
try
{
if(!Excel.CreateDispatch("Excel.Application",Error)) // Create the instance
throw Error; // throw an error on failure
}
catch (COleDispatchException * Error) // Oops... could not create...
{
CString strMessage;
strMessage = "Could not launch Microsoft Excel\r\n";
if (!Error->m_strSource.IsEmpty())
{
strMessage += Error->m_strSource;
strMessage += " - ";
}
if (!Error->m_strDescription.IsEmpty())
strMessage += Error->m_strDescription;
else
strMessage += "unknown error";
MessageBox (HWND_DESKTOP, (LPCTSTR)strMessage, NULL, MB_OK|MB_ICONERROR);
bCreated = FALSE; // no Excel created afterall
bExcel = FALSE; // and no Excel running....
}
}
Error->Delete (); // we do not need the Error exception
return bExcel;
}
|
|
|
|
|
Hello,
Your code above was useful.I wanted to find out how many rows should be read from the sheet.For that I have used the following but it doesn't work.
LPDISPATCH lpDisp;
lpDisp = sheet.GetUsedRange();
range.AttachDispatch(lpDisp);
range.Select();
range.GetRows();
long lTotalRows = range.GetCount();
Any ideas
Thanks
Prithaa
|
|
|
|