Array of Strings problem (I think)

Started by
5 comments, last by PhilHalf 21 years, 1 month ago
I am currently making a Windows dialog based program but have come across yet another problem with it that I can't figure out. I have a list box that has strings added to it. No problems so far. At one point, I want to pull the strings out of the list box and compare them to a token from a text file. The way I have done it is to make a dynamic array of strings char **szStrings I read the number of strings in the listbox with the LB_GETCOUNT message and dynamically make the first section of the array szStrings = new char*[iNumberOfStrings] From there, I loop through the array and the listbox (by index) and get the length of each string with LB_GETTEXTLEN and make each string in the array big enough to hold the value szStrings = new char[iStringLength] This all seems to work fine, but if I try to use the variable for the comparison as I'm trying or even to output to a message box or anything, it comes up with an error (in Debug mode) Access violation reading location 0x65646e69 and also shows me the assembly for the strcmp function (I think) <!–STARTSCRIPT–><BR><DIV CLASS=source><pre> CODESEG <font color="blue">public</font> strcmp strcmp proc .FPO ( 0, 2, 0, 0, 0, 0 ) mov edx,[<font color="purple">esp + 4</font>] ; edx = src mov ecx,[<font color="purple">esp + 8</font>] ; ecx = dst test edx,3 jnz <font color="blue">short</font> dopartial align 4 dodwords: mov eax,[<font color="purple">edx</font>] cmp al,[<font color="purple">ecx</font>] <font color="gray">// arrow points to this line </font> jne <font color="blue">short</font> donene </pre></DIV><!–ENDSCRIPT–> If I don't do anything with the szStrings variable, other than create the arrays, I have no problems. The error &#111;nly comes up when I try to access the variable. Anyone know what might be causing this, or if there's anything that I could be doing better in the code? Thanks in advance for any help or advice, PhilHalf <SPAN CLASS=editedby>[edited by - PhilHalf &#111;n March 16, 2003 3:51:08 PM]</SPAN>
Advertisement
The size LB_GETTEXTLEN returns doesn''t include the NULL terminator, so when you allocate the string, you have to add one for the NULL. That might be what the problem is, strcmp is trying to read outside the bounds of your string because it doesn''t know where to stop!
LB_GETTEXTLEN returns string length excluding the terminating null character which is needed by strcmp, so i believe it should work if you use szStrings = new char[iStringLength+1]<br>and make sure the last character of every string is '\0' <br><br>Edit: doh, Zipster beat me to it by 30 seconds <img src="smile.gif" width=15 height=15 align=middle><br><br><hr><A href=" http://www.runicsoft.com">My Homepage</A><br><br><SPAN CLASS=editedby>[edited by - Burning_Ice &#111;n March 16, 2003 4:05:41 PM]</SPAN>
Having tried your suggestions, I'm still getting the same problem and the same line in the assembly file.
Here is the code that seems to be causing the problem

      iNumberOfStrings = SendMessage(GetDlgItem(GetParent(hwnd), IDC_LISTSTRINGS), LB_GETCOUNT, 0, 0);szStrings = new char*[iNumberOfStrings];if (iHits)    delete [] iHits;iHits = new int[iNumberOfStrings];for (i = 0; i < iNumberOfStrings; ++i){    stringLength = SendMessage(GetDlgItem(GetParent(hwnd), IDC_LISTSTRINGS), LB_GETTEXTLEN, (WPARAM)i, 0);    szStrings[i] = new char[stringLength + 1];    szStrings[i][stringLength] = '\0';    SendMessage(GetDlgItem(GetParent(hwnd), IDC_LISTSTRINGS), LB_GETTEXT, (WPARAM)i, (LPARAM)&szStrings[i]);    iHits[i] = 0;}  


And the code that contains the strcmp function:


  hFile = CreateFile(szFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);dwSize = GetFileSize(hFile, NULL);szBuffer = new TCHAR[dwSize];	ReadFile(hFile, (LPVOID)szBuffer, dwSize, &dwRead, NULL);szToken = strtok(szBuffer, szDelimeters);while (szToken != NULL){    for (int j = 0; j < iNumberOfStrings; ++j)    {        if (strcmp(szToken, szStrings[j]) == 0)	{	    iHits[j]++;	}    }    szToken = strtok(NULL, szDelimeters);}  


If anyone has any more suggestions I'd be grateful as this is starting to get frustrating now.

Thanks in advance for any help or advice,

PhilHalf

[edited by - PhilHalf on March 19, 2003 3:46:57 PM]
Your first string wouldn''t happen to start with "inde" would it? That''s what 0x65646e69 is (little-endian).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Two things that I can think of off the top of my head. Where do you actually copy the string over into the string array. In the code you show it just saves space for it but doesnt actually copy it. The second thing, is that you shouldnt use strtok because it isnt a very goodfunction. A quick look on google shows that it overwrites the first argument you pass it, maybe that is important maybe it isnt. Anyhow that is my two cents, other than that I dont see anything wrong with the code.

"I may not agree with what you say but I will defend to the death your right to say it."
--Voltaire
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
cmptrgear: I was under the impression that

    SendMessage(GetDlgItem(GetParent(hwnd), IDC_LISTSTRINGS), LB_GETTEXT, (WPARAM)i, (LPARAM)&szStrings[i]);    

would copy the text in index 1 of the listbox into szStrings?<br><br>As for the strtok thing, I don't use szBuffer after that point anyway, so I don't think that it should cause a problem.<br><br>Jambolo: Yes, the first string is index.htm. I'm trying to make a program to website logs for hits &#111;n certain pages and so index.htm is the main &#111;ne. I still have the problem searching for other strings however.<br><br>Thanks again for any suggestions,<br><br>PhilHalf <br><br><SPAN CLASS=editedby>[edited by - PhilHalf &#111;n March 19, 2003 4:07:27 PM]</SPAN>

This topic is closed to new replies.

Advertisement