|
someobject *var=new someobject[100];
delete var;
----------------
someobject *var=new someobject[100];
var=null;
---------------
what is the difference betn these two statements?
|
|
|
|
|
I'm not sure, but I think that
someobject *var=new someobject[100];
delete var;
frees alocated memory
and
someobject *var=new someobject[100];
var=null;
changes the pointer to null
|
|
|
|
|
Does that mean, when the pointer is made null,
the memory it pointed to also gets freed.
can "var=null" be used inplace of "delete var;" ?!!
|
|
|
|
|
No, as C++ is not garbage collected var=NULL is probably an error and generates a leak in your application. Nobody will take care of the memory you leave behind undeleted. This doesn't necessarily mean a memory leak if var points to some memory block that is also pointed to by some other pointer, and you properly delete that.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
"var=NULL" is NOT the same as "delete var".
If you imagine that
BYTE *var= new BYTE [100]<pre>
allocates a lump of memory for your use (it may be longer than 100 bytes long) and sets
var to point to it.
<pre>var=NULL;
simply makes your variable point "nowhere". The memory is still allocated.
delete [] var;
frees the memory, but your variable is still pointing to the now freed patch or memory.
You could still use it, but not safely. Note the [] in the delete command as you
are freeing an array.
You should be doing something like:
BYTE *var = new BYTE [100];
...
delete [] var;
var = NULL;
Pointers are one of those things you struggle with for a while then wake up one morning
going "Ahah!"
Iain.
|
|
|
|
|
Thanks guys!!
That was tremenodus response in no time at all.
You're all great.
Life is so much easier with you.
God Bless All.
|
|
|
|
|
Hi, everyone!
I want to implement a mini-TCP protocol that can simulate
TCP's three-time shake hands method and simulate simple
"send" and "recv" method in socket programming. I want to
use raw socket but I am not sure whether there are some
alternative methods.
I just want to get simple implementions to make a concept
proof purpose of how TCP is working. Just for learning purpose.
I want to know whether there are some sample codes of this
topic that I can make a reference? Must I use RAW SOCKET?
Are there some alterative methods or alternative methods?
Thanks in advance,
George
|
|
|
|
|
Hi,
I need to change the title of a dialog box, this is what I have tried:-
<code>
CDlgCal dlgCal(this);
dlgCal.SetWindowText("Step to something");
if(dlgCal.DoModal()==IDOK)
{
blah blah blah .....
}
</code>
But I get this ASSERT error when it runs ....
<b>ASSERT(::IsWindow(m_hWnd));</b>
Any ideas?
Thanks.
Ali
|
|
|
|
|
The window hasn't been created at this point, only the object that will be managing the window.
You need to handle the WM_INITDIALOG message and call SetWindowText() then.
Software is everything. It also sucks. Charles Fishman [^]
Awasu 1.0.2 (beta)[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
Yes it's because your window doesn't exist when you call the SetWindowText function. It's better to change the text in the OnInitDialog() overloaded member function.
|
|
|
|
|
You have to do the SetWindowText inside the OnInitDialog (or later)...
The problem with Modal Windows dialogs is the Win32 Window is created & destroyed in DoModal(), i.e. it does not exist before the call.
Since MFC is supposed to be a "light wrapper", it doesn't do the same level of abstraciton as .NET or (shudder) VB.
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen
|
|
|
|
|
The assertion comes because you try to set the text before the dialog is created with DoModal(), so the dialog has no handle, and the API function fails.
To do it properly, go in the CDlgCal::OnInitDialog() (Override the WM_INITDIALOG message if you do not have that function present in the class browser), and put your code in this function. i.e. :
... CDialog::OnInitDialog(...bla...)
{
...
SetWindowText("Step to something");
...
}
~RaGE();
|
|
|
|
|
Thanks guys - for the replies - there are some nice expainations there. I've moved my 'SetWindowText' call and its all working fine now.
Thanks again, Ali.
Ali
|
|
|
|
|
hi,
i've developed a program that puts a struct into a linked list... Wenn i put the new record into my list, and i read it after that, the program crashes(wenn getting the CString)...
the problem is that my list is not updated at all... if i read a number that is inserted with the value 3, it has the value -2421523832 wenn retreiving it...
now i want to know how to update my list, not that i don't know it at all but my way is not working.
i get the pointer(Leverancier) to my struct in the function that updates the list...
<br />
NewLeverancier = new CLeverancierStruct;<br />
NewLeverancier = Leverancier;<br />
m_LeveranciersLijst.AddTail(NewLeverancier);<br />
should this work?
[]D [] []D []
|
|
|
|
|
Hi Willen,
I asked this very question a couple of days ago. Didn't you undesrstand my reply? If so, I'll gladly try to make myself clearer, if you're so kind as to tell me what you didn't get.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
well it didn't work...
i made the the struct then i send the pointer to that struct towards the function to safe it in the list... than after that i just want to put it into the linked list...
so: you say i have to do this:
<br />
<small>NewLeverancier = new CLeverancierStruct(*Leverancier );<br />
m_LeveranciersLijst.AddTail(NewLeverancier);</small>
[]D [] []D []
|
|
|
|
|
okey, i tried it one more time, and it does copy the values of the struct but the problem with the CString still remains....
[]D [] []D []
|
|
|
|
|
oke...
I'm getting pretty crazy here... it was all a compiler fault... i rebuilt my hole project and guess what: problem solved.... damn those debug files....
thanx for helping me out...
[]D [] []D []
|
|
|
|
|
You need to give us some more information. From the code above it looks like you
have a pointer which you set to a new struct instance. You then point it to
a previous instance which you add to the list...? It may already be in!
You mention strings and numbers without giving us actual facts / definitions.
Try something like the following (if you like MFC):
struct Foo
{
int nBar;
};
CTypedPtrList<CPtrList, Foo *> FooList;
Foo *f1 = new Foo;
Foo *f2 = new Foo;
f1->nBar = 3;
f1->nBar = 17;
FooList.AddTail (f1);
FooList.AddTail (f2);
Foo *f = FooList.GetHead ();
TRACE1("nBar=%i\n", f->nBar);
f = FooList.GetHead ();
TRACE1("nBar=%i\n", f->nBar);
...
There are STL equivalents to the above code, but I'm not so confident with STL to rattle
out a chunk of code off the top of my head.
Iain.
|
|
|
|
|
Hi,
I have programmed a MFC application under Win XP, and now i try to run my app on Win98.
I have copied the following files to the win98 system dir:
MFC42.dll, msvcp60.dll, msvcrt.dll
I get the following error:
The MSVCRT.DLL file is linked to missing export NTDLL.DLL RtlGetNtVersionNumbers
With Dependency Walker i see that the version of ntdll.dll not correct is for my app. When i copy a newer version of ntdll.dll to the system dir, windows doesnt start correctly.
Have i copied the wrong MSVCRT.DLL? Which DLL's do you normally use?
Thanks for helping me!
Willem
|
|
|
|
|
you can link your program statically with MFC
|
|
|
|
|
giant_hao wrote:
you can link your program statically with MFC
I have tried it, but then i get the error "Failed to create empty document", I get this error on the XP machine where i compiled the application. If i link the app normally, then the app runs fine!(under XP)
|
|
|
|
|
I can only get the menu of main frame by the operation of "AfxGetMainWnd()->GetMenu()",if I want to get the menu which is owned by specific document template,how can I do?
|
|
|
|
|
If you have access to the CDocTemplate objects that were registered in InitInstance you can get it like this:
pTemplate->m_hMenuShared
You can get a specific doc template by iterating the CDocManager object:
CDocManger = *pDocMan = AfxGetApp()->m_pDocManager;
POSITION pos = pDocMan->GetFirstDocTemplatePosition();
while (pos)
{
CDocTemplate *pTemplate = pDocMan->GetNextDocTemplate(pos);
// make use of the pTemplate here.
}
Roger Allen
Sonork 100.10016
Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003
|
|
|
|
|
I design a webservice with c#,
this webservice have some [WebMethod(EnableSession=true)]'s method
but when I use it with c++,Session lose
why?
thanks, codeproject
|
|
|
|