Note that there are some explanatory texts on larger screens.

plurals
  1. POSIGBART in C code with malloc/free
    primarykey
    data
    text
    <p>I wrote this small program to find all occurrences of a substring in a larger string, or a <em>needle</em> in a <em>haystack</em>. When I run the program locally, it seems to work just fine. However, when I submit it to an online contest for judging, it gives a SIGBART error. I assumed it was because of poor memory-management, so I deleted the <code>free()</code> function calls, but then I got a <em>Time Limit Exceeded</em> error (but the SIGBART error disappeared). Does removing the <code>free()</code> calls slow the program? And are there any leaks in my program?</p> <p>Here the contest I was talking about: <a href="http://www.spoj.pl/problems/NHAY/" rel="nofollow">Needle in the Haystack</a></p> <p>Here's the code:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define RAW_INPUT_SIZE 10000 #define BOOL unsigned int #define NO 0 #define YES 1 int main (int argc, char **argv) { int needleLength; char *rawNeedle = (char *)malloc(RAW_INPUT_SIZE); char *rawHaystack = (char *)malloc(RAW_INPUT_SIZE); char *needle; // to be allocated later char *haystack; // to be allocated later, but not deallocated while (scanf("%i\n%s\n%s", &amp;needleLength, rawNeedle, rawHaystack) != EOF) { needle = (char *)malloc(needleLength); strncpy(needle, rawNeedle, needleLength); haystack = strchr(rawHaystack, needle[0]); int i = haystack - rawHaystack; BOOL matchesFound = NO; if (i + needleLength - 1 &lt; strlen(rawHaystack)) { while (haystack != NULL) { if (i + needleLength - 1 &lt; strlen(rawHaystack)) { char *substr = (char *)malloc(needleLength); strncpy(substr, haystack, needleLength); if (strcmp(needle, substr) == 0) { printf("%i\n", i); matchesFound = YES; } free(substr); substr = NULL; } haystack = strchr(haystack+1, needle[0]); i = haystack - rawHaystack; } } if (matchesFound == NO) printf("\n"); free(needle); needle = NULL; } free(rawNeedle); free(rawHaystack); rawNeedle = NULL; rawHaystack = NULL; return 0; } </code></pre> <hr> <p>Transcription of input and output specification from the question</p> <blockquote> <h3>Input</h3> <p>The input consists of a number of test cases. Each test case is composed of three lines, containing:</p> <ul> <li>the length of the needle,</li> <li>the needle itself,</li> <li>the haystack.</li> </ul> <p>The length of the needle is only limited by the memory available to your program, so do not make any assumptions - instead, read the length and allocate memory as needed. The haystack is not limited in size, which implies that your program should not read the whole haystack at once. The KMP algorithm is stream-based, i.e. it processes the haystack character by character, so this is not a problem.</p> <p>The test cases come one after another, each occupying three lines, with no additional space or line breaks in between.</p> <h3>Output</h3> <p>For each test case your program should output all positions of the needle's occurences within the haystack. If a match is found, the output should contain the position of the first character of the match. Characters in the haystack are numbered starting with zero.</p> <p>For a given test case, the positions output should be sorted in ascending order, and each of these should be printed in a separate line. For two different test cases, the positions should be separated by an empty line.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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