|
Please don't post the complete code, it is unnecessary. Also use the code and pre tags to format your code (it is impossible to read).
For your question, I seriously doubt that the borland compiler supports the MFC (never worked with it).
|
|
|
|
|
What I want to do is probably impossible, but regardless, I would still like your thought and comments.
Lets say we have a c style string. We have a pointer to the begining and the end is found by increasing the pointer until we reach a null char. strings 101. But now lets say we have a 'fragmented' string (ie there are two halves to the same string, each half having a pointer. We know both pointers we also know the exact length of each part.
First. Is it possible to return this string from a function, without allocating, copying and merging?
Second. Is it possible to write a string to fragmented memory like this? Again without chopping.
Idealy what I want is to line the pointers up, taking the block size into account, and return a single pointer.
|
|
|
|
|
No because a "string" in C isn't a built-in type, it's a convention that says an array of char s, marked with 0 at the end, is called a "string". All CRT string functions expect to operate on arrays such as that.
If you split up the array, then by definition it's no longer a "string" so you'll need to write your own code to handle that new kind of string.
|
|
|
|
|
It's looks like I am going to have to do this the long way. The reason I ask is that I have strings of up to some 10mb in length, yet these are stored in 4k chunks. I cannot be sure if the chunks would be sequencial. It seems a shame to have to allocate 10mb of memory just to return a single string which is already in memory.
I think the only way I could increase performance is to check if the memory is sequencial, if so return the first pointer, if not reallocate the whole thing.
|
|
|
|
|
My program has many thread, every pair of threads has its own memory block.
When one thread want to change value of variable, is it possible to just lock its couple instead of locking every other threads that will never touch that block of memory?
|
|
|
|
|
I'm not sure I understand very clearly. Between two threads there is 1 block of shared memory? How are you locking the memory?
I would use a mutex and create a unique name for every block of shared memory. That way there would only ever be two threads trying to lock the same mutex.
|
|
|
|
|
When I use mutex lock, it will stop every thread from running instead of stop only thread that trying to access that variable, right?
if yes, could you please give me a short example about mutex locking specify thread
|
|
|
|
|
No. When you create a mutex you can give it a unique name. So you need a different name for each pair of threads. Because only 2 threads will share the same mutex name, they will only ever be able to lock each other out. So, for example, you have 10 threads with 5 blocks of memory, you should create 5 mutex names.
|
|
|
|
|
A customer of mine has sent me a user.dmp file generated when a debug version of an application of mine crashes. The application was compiled with VS2005. Now, I thought I could open and then run .dmp files in Visual Studio, but when I run this file I get the following error:
Debugging older format crashdumps is not supported
Anyone know how I can extract some useful information from this .dmp file? The call stack would be useful and it would at least give me some idea where the problem occurs!
Thanks in advance.
|
|
|
|
|
Downloaded windbg and this allows me to open the file...
|
|
|
|
|
lmao, I was just scratching my brains trying to remember the name of that program so I could tell you to try it.
|
|
|
|
|
I would like to loop through a vector<string> type and remove the contents of strSearch from
all the lines within Sentences.
CSentences.h
...
std::vector <std::string> Sentences; //list of sentences.
...
The following function returns the number of occurrences of strSearch within sentence.
CSentences.cpp
unsigned int CSentenceList::searchInSentence(const string sentence, char* strSearch)
{
char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
const char* l_strSentence = sentence.c_str();
memcpy(buffer, l_strSentence, MAX_SENTENCE_LENGTH);
char* strToken = 0;
unsigned long iWordCount = 0;
strToken = strstr(buffer, strSearch);
while (strToken !=NULL)
{
if (strlen(strToken) > 0)
{ // For each line within Sentences
// remove strSearch
iCount++;
strToken += strlen(strSearch);
}
strToken = strstr(strToken, strSearch);
}
return iCount;
}
Jon
|
|
|
|
|
The following function removes all occurances of strSearch in sentence:
void StringRemove(std::string& sentence, const char * const strSearch)
{
size_t len = strlen(strSearch);
std::string::size_type i = sentence.find(strSearch);
while(i != std::string::npos)
{
sentence.erase(i, len);
i = sentence.find(strSearch, i);
}
}
If, as the title of your post suggests you actually want to replace a substring use the following:
void StringReplace(std::string& sentence,
const char * const strSearch,
const char* const strNew)
{
size_t len = strlen(strSearch);
size_t newLen = strlen(strNew);
std::string::size_type i = sentence.find(strSearch);
while(i != std::string::npos)
{
sentence.replace(i, len, strNew, newLen);
i = sentence.find(strSearch, i + newLen);
}
}
To replace all occurances in the vector use:
std::vector<std::string>::iterator i;
for(i = sentences.begin(); i != sentences.end();++i)
StringReplace(*i, "foo", "bar");
BTW a more succinct version of your count occurances in string function is
unsigned int StringCount(const std::string& sentence,
const char* const strSearch)
{
unsigned int count = 0;
size_t len = strlen(strSearch);
std::string::size_type i = sentence.find(strSearch);
while(i != std::string::npos)
{
i = sentence.find(strSearch, i + len);
++count;
}
return count;
}
Graham
My signature is not black, just a very, very dark blue
|
|
|
|
|
This function REMOVES sub-string of strText starting at iStart and resuming at iStart + iNoOfChars:
string CSentenceList::subString(char* strText, int iStart, int iNoOfChars)
{
int iLength = strlen(strText);
char l_Substring[MAX_SENTENCE_LENGTH];
// copy character by character till iStart
for (int iFirstCount = 0; iFirstCount < iStart; iFirstCount++)
{l_Substring[iFirstCount] = strText[iFirstCount];}
// copy from iContinue till the end of the text
int iContinue = iStart + iNoOfChars;
for (int iSecondCount = iContinue; iSecondCount < iLength; iSecondCount++, iFirstCount++)
{l_Substring[iFirstCount] = strText[iSecondCount];}
return (string) l_Substring;
}
However, the string returned is terminated by a series of unreadable characters. For example, substring ("joe123joe",3,3) should return "joejoe", but it returns:
joejoe╠╠╠╠╠╠╠╠... // up to the value of MAX_SENTENCE_LENGTH (1000)
I have tried using dynamic arrays of chars but I had trouble converting them to std::string type.
Jon
|
|
|
|
|
Add a NULL terminator to the end of the string. Or better still, call memset() for l_Substring and zero the memory.
memset(l_Substring,0,MAX_SENTENCE_LENGTH);
|
|
|
|
|
I think the above line zero's the memory for l_Substring. I would like to remove what has not already been 'filled' within the function.
How do I add a NULL terminator btw?
Jon
|
|
|
|
|
jon_80 wrote: I think the above line zero's the memory for l_Substring. I would like to remove what has not already been 'filled' within the function.
When you memset with '0', you are filling the array with NULL terminators, so after you add your characters one by one there will be a NULL at the end.
jon_80 wrote: How do I add a NULL terminator btw?
After you have added the last character, increase the pointer and make it = '0' instead of adding a character.
|
|
|
|
|
string CSentenceList::removeSubstring(char* strText, int iStart, int iNoOfChars)
The NULL is not started at the end of the string though...
{
int iLength = strlen(strText);
char l_Substring[MAX_SENTENCE_LENGTH];
// copy character by character till iStart
for (int iFirstCount = 0; iFirstCount < iStart; iFirstCount++)
{l_Substring[iFirstCount] = strText[iFirstCount];}
// copy from iContinue till the end of the text
int iContinue = iStart + iNoOfChars;
for (int iSecondCount = iContinue; iSecondCount < iLength; iSecondCount++, iFirstCount++)
{l_Substring[iFirstCount] = strText[iSecondCount];}
memset(l_Substring,0,MAX_SENTENCE_LENGTH); //resets l_Substring to ""
return (string) l_Substring;
}
Jon
|
|
|
|
|
I am finding references to control handles in VC++, but haven't yet found a definition. What is the handle? When I create an edit box, it returns a label of IDC_EDIT1, IDC_EDIT2, etc. Is this the handle? Is the handle the variable m_edit1 that I assign to he control?
|
|
|
|
|
The handle for a control is commonly called the HWND <-- 'H' means Handle
|
|
|
|
|
A handle (such as HWND or HANDLE) is simply typedef'ed to a long. It is a unique number assigned by the operating system to every window. When you create a window through the Win32 API, a handle is returned (HWND). Whenever you want to do something to that window you pass the HWND value to whatever Win32 function you're calling. Since MFC is basically a huge wrapper around the whole Win32 API, handles work the same way in MFC.
Your IDC_EDIT1, IDC_EDIT2, etc constants refer to the resource ID of a particular control. It is not the same as the handle. The resource ID is simply a number that the compiler uses when making resource files.
The control MFC classes (such as CEdit, CComboBox, etc.) all have a method called GetSafeHwnd() and a member called m_hWnd. Whenever you need a handle (HWND) of your control, just call GetSafeHwnd(). Or you can just grab the value of m_hWnd (it is public).
-Greg Dolley
|
|
|
|
|
We have many handle in VC for example HWND window/HMENU menu /HDC Context_Device and... when you declare a variable for editbox this handle in Hierarchy Chart is CObject->CCmdTarget->CWnd->CEdit
|
|
|
|
|
Hi all,
I have the following code:
// CODE STARTS HERE //
int i,j;
std::fstream f_load;
char appoggio[20]; // carica il nan oppure il valore numerico
CString percorso;
CFileDialog fd(TRUE, NULL, percorso, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY);
fd.m_pOFN->lpstrTitle = "Carica file di osservazioni";
if (fd.DoModal() == IDOK)
{
percorso = fd.GetPathName();
UpdateData(false);
f_load.open(percorso,std::ios::in);
int contatore = 0;
do{
f_load>>appoggio;
contatore++;
}
while(f_load!=EOF);
}
// CODE ENDS HERE //
but there's something wrong in the while condition.
Which is the right while condition to scan the file until its end ?
Thanx in advance,
Desmo16.
|
|
|
|
|
In your loop, you are checking if the EOF has been reached at the end of the loop, but the pointer has already been increased causing your code not to go through the loop for the final time. You should test before entering the loop, ie
while (f_load != EOF)<br />
{<br />
}
|
|
|
|
|
I don't think it's that the matter,
because the compiler tells me it cannot convert
an int (the EOF) into a std::fstream (the f_load)
|
|
|
|