|
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
|
|
|
|
|
Are the client and the server on the same machine?
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
CString LString;
CString Win = GetWinampPath();
Win.Replace("Winamp.exe","winamp.ini");
AfxMessageBox(Win);
CStdioFile file;
CString Filepath = Win;
const int sz =100;
char buf[sz];
if (file.Open(Filepath,CFile::modeReadWrite|CFile::modeNoTruncate|CFile::modeCreate))
{ // what i have to add here?
file.ReadString(buf,100);
LString = (LPTSTR)buf;
if ( LString == "minimized=0")
{
file.Remove(LString);
file.WriteString("minimized=1");
}
}
file.Close();
|
|
|
|
|
Hi
I am not able to answer as per your question.
From your code it seems that you want to change the settings of INI file.
Then why not to try the functions ?
GetPrivateProfileInt
SetPrivateProfileInt
GetPrivateProfileString
SetPrivateProfileString etc...
Regards
Anil
|
|
|
|
|
oh ?
I got the winamp.ini file:
...
...
...
...
minimized=0 //the part i want to remove
...
...
Files.ini have different act than .txt etc?
|
|
|
|
|
In that case the above code seams a lot of things missing
What I think of is that in stead of using CStdioFile you use CFile.
In CFile you can read the whole file into a CString.
Then use CString remove command to remove the part of the string you want to remove.
Then write the string back to the file.
Other method is use a temporary file and read one line and write to temp file, Except the line you want to remove.
Then copy the temp file to .ini file.
Regards
Anil
|
|
|
|
|
thx 
|
|
|
|
|
Immunity18 wrote: file.ReadString(buf,100);
Why use char here? A CString object (e.g., LString ) is a much better choice.
If you are changing the contents of a .ini file, why not use the appropriate functions?
GetPrivateProfileString()
WritePrivateProfileString()
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|