Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Probably not the answer you were looking for, but perhaps the best solution here would be to properly initialise your strings and pointers. If your memory contains garbage, why not do the decent thing and set</p> <pre><code>yourString[0] = '\0'; </code></pre> <p>If it really is just an arbitrary bit of buffer, you might be better off using something like <a href="http://www.cplusplus.com/reference/clibrary/cstring/memcmp/" rel="nofollow noreferrer">memcmp</a> and slide the memory buffer's pointer along <code>N</code> characters (where <code>N</code> is the number of characters you're interested in minus the length of the string you're comparing). That might not be the most efficient implementation, but should be a fairly robust approach I should think.</p> <p><strong>[Edit]</strong> Your question intrigued me enough to do a little experimentation. Given that you seem to be looking for more C-styled answer, here's a little snippet of code I came up with to elaborate on my memcmp suggestion:</p> <pre><code>// SearchingMemoryForStrings.cpp : Defines the entry point for a win32 consol application // Purpose : Demonstrates a way to search a section of memory for a particular string // #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define VALUE_NOT_FOUND (-1) int FindStringInBuffer( const char* pMemBuffer, const size_t&amp; bufferSizeInBytes, const char* pStrToFind ) { int stringFound = VALUE_NOT_FOUND; // Return value which will be &gt;= 0 if we find the string we're after const char* pMemToMatch = NULL; // An offset pointer to part of 'pMemBuffer' which we'll feed to memcmp to find 'pStrToFind' // Set up some constants we'll use while searching size_t lenOfStrToFind = strlen( pStrToFind ); size_t lastSearchablePosition = bufferSizeInBytes - lenOfStrToFind; // Search the memory buffer, shifting one character at a time for 'pStrToFind' for( size_t i = 0; i &lt;= lastSearchablePosition; i++ ) { pMemToMatch = &amp;pMemBuffer[i]; if( memcmp(pMemToMatch, pStrToFind, lenOfStrToFind) == 0 ) { // We found the string we're looking for stringFound = i; break; } } return stringFound; } void ReportResult( int returnVal, const char* stringToFind ) { if( returnVal == VALUE_NOT_FOUND ) { // Fail! printf("Error, failed to find '%s' - search function returned %d\n", stringToFind, returnVal ); } else { // Win! printf("Success, found '%s' at index %d\n", stringToFind, returnVal ); } } void FindAndReport( const char* pMemBuffer, const size_t&amp; bufferSizeInBytes, const char* pStrToFind ) { int result = FindStringInBuffer( pMemBuffer, bufferSizeInBytes, pStrToFind ); ReportResult( result, pStrToFind ); } int main( int argc, char* argv[] ) { const int SIZE_OF_BUFFER = 1024; // Some aribitrary buffer size char some_memory[SIZE_OF_BUFFER]; // The buffer of randomly assigned memory to look for our string const char* stringToFind = "This test should pass"; const char* stringYouWontFind = "This test should fail"; FindAndReport( some_memory, SIZE_OF_BUFFER, stringYouWontFind ); // Should fail gracefully // Set the end of the buffer to the string we're looking for memcpy( &amp;some_memory[SIZE_OF_BUFFER-strlen(stringToFind)], stringToFind, strlen(stringToFind) ); FindAndReport( some_memory, SIZE_OF_BUFFER, stringToFind ); // Should succeed this time and report an index of 1003 // Try adding at some arbitrary position memcpy( &amp;some_memory[100], stringToFind, strlen(stringToFind) ); FindAndReport( some_memory, SIZE_OF_BUFFER, stringToFind ); // Should still succeed but report the offset as 100 FindAndReport( some_memory, SIZE_OF_BUFFER, stringYouWontFind ); // Should still fail return 0; } </code></pre> <p>That snippet compiled under Visual Studio 2008 as a Win32 console app. Gives me the following:</p> <pre><code>Error, failed to find 'This test should fail' - search function returned -1 Success, found 'This test should pass' at index 1003 Success, found 'This test should pass' at index 100 Error, failed to find 'This test should fail' - search function returned -1 </code></pre> <p>The <code>FindStringInBuffer</code> function is the bit you'll want and if you need to cope with wide characters, you'll need to do some conversion, but this should at least give you some ideas that you can proceed with. If you do come up with a wchar version, I'd be interested to see what the solution looks like (I've not dealt with them myself).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload