|
Nope - I've never done this stuff. If it were me, I'd write the function that takes the pointer, then I'd run it and check it like crazy for any sort of leak or problem. Or I'd look in the STL source for a similar operator to see what it gets used for.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Well, I just looked at "The Book" (St Bjarne Stroustrup), and found out that this new with two arguments is so-called "placement operator" - it usually just returns the pointer that comes as second argument. Strange...
|
|
|
|
|
Unfortunately, and for reasons that I do not understand, we do not have that book at work, hence my inability to check on such details. Sounds like a winner though.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Uff, there's a couple of misunderstandings here.
void* operator new (size_t)
{
return ::new klasa;
} This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor). Instead, write something like:
void* operator new (size_t size)
{
return ::new char[size];
} or
void* operator new (size_t size)
{
return malloc(size);
} Similarly, your overloaded delete operator should be only concerned with deallocating memory without calling (implicitly) klasa::~klasa .
Now for the second problem. Once you provide an overload for new , the compiler won't use (for the relevant class) any of the predefined global new oeprators available. In particular, it won't use the so-called placement new , whose signature is:
void* operator new (size_t size,void* pos); Placement new does not really do anything: the caller already provides the space (in pos ) where the object is to be constructed. An example of an explicit call to placement new :
void *pos;
whatever *p=new(pos)whatever; Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant:
class klasa{
...
void* operator new (size_t size,void* pos)
{
return pos;
}
}; Hope I made myself clear enough
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hello, Joaquín. Thanks for your answer.
Joaquín M López Muñoz wrote:
This is the first problem in your program. An overloaded new operator is meant to be taking care of memory allocation only: the call to the class contructor is generated automatically by the compiler. Please note that the new you provide is calling implicitly the ctor through the use of golbal ::new (altough in this case the error will go unnoticed cause kasa has a trivial ctor).
As I already wrote to Christian, my original new and delete look quite different. I replaced my code with calls to global new and delete, because I wanted to make my examlple short and clear. I am aware of the issues you are talking about.
Joaquín M López Muñoz wrote:
In particular, it won't use the so-called placement new, whose signature is:void* operator new (size_t size,void* pos);
Placement new does not really do anything: the caller already provides the space (in pos) where the object is to be constructed.
Exactly. And that's what confuses me a lot. If you take a look at the implementation of placement new and delete in <new> you will see
inline void *__cdecl operator new(size_t, void *_P)
{return (_P); }
#if _MSC_VER >= 1200
inline void __cdecl operator delete(void *, void *)
{return; }
What is this good for?
Joaquín M López Muñoz wrote:
Now, placement new is extensively used by allocators in STL, and this is what the compiler is complaining about: since you overloaded new you also have to provide an overload for the placement variant
You are 100% right. The default allocator uses placement new in function construct, and when I put this code in my class
void* operator new (size_t size,void* pos) { return pos; }
void operator delete (void*, void*) {}
everything seems to work fine.
However, I still don't understand why. What is such implementation of placement new and delete good for? I know that in some cases you can provide i.e. your custom memory pool as a second argument, but here it is not the case. Why do they use this placement new in default allocator?
|
|
|
|
|
|
Given a machine with multiple network cards, how do you tell Windows which card to use when working with Sockets?
|
|
|
|
|
hello!
i have a dialogbased mfc program.
in this programm i have several strings.
CString str='Text';
now i want the string read into a access database. the table inside the database is empty, i have only 2 columns (one column for the number of the string (automatically) and the second one for the string).
i use ODBC and tried this here:
CMyDataBase db;
db.Open();
db.m_zweiter=strText;
db.Update();
db.Close();
WHAT`S WRONG?
can somebody help me?
thanks
MFC
|
|
|
|
|
Well, without seeing what's being your Open, Update, and Close functions, it's hard to say. What's your SQL statement look like, or are you trying to populate a dataset? Besides, you declare CString str and then have a ...=strText. Was that just a typo in the example? Where is the program failing? Connection strings can be complicated to set up--maybe it's the connection string you're using. Also, have you set up an ODBC data source under "Data Sources (ODBC)" usually in the control panel or adminstrative tools program groups.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator.
|
|
|
|
|
hi!
yes, it was a mistake. both times it must be CString strText.
i tried several things, sometimes the mistake was in that row:
db.m_zweiter=strText;
or everything was fine, but nothing changed in the database.
|
|
|
|
|
|
You need to do this:
1. Create a database object and open it.
2. Create a recordset object and open it. You send the SQL statement to the Recordset.
3. See CRecordset member functions for details how to insert, update and delete rows
4. You close the recordset and then the database.
Best regards,
Alexandru Savescu
|
|
|
|
|
Does anyone know of an easy way to change the text and background colors on one edit control in a dialog box?
|
|
|
|
|
|
Handle the WM_CTLCOLOR message. There are plenty of posts here explaining the use of that message and it is in MSDN. You can check the ID of your edit control and if it is the right one, you can then set the background colour and text colour using the device context...
hope this helps
Adam.
www.beachwizard.com/travelogue[^]
"I spent a lot of my money on booze, birds and fast cars. The rest I just squandered"
George Best.
|
|
|
|
|
I get and assertion when I run this code, is the reason it fails because the m_cTabMain control isn't created when the first OnSize message comes through?
void CToolTabsDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
int ncx = cx - ::GetSystemMetrics (SM_CXBORDER);
int ncy = cy - ::GetSystemMetrics (SM_CYBORDER);
m_cTabMain.SetWindowPos (NULL, 0, 0, ncx, ncy, SWP_NOMOVE | SWP_NOZORDER);
} I put similar code in the OnPaint handler and it works perfectly. Is there anyway I can get the code to run in the OnSize handler? m_cTabMain is a CTabCtrl that I put on the dialog in the resource editor.
Thanks in advance.
- monrobot13
|
|
|
|
|
you can test to see if m_cTabMain is a window (::IsWindow(m_cTabMain.m_hWnd)) - don't call SWP if it's not a window.
-c
"[it was..] one of those evenings when you feel that not only will there definitely be a revolution, but that the Association of Manufacturers will foot the bill."
-- Umberto Eco, Foucault's Pendulum
|
|
|
|
|
the reason it fails because the m_cTabMain control isn't created when the first OnSize message comes through?
Probably. SOmetimes this trick works: insert an UpdateData(FALSE) right at the beginning of CToolTabsDlg::OninitDialog , after the call to the parent's OninitDialog .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Okay,
Just wondering if anyone has seen an STL implementaion used to iterate though an XML document.
While I don't know how this could be implemented I would have though something along the lines of a std::map in that it would be implemented as a tree (as most maps are - bit do not have to be according the specification), have a pair for the node name and the rest of it, but not have the sorting.
|
|
|
|
|
In the C# XML wrapper I wrote recently for WDJ, I did a dictionary ( i.e. a map ) of an XMLDOMNode to it's path, and one to it's value, which I guess is what you're thinking of here, yes ? It's pretty easy to do recursively.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Christian Graus wrote:
In the C# XML wrapper I wrote recently for WDJ, I did a dictionary ( i.e. a map ) of an XMLDOMNode to it's path, and one to it's value, which I guess is what you're thinking of here, yes ? It's pretty easy to do recursively.
Sounds like the kind of thing. What made me think about it was that iterating though an XML tree looks so ugly, getchild and then get its child, and then its best friends dog etc. Its stuff that has to be done over and over and aI though STL may be able to come to the rescue, as iterators can save so much time in the right places.
Only just started looking at C#, haiving bought Tom's book the other day. Not had chance to get stuck in though having been busy with things.
|
|
|
|
|
Tom's book is excellent, but there is nothing on XML in there, apart from XML comments. I think a C++ container that fits into the STL and iterates XML is a great idea. Good enough that I'm considering doing it, unless you intend to ?
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Please go ahead, and drop me a line if you need any help with testing etc. While I use STL quite a bit, I've never written any new containters and iterators before, and currently don't have the time to get stuck in, as I'm busy wrting some Excel wrappers which I use for report writing that I was thinking of posting as an article on CP in the near future
After all, you are the STL king around here.
|
|
|
|
|
Giles wrote:
After all, you are the STL king around here.
No, I don't think so Check out the top rated article this week for the STL king. I am but the clown prince.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
Christian Graus wrote:
No, I don't think so Check out the top rated article this week for the STL king. I am but the clown prince
Ahh the modesty.
Seriously, let me know how you get on, would be really interested to see how it turns out if you go ahead with it.
Right I'm off to bed, and to find my Harry Potter book. Its somewhere around here.
|
|
|
|
|