|
the std::vector is a dynamic array.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
If you are looking for a static size array, there is also boost::array[^].
Extra note: boost::array is in TR1[^], so if VC++ supported it, you could have used std::tr1::array by including "tr1/array" (this works in gcc), but until then, if you decide to use boost, you can include "boost/tr1/array.hpp" and use std::tr1::array (instead of boost::array).
Orhun Birsoy
|
|
|
|
|
How can I ensure that the memory allocated by new gets deleted even if if do not give a delete operator.
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
They'll manage deletion of the memory for you. But use it only when you need them, not putting them everywhere just because you are lazy
|
|
|
|
|
tom groezer wrote: even if if do not give a delete operator.
what does "give a delete operator" mean? "give" is not a standard term used in software development. Here is a tip, don't make up your own terms when posting messages as it will be difficult to understand what you are saying.
Here is what you can do with a delete operator:
* overload
* call
Which one are you speaking about?
|
|
|
|
|
u call delete as in delete p;
|
|
|
|
|
Ok
tom groezer wrote: How can I ensure that the memory allocated by new gets deleted even if if do not call a delete operator.
Then Cedric has given you the correct lead to the smart pointer[^]. However there are different models of smart pointers. There is one in STL called "auto_ptr" and there are others in the Boost library[^] that have different use models.
I recommend that you feel confident in your understanding of the different models before attempting to use them. Misuse of smart pointers can be worse than leaking memory.
|
|
|
|
|
If a base class has private functions how does the derived class call it without use of friends. is there some idiom where u can acess the private function of base class. Also is there a difference between a function and a method.
|
|
|
|
|
tom groezer wrote: If a base class has private functions how does the derived class call it without use of friends.
That's why C++ has the 'protected' keyword: your inherited class will be able to access the function but other classes won't be able to call it.
tom groezer wrote: Also is there a difference between a function and a method.
It is just a 'naming' convention: a function is considered to be a global function and a method is a member function of a class.
|
|
|
|
|
tom groezer wrote: is there some idiom where u can acess the private function of base class.
No, because it would not serve the purpose of having them private instead of protected.
Declaring members as private in a base class is to guard both variables and functions from being accessed and misused. They are used internally and the developer that designed the base class declared them private to guard against misuse.
I consider private declared base class members a powerful tool in order to make sure the class is used in the way it is supposed to be used. I think if someone feels the need for accessing private members of the base class there's a 5% chance the designer of the base class has made an error, but 95% chance that someone is trying to use the class in a way it wasn't supposed to.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Can inline functions be inherited? How?
|
|
|
|
|
I think you are talking about classes
Yes. inline functions can be inherited.
inline functions can be defined in two ways.
1) functions defined with class declarations are by default inline.
2) if you defined the function to be inline in cpp files then those functions cannot be accesed outside its visibility.
Best Regards
Raj
|
|
|
|
|
Hi all,
I have developed one small application in VC++ using Visual Studio
I am going to fix memory leakages occurred in my program in the following code
char *m_tmpChar;
m_tmpChar=new char[100];
m_tmpChar="0";
now when I am going to delete this object with delete [] m_tmpChar it is showing me error dialog saying that assertion failed _crtisvalidheappointer(pUserData)
anyone please help me to solve this problem????
|
|
|
|
|
You can't copy a char arry using =. You need to use strcpy for that purpose.
What you are doing here is storing the address of a new string in m_tmpChar. This memory was not allocated with new so you get a crash when you try to delete it.
|
|
|
|
|
char *m_tmpChar;
m_tmpChar=new char[100];
m_tmpChar="0";
delete [] m_tmpChar;
That's because you're making a fundamental mistake. m_tmpChar is not some magic string type - its a pointer to a character.
In line 2, you allocate a patch of memory big enough for 100 chars, and make m_tmpChar point there.
In line 3, you make that variable point to another patch of memory that holds the character '0' followed by a NULL. This might even be read only, depending on complier settings. In the process, you've forgotten where your recently allocated lump of 100 bytes was.
In line 4, you are asking delete to free the "0" patch of memory, which certainly wasn't created by new . As you are in debug mode, you are getting errors.
So you have 2 bugs - (a) deleting memory which wasn't created with new [giving you the fault], and (b) not deleting memory that *was* created by new [giving you the leak].
That was the long explanation. The short explanation:
Ask your teacher about strcpy , or read your lecture notes.
Pointers are one of those bits of learning that is really strange and bizarre, until it all clicks in your head. Then its just dangerous if you're not careful.
Iain.
|
|
|
|
|
This is my complete code
char *m_tmpChar;
CString temp = m_list.GetItemText(0);
m_tmpChar="1";
RegSetValueEx(hCurKey,"",NULL,REG_SZ,(unsigned char*)m_tmpChar,strlen(m_tmpChar)+1);
Now in the above code it shows me Linkage error
So can you please give me solution with strcpy function
Thanks again!!!!!!
|
|
|
|
|
Sorry one line is missing
char *m_tmpChar;
m_tmpChar=new char[100];
CString temp = m_list.GetItemText(0);
m_tmpChar="1";
RegSetValueEx(hCurKey,"",NULL,REG_SZ,(unsigned char*)m_tmpChar,strlen(m_tmpChar)+1);
|
|
|
|
|
For this code, you will get away with not using strcpy, but you will still have the buf when you come to delete m_tmpChar;
change m_tmpChar="1"; to strcpy (m_tmpChar, "1"); which will copy the fixed string 1null into your temporary buffer.
The RegSetValueEx call looks fine to me. If you are getting a linker error, then you need to see what function it it failing to link, find that, and see where it lives.
Iain.
|
|
|
|
|
Thank you so much sir!!!
Now I get solution for that error
Thanks again for your valuable time spent behind me
|
|
|
|
|
Hi, again I have a same problem for memory leakage
here I am using CFont class for setting my font but it gives me not perfect result at output
CFont* m_pBoldFont1;
m_pBoldFont1 = new CFont;
m_pBoldFont1->CreateFont(15,0,0,0,900,0,0,0,0,0,0,ANTIALIASED_QUALITY,0,"Arial");
CString test;
test.SetFont(m_pBoldFont1);
test.SetWindowText("test Section");
how can i free memory for the object(m_pBoldFont1) of CFont from memory
the above code gives me memory leakage error and also when I free memory
with delete operator it will display my string not correctly.
So, can you please help me to solve above problem
Thanks in advance!!!
|
|
|
|
|
Um, CString doesn't have a SetFont method. Or a SetWindowText method. So your above code not only has a memory leak, it wouldn't compile. CString is just a handy holder for a bunch of characters all in a row.
I'm going to make a leap in the dark, and say you're confusing it with a Label in VB...
As for the other question...
You need to keep a copy of the font around.
class CMyDialog : public CDialog
{
public:
CDialog (.....)
{
m_fMyFont.CreateSomehow ();
.....
}
....
BOOL OnInitDialog ()
{
SetFont (&m_fMyFont);
....
return CDialog::OnInitDialog ();
}
protected:
CFont m_fMyFont;
};
Obviously, I've removed a lot of boilerplate code. I'm assuming you have a dialog, or such. Create the font, and SetFont with a pointer to it. In the above example, the window will be destroyed, then the destructor of CMyDialog. At the end of that, the destructor for CFont will be called, tidying up the font.
Not all solutions involve pointers!
Iain.
|
|
|
|
|
Hi,
Sorry it's not a CString but it is a CTransparentStatic this is which I have taken from Codeproject site.
So, it is like CTransparentStatic and all the methods are maintained in this file.
So, please give me solution if possible
Thanks!!!
|
|
|
|
|
Change the scope of your font variable.
In the class this code is from, you could:
Add a member variable CFont m_BoldFont1;
In the class' constructor, call CreateFont on the object.
m_BoldFont1.CreateFont(15,0,0,0,900,0,0,0,0,0,0,ANTIALIASED_QUALITY,0,"Arial");
Once the control is created, call SetFont on it using the member variable.
test.SetFont(&m_BoldFont1);
The object will be freed when the owner class destructor is called.
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
hii all...
I want to upload a document from my asp page the asp GUI contain a browse button and sourse and destination path.....
i want to know how can i upload a file from sourse to destination.....
what i am thinking...
i need to read the content and name of the file and then i nead to make a file on destination with same name and then write the content...
please help me....
Thanks in advance........
vikas da
|
|
|
|