1

I have a function that goes through my array of strings to find out how many times that string occurs with in an array. If found, the string will be set to NULL and a counter keeps track of how many times the string is found. I then call another function within the loop to allocate memory for my frequency array so that I can store count. It seems to work fine, but when I go and create any other variable within main my program crashes. Here are my two functions:

int search(char **table, int **frequency, int wordSize)
{
//  Local Declaration
int i, j, k;
int count = 1;
int strCount = 0;
char target[25];

// Statement
for(i = 0, k = 0; i < wordSize; i++)
{
    if(table[i] != NULL)
    {
        strcpy(target, table[i]);
        for(j = i + 1; j < wordSize; j++)
        {
            if(table[j] != NULL &&
               strcmp(target, table[j]) == 0 &&
               target != table[i])
            {
                count++;
                free(table[j]);
                table[j] = NULL;
            }
        }
    strCount += makeFreq(frequency, k, count);
    k++;
    }
    count = 1;
}

return strCount;
}// search


int makeFreq(int **frequency, int k, int count)
{
//  Local Declaration
int strCount = 0;

//  Statement
frequency[k]=(int*)malloc(sizeof(int));
frequency[k][0] = count;
strCount += 1;

return strCount;
}// makeFreq

Can someone explain to my why my program is crashing?

Here I allocated 1000 pointers for my table.

char** getPoint(void)
{
//  Local Declaration
char **table;

//  Statement
table = (char**)calloc(MAX_SIZE + 1, sizeof(char));
if(table == NULL)
{
    MEM_ERROR, exit(100);
}

return table;
}// getPoint

Than I read I allocate memory for the strings in my file and store it into the array of strings.

int scanFile(char **table, FILE *fpFile)
{
//  Local Declaration
int count = 0;
char temp[500];
char **ptr = table;

//  Statement

//  scan file, allocate, and copy string to array.
while(fscanf(fpFile, "%s", temp) != EOF)
{
    *(ptr + count) =(char*)calloc(strlen(temp)+1, sizeof(char));
    strcpy(*(ptr + count), temp);
    count++;
}

return count;
}// scanFile

Here is how I allocated the array of pointers for my frequency array.

void aloFreqAry(int **frequency, int wordSize)
{
//  Local Declaration

//  Statement
frequency =(int**)calloc(wordSize + 1, sizeof(int));
if(frequency == NULL)
{
    MEM_ERROR, exit(103);
}

return;
}// aloFreqAry
Nathan
  • 483
  • 4
  • 7
  • 19
  • Can you show how you're allocating the memory for table and frequency in your main() function ? It might have something to do with that. – Tuxdude Feb 27 '13 at 21:31
  • @Tuxdude - okay give me a second to upload it. – Nathan Feb 27 '13 at 21:34
  • And, always compile with the highest warning level. – Daniel Fischer Feb 27 '13 at 21:35
  • @Tuxdude - okay I uploaded the functions. – Nathan Feb 27 '13 at 21:40
  • @DanielFischer - I'm using codeblocks right now and I'm not sure how to turn on all the warnings, because when I use visual studio to check for memory leaks, I know it shows way more warnings that codeblocks didn't catch. – Nathan Feb 27 '13 at 21:42
  • `frequency[k]=(int*)malloc` - [AAAAARGH!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) –  Feb 27 '13 at 21:47
  • @H2CO3 If you don't want a heart attack before you're 22, you really need to develop some indolence. – Daniel Fischer Feb 27 '13 at 22:03
  • @DanielFischer Indeed. (Well, I have more serious problems than malloc, that really hurt, this is just annoying.) –  Feb 27 '13 at 22:05
  • @Nathan Don't know codeblocks, but there ought to be something like "Project settings" where you can put compiler flags. `-Wall -Wextra` is a decent warning level for gcc and clang. – Daniel Fischer Feb 27 '13 at 22:05
  • As a small improvement in readability, you can replace the `calloc()`/`strcpy()` combo in `scanFile()` with `table[count] = strdup(temp);` – Michael Burr Feb 27 '13 at 22:31

2 Answers2

3

Apart from the problem of the sizes in the allocations (should be sizeof(char*) in the allocation of table, and sizeof(int*) in the allocation of frequency),

void aloFreqAry(int **frequency, int wordSize)
{
//  Local Declaration

//  Statement
frequency =(int**)calloc(wordSize + 1, sizeof(int));
if(frequency == NULL)
{
    MEM_ERROR, exit(103);
}

return;
}// aloFreqAry

doesn't allocate anything to the frequency in the caller. It just allocates memory to the local copy of that pointer, and loses the handle to that when the function returns.

Instead of taking an int** as argument, the function should return one,

frequency = calloc(wordSize + 1, sizeof(int*)); // size of a _pointer_ to int
if(frequency == NULL)
{
    MEM_ERROR, exit(103);
}

return frequency;

that you assign in the caller.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • Thanks for pointing that out. It makes perfect sense now. I really appreciate your time and effort for helping me solve this issue. – Nathan Feb 27 '13 at 22:38
1

This statement looks suspect (where you say "Here I allocated 1000 pointers for my table"):

table = (char**)calloc(MAX_SIZE + 1, sizeof(char));

That doesn't look like an allocation of pointers, but an allocation of a char buffer.

Perhaps you mean:

table = (char**)calloc(MAX_SIZE + 1, sizeof(char*));
Michael Burr
  • 333,147
  • 50
  • 533
  • 760