|
first
I have read that all local variables that are not initialized explicitly are being intialiazed to 0xcccccccc with the Microsoft compilers. The first OK but the other two?
second
what happens with this address if the program exits
or
is this
void func1()
{
SOME *sm = new SOME;
delete sm;
}
void func2()
{
func1();
// What happents here?
}
|
|
|
|
|
Dennis L wrote: what happens with this address if the program exits
It is reclaimed by the memory manager.
Dennis L wrote: // What happents here?
Nothing.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
I think i got it now
Thanks for your reply!
|
|
|
|
|
Dennis L wrote: Is that a problem?
In short: no, unless you're trying to use it.
The fact that you have released the memory you previously allocated does not make the pointer to point somewhere else. The memory should not be used after being deallocated since it may contain invalid data as you've instructed the memory manager that you don't need it any longer by deallocating it.
This is why it's considered best practice to assign NULL to pointers you've deallocated to be able to test on the pointer to see if you can use the memory it points to.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Dennis L wrote:
Can any one explain me why is this happening
...
before allocation (with new)
0xcccccccc
{
s0=0xcccccccc
s1=0xcccccccd
s2=0xcccccccf
}
It's for debugging purposes I think - this way it's easier to detect if you're using your data before you've initialized it.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
Well, what's being missed is that s0,s1 and s2 are char Arrays of 1,2 and 1 byte respectively, and NOT poiners! Why are they being displayed as DWORD.
What byte packing is used. Unless if you have a constructor SOME(), in which you specifically initialise the structure, you should not rely on the content of the structure because it is unpredictable.
The DEBUG version of the compiler tries to pad uninitialised and deleted structures with garbage in an attempt to make your code blow if you by mistake forget to initialise, or attempt to refer to de-allocated data.
The Release version is by no means as friendly, in that it does none of these things. If your code works while referring to data outside their validity scope, you create a timebomb which at some time in the future will cause unpredictable and mysterious crashes. I note that you read the structure after deallocation, This mere read could lead to an invalid memory access crash. (ever tried to read a NULL pointer?)
Regards,
Bram van Kampen
|
|
|
|
|
I need a constant array with pointers to two words - OK, CANCEL.
I tried:
const TCHAR Texts[2][7] = {L"OK", L"Cancel"};
It works fine, but I want to remove [7] and put [] instead, so when I change the text, I don't need to calculate the largest possible size.
Also I want to have pointers to this words ...
|
|
|
|
|
first of all, you're making a type mistake.
if you're prepending the strings with a L , then you must use WCHAR.
if you still want to continue with TCHAR, then L is not what you're after. You must use _T() around the string.
this being said, why don't you just do this :
const TCHAR* Texts[2] = { _T("OK"), _T("Cancel") };
|
|
|
|
|
Than you very much!!! It worked !!!
Actually I'm compiling for WM5.0, and TCHAR is WCHAR by default, so L is ok.
Is there a place when I can read for the foundation of C++??
I don't know the difference of TCHAR* xx and TCHAR *XX
|
|
|
|
|
akirilov wrote: I don't know the difference of TCHAR* xx and TCHAR *XX
actually, this is just a C++ language stuff.
FOO* XX has absolutely no difference with FOO *XX .
it's just a matter of taste when declaring a variable...
TCHAR however is part of the MFC, and is declared like the following IIRC:
#if defined(UNICODE) || defined(_UNICODE)
#define TCHAR WCHAR
#else
#define TCHAR char
#endif
On the same, _T() is defined like this:
#if defined(UNICODE) || defined(_UNICODE)
#define _T(x) L##x
#else
#define x
#endif
|
|
|
|
|
I know that _T(xx) is better (it works for ANSI and Unicode) then Lxx , but when I need a constant string it is easier to put just L (because for WM you almost always work with Unicode).
P.S. If you know a place on Internet, where I can learn some basic C++ conception this will be great.
|
|
|
|
|
It is not a question of "one is better than the other".
you MUST use _T() with TCHAR and L with WCHAR.
the point that UNICODE is defined is not for "WM" at all. it is defined in your project settings, and some might want to build a non unicode version.
if your code is well written then, only switching the parameter in the project settings will do. otherwise, you'd have to change every line of code using L to remove it, so that the ANSI build can perform successfully.
so be independant of that, and use _T()
|
|
|
|
|
akirilov wrote: I don't know the difference of TCHAR* xx and TCHAR *XX
xx is a different variable than XX . Whether you put the space before or after is irrelevant, as the compiler sees tokens rather than spaces.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Hi,
I have an application which executes query through ExecuteSQL statement in an postgres 8.1 windows native version.
During ExecuteSQL if the server is switched off(power off) the system gets hanged and no exception is thrown.
The reason we found out is since the communication happens through TCP/IP and the TCP_KEEPALIVE_IDLE time is 2hrs by default it takes lot of time for sending the next heartbeat request.
It can be reduced by changing the value in registry , but the problem we are facing is since it will be modified for all the TCP/IP ports,it will increase the network traffic.
Is there any way to restrict the option only to a port ? If so how to apply it to the port confiured for the datasource.
Else is there any other ways to solve the issue?
Thanks
Krishnan
|
|
|
|
|
I am using Microsoft visual c++ 6, and I want to draw shapes on a view.
What i want is a displayed shape with a
semi-transparent color i.e. we should still be able to see whatever is
beneath the shape. Can anyone help me?
Thanks.
|
|
|
|
|
You could use GDI+[^]
Gdiplus::Graphics g(hwnd, 0);
Gdiplus::Pen p(Color(64, 255, 0, 0), 10);
g.DrawLine(&p, 0, 0, 400, 400);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you very much for your answer!
It works fine, thanks again!
|
|
|
|
|
|
I have just added this section of code to the initinstance of my app.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
if (iResult != NO_ERROR) {
OutputDebugString("Error at WSAStartup()\r\n");
}
just before this line
int nResponse = dlg.DoModal();
when this is run an unhandled exception occurs
the message reads
"Unhandled Exception in MyApp.exe: 0xC0000005: Access Violation"
When I click 'OK' the code breaks at this line
size_type size() const
{return (_First == 0 ? 0 : _Last - _First); }
which is line 114 in the VECTOR.h file from the VC98\include directory.
Anyone have any ideas, I am using VC6.
Cheers
Jim
|
|
|
|
|
Could you please post the entire code snippet of your InitInstance() function? There is no chance for a vector to be in a mfc wizard generated InitInstance() . I think some other code block of your's is causing the problem.
Well, while posting code, please enclose in "code block" so that the it will be easier to read.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
jimjim733 wrote: when this is run an unhandled exception occurs
And if you remove the code, does the error go away?
Yes I realize you need sockets initialized but that is a secondary problem.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Yeap, it goes away.
Here is a copy of the InitInstance
Cheers
Jim
BOOL CMyApp::InitInstance()
{
#ifdef _AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
if (iResult != NO_ERROR) {
OutputDebugString("Error at WSAStartup()\r\n");
}
CMyDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}
return FALSE;
}
|
|
|
|
|
I can't see what's causing the exception, but is
there a reason you're not calling the base class
InitInstance()?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Sorry, not sure I follow....that is the base class of the project....isn't it?
Am I missing something here?
|
|
|
|
|
jimjim733 wrote: that is the base class of the project....isn't it?
Maybe it is (I can't see the header file) but it looks so much like
an MFC CWinApp/CWinAppEx InitInstance() override that I assumed it was
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|