|
_AnShUmAn_ wrote:
This is how it is done in VC7
Nothing VC7-specific about it. This is how it has been done in C as far back as I can remember.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
how i link two MFC projects to visual C++ 6.0 ??
-- modified at 0:39 Thursday 1st June, 2006
|
|
|
|
|
U mean, inserting a project into another project.
Do this.
Right Click on the workspace,then click on Insert project in to workspace.
then u can observe both the project in the same workspace.
hope this helps.
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
Hi,
I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
|
|
|
|
|
|
Anu_Bala wrote: I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
Well some of the things I know I would like to share.
- Use
new and delete .
- Always use
delete[] with new[] and delete with new - Do not mix up c, c++. Either you use C, or use C++ for memory allocation.
- Always set a pointer to
NULL after deletion.
- Always allocate sufficient amount of memory. Do not allocate memory like this...
char *str = new char[ strlen( someStr ) ];
strcpy( str, someStr );
delete [] str;
str = NULL;
instead use
char *str = new char[ strlen(someStr) + 1 ];
strcpy( str, someStr );
delete [] str;
str = NULL;
- Try using
auto_ptr ;
- Maintain a list of pointers to memory that you have allocated. When you exit from your app you can delete them all at one go if any of them hasn't been deleted.
- Always check for
NULL on pointers after a new .
- Always check for
NULL on pointers before using them.
- Be really really afraid to dynamically allocate memory.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
Nibu thomas wrote: strcpy( str, someStr );// well asking for trouble
True, use strncpy() instead.
Nibu thomas wrote: delete [] str;//error here
Why do you assert this is in error?
Nibu thomas wrote: Be really really afraid to dynamically allocate memory.
Why?
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
DavidCrow wrote: Nibu thomas wrote:
delete [] str;//error here
That was in relation to the point I mentioned. i.e. to allocate sufficient amount of memory. Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block. And when you try to delete such pointers it will result in memory damaged error message.
strcpy was used here just to regenerate that issue.
DavidCrow wrote: Nibu thomas wrote:
Be really really afraid to dynamically allocate memory.
Why?
Yeah, use it as a last resort. For eg: using standard stl classes can avoid most of the issues.
Note: I didn't say not to use it but instead to be afraid. Sometimes being afraid is good.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
Nibu thomas wrote: Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block.
But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.
Nibu thomas wrote: For eg: using standard stl classes can avoid most of the issues.
Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
DavidCrow wrote: But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.
Exactly, I was just demonstrating how such things could happen.
DavidCrow wrote: Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.
Exactly, they work by allocating on the heap. But then we don't have to do it. These are proven classes (AFAIK) which work efficiently. Hence using them would be best for us. These classes takes the onus on them to do memory clean up.
Nibu thomas
A Developer
Programming tips[^] My site[^]
|
|
|
|
|
I suggest you to read Effective C++ 2nd Edition or the latest 3rd Edition.
If u r confused about pointers, read Pointers in C to know more about pointers.
as nibu said if u r much concerned about pointers, try using auto_pointers that is the best way to handle memory leaks. but I suggest that not to use auto_pointer because simply u can handle those allocation and deallocation of objects.
In MFC, it is not using any special allocation strategy, its all C++.
Code Complete 2 is a book which contains some best practices.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
SaRath C wrote: In MFC, it is not using any special allocation strategy, its all C++.
That is not entirely accurate.
See this MSDN Article[^]
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
Thanks for ur information. It was quite new for me.
Anu said that (he/she) is new to MFC and suffering with some exception and all.
Everything has an user point of view. We are the users of C++ right, at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact. never I meant any regarding internal handling of compiler. I think the language I used was something not proper
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
-- modified at 1:55 Thursday 1st June, 2006
|
|
|
|
|
SaRath C wrote: at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact.
ah, I understand now, thanks.
-- signature under construction -- -pete
|
|
|
|
|
Need a way in C++ to determine how much memory a user has on their graphics card. Either DirectX or some other way. Any leads?
===================
Matthew R. Miller
www.bytemycode.com
www.codeandcoffee.com
|
|
|
|
|
Im not sure maybe it is some helpful to you Here[^]
whitesky
|
|
|
|
|
I just moved some code from a CWinThread into a new thread (standard thread) and am now getting stack overflows (while accessing a COM engine -- "DesktopSearch").
Any clues for me?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc.</A>
|
|
|
|
|
Have you initialized com for the new thread?
John
|
|
|
|
|
absolutely!
I played with different CoInit calls to see if WDS would behave better...
But, I am CoInit'ing in the thread...
What are the issues that surround stack and threads? Is this additional stuff surrounding COM and/or DCOM?
Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc.</A>
|
|
|
|
|
In windows, if u have specified initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. may be u ur program requires more stack or encoutering with some loops which do some allocation. Pls check ur code.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
Hi ~
I have an MFC application using automation that needs to open an access database, perform a query to get a recordset and then copy the recordset to Excel. I found an article on the microsoft site that explains how to do it, but I am getting an error = "The server threw an exception"
The article is at: support.microsoft.com/kb/243394/en-us
I am using Visual C++ 6.0 and Excel/Access 2003 (although my file is Access 2000 format).
Any ideas on what I might need to do to fix it? Thanks so much for your help!
Jen
|
|
|
|
|
Have you single-stepped through the code to find which statement is throwing the exception?
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
I have to write a clinet/server application with sockets and the client use CODBC classes to conect to a local .mdb file and i don't know what to choose for server implementation .
I have tried CDAO clases but DAO doesn't suport multithreading
Please can anyone tell me what I have to use so i can connect to a db engine o server application and be able to use multithreading?
Eg the server thread
UINT ServerThread (LPVOID pParam)
{
threadstruct* ts = (threadstruct*)pParam;
char buffer[BUFFERSIZE];
int size = BUFFERSIZE;
int blabla=0;
cout << "a new client";
while(1)
{
int sz = ts->pSock->Recv(buffer,size,ts->indice);
if(sz == 0)
{
ts->pSock->ServerClose(ts->indice);
delete ts;
return 0;
}
if (sz>3)
{
CMyMessage msg(buffer);
switch(msg.GetMyMsgType())
{
case 0:
cout << endl;
cout << msg.GetMyMsgComm() << " -> " << msg.GetMyMsgText() << endl;
if (!strcmp(msg.GetMyMsgComm(),".SD4P"))
{
CString strComm;
strComm.Format(".SD4P");
CString strText;
strText.Format("return .SD4P");
CMyMessage msg(strText.GetBuffer(strText.GetLength()),strText.GetLength(),strComm.GetBuffer(strComm.GetLength()),strComm.GetLength(),0);
char buffer1[253];
int size1;
msg.GetMyMsg(buffer1,&size1);
ts->pSock->Send(buffer1,size1,ts->indice);
// here i have to implement a db connection to extract some fields and to send the obtained data to client. CArticolSet is a CDAO inheritance and does not work
/*WaitForSingleObject(hMutex1, INFINITE);
CArticolSet* artset = new CArticolSet();
artset->GetDefaultDBName();
artset->GetDefaultSQL();
artset->Open();
artset->Close();
ReleaseMutex(hMutex1);
delete artset;
*/
}
|
|
|
|
|
I've done this very thing -- with ADO
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc.</A>
|
|
|
|
|
Use ADO or ODBC for multithreaded database access. It is possible (as I have done this in the past) with DAO but why waste time with technology that microsoft has quit developing 5 years ago...
John
-- modified at 22:07 Wednesday 31st May, 2006
|
|
|
|