|
Then
sprintf(i,"%d",Test);
is evil (sprintf assumes i is an array of characters).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
and what is addMsg1() and what does it do? It is blowing up in there?
Also, you have a macro that defines to 2048 and max_msg that is set to 2048 and then use both in the code, interchangably. Pick one and use it exclusively otherwise you'll get bit one of these days.
|
|
|
|
|
You arent checking the return form malloc for a valid allocation.
==============================
Nothing to say.
|
|
|
|
|
manju 3 wrote:
pSharedBuffer2 = (char*)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
This doesn't really make a lot of sense since you are just copying the pointer, not the contents!
First, you overwrite the only pointer to a memory block on the heap that you allocated with malloc, so you create a memory leak, right there.
Second, while the pointer is global and will survive the call to WriteToShared, the HANDLE you get from MapViewOfFile() will not: at the latest, it will be invalidated when you leave the function, upon which your handleMappedFile gets destroyed. At that point, pSharedBuffer2 will point to garbage! The address stored therin is invalid, and you may not access this memory. And this, exactly, happens, when next you call memset ! That is your crash.
|
|
|
|
|
Hi,
thanks for your reply,
so how can i avoid the memory crash now.
thanks
sharan
|
|
|
|
|
Easy: don't call memset !
Two possibilities:
1. if you don't use pShardedBuffer2 outside this function, then (a) declare it inside the function as a local parameter and (b) don't malloc, and don't memset (in this case I really don't know what you do that for)
2. if you do use it elsewhere, then you must copy the data, not the pointer! You should be aware though, that changes to that copy won't affect the original data, you can use that copy only to read from! Anyway, if you copy data to your buffer, memset is not required - the value you set your buffer to gets overwritten anyway!
Note that I have no idea how to go about copying the data, as MapViewOfFile returns a HANDLE , not a string, and does not provide any information about the structure or size of that data. Accessing it like a string sounds like calling for trouble, especially if there are 0 bytes within. I'm not familiar with this function or the data structure it delivers, but I suggest you read up on it and how to use it properly.
What you need to do may also depend on how you use the data later, such as in your call to addMsg1() . What does it do?
|
|
|
|
|
Hi
How to change the background color of CIPAddressCtrl .
Thanks
|
|
|
|
|
Hi,
You will probably need to derive a class from CIPAddressCtrl and handle the WM_CTLCOLOR message and return a BRUSH with the color of your choice.
Best Wishes,
-David Delaune
|
|
|
|
|
Hi,
add handler for WM_CTLCOLOR, then check for CTLCOLOR_EDIT and use SetBkColor(RGB());
|
|
|
|
|
how to read a string character by character in C?
|
|
|
|
|
In C, a string is simply an array of characters.
So to read each character you simply index into the array -
char arr[10];
arr[0];
arr[9];
|
|
|
|
|
char str[10]="hghghhj"
int i;
for(i=0;i<10;i++)
printf("%c",str[i]);
|
|
|
|
|
robin700 wrote: how to read a string...
This is very ambiguous. Read a string from where?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Read from what?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
i have a character array such that
char buffer[]= "bob 12345"
and text file has the data
bob 12345
mike abcde
easton 9999
now i want to compare the array with the file line by line and find whether a valid combination of usernames and passwords exist....
please help
|
|
|
|
|
Read the file line by line (fgets ) and compare with buffer (strcmp ).
|
|
|
|
|
Didn't you just ask this question here?
So which part exactly are you needing help with? Iterating a file, text comparison, or something else?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
I have a vector string array filled as follows:
"s1" "v1" "s1" "f1" "g1" "s1" "s1" "o1"
so the result has to be "s1 4". I have to write the string value and the num value in strArtist and iNum. How's that?
void maxCollByArtist(string& strArtist, int& iNum)
{
int max = 0;
string t_str = "";
vector<string> temp;
for(int i = 0; i < m_vMusic.size(); i++)
temp[i] = m_vMusic[i].GetArtist();
for(int i = 0; i < temp.size(); i++)
{
}
}
|
|
|
|
|
if you sort the vector, identical items will be next to each other so it will be simpler to count which of them repeats the most
|
|
|
|
|
I did it. Now how to see which element repeats the most and how many times? (Created a new project)
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
vector<string> v_str;
string t_str;
int iNum = 0;
for(int i = 0; i < 8; i++)
v_str.push_back(str[i]);
for(int i = 1; i < v_str.size(); i++)
for(int j = v_str.size() - 1; j >= i; j--)
if(v_str[j - 1] > v_str[j])
{
t_str = v_str[j - 1];
v_str[j - 1] = v_str[j];
v_str[j] = t_str;
}
for(int i = 0; i < v_str.size(); i++)
cout << v_str[i] << endl;
return 0;
}
|
|
|
|
|
or, just use:
std::sort(v_str.begin(), v_str.end())
so if you have a sorted list of strings:
aaa, aaa, bb, cc, ccc, dd, dd, dd, dd, e, f, g
iterate through the list, check to see when the current item is equal to the last item. if it's the same, keep track of how many times it repeats. if it's different, see if the number of repetitions was greater than the max number of reps you've seen so far.
cur = vec[0]
maxrep = 1
maxstring = cur
currep = 1
for each item in vec, starting at vec[1]
if item==cur
currep++ // we have a repeat, keep track of how many repetitions
else
// the strings are different,
if currep > maxrep // was this repetition count higher than the max
maxrep=currep // it is the new max
maxstring=cur
cur=item // next item is current
currep = 0
|
|
|
|
|
It's not working. Gives me "zxc" and 1, but the correct is "qwe" and 3.
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
vector<string> v_str;
string t_str, maxstring;
int maxrep = 1;
int currep = 1;
for(int i = 0; i < 8; i++)
v_str.push_back(str[i]);
for(int i = 1; i < v_str.size(); i++)
for(int j = v_str.size() - 1; j >= i; j--)
if(v_str[j - 1] > v_str[j])
{
t_str = v_str[j - 1];
v_str[j - 1] = v_str[j];
v_str[j] = t_str;
}
t_str = v_str[0];
maxstring = t_str;
for(int i = 1; i < v_str.size(); i++)
{
if(v_str[i] == t_str)
currep++;
else if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
}
for(int i = 0; i < v_str.size(); i++)
cout << v_str[i] << endl;
cout << endl;
cout << t_str << endl;
cout << maxrep << endl;
return 0;
}
|
|
|
|
|
else if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
should be:
else
{
if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
}
also, you will have to add a check after the loop, to see if the last repetition has not ended.
|
|
|
|
|
" to see if the last repetition has not ended."
what does that mean?
|
|
|
|