#### Archived

This topic is now archived and is closed to further replies.

# Array of Strings problem (I think)

## Recommended Posts

PhilHalf    148
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[ i ] = 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)

CODESEG

public  strcmp
strcmp  proc

.FPO    ( 0, 2, 0, 0, 0, 0 )

mov     edx,[esp + 4]   ; edx = src
mov     ecx,[esp + 8]   ; ecx = dst

test    edx,3
jnz     short dopartial

align   4
dodwords:
mov     eax,[edx]

cmp     al,[ecx]       // arrow points to this line

jne     short donene

If I don't do anything with the szStrings variable, other than create the arrays, I have no problems. The error only 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 [edited by - PhilHalf on March 16, 2003 3:51:08 PM]

##### Share on other sites
Zipster    2365
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!

##### Share on other sites
Burning_Ice    122
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[ i ] = new char[iStringLength+1]
and make sure the last character of every string is '\0'

Edit: doh, Zipster beat me to it by 30 seconds

My Homepage

[edited by - Burning_Ice on March 16, 2003 4:05:41 PM]

##### Share on other sites
PhilHalf    148
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]

##### Share on other sites
JohnBolton    1372
Your first string wouldn''t happen to start with "inde" would it? That''s what 0x65646e69 is (little-endian).

##### Share on other sites
cmptrgear    306
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

##### Share on other sites
PhilHalf    148
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[ i ]?

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.

Jambolo: Yes, the first string is index.htm. I'm trying to make a program to website logs for hits on certain pages and so index.htm is the main one. I still have the problem searching for other strings however.

Thanks again for any suggestions,

PhilHalf

[edited by - PhilHalf on March 19, 2003 4:07:27 PM]

• 9
• 11
• 9
• 11
• 9