0
char **makeString ( const int numOfArrays, const int sizeOfEachArray  ) {
    /* Function to create an array of strings: ( Array of pointers to strings ) */

    /* Local variables */
    int i, j;
    //int numOfArrays = 6;
    /*char stringsToCopy[6][12] = {"Hello", "Good-Bye!", "Greetings",
                        "Pointers", "Arrays", "Programming"}; */
    char **stringArray = NULL;

    if ( ( (numOfArrays) && (sizeOfEachArray) ) == (int)NULL ) {
        /* Free memory */
        for (j = 0; j < numOfArrays; j++) {
            free(stringArray[j]);
        }
        free(stringArray);
    }
    /* Allocate room for numOfArrays */
    stringArray = malloc(numOfArrays * sizeof *stringArray);

    /* If allocation succeeded */
    if (stringArray != NULL) {
        /* Loop through all columns */
        for (i = 0; i < numOfArrays; i++) {
            /* Allocate */
            stringArray[i] = malloc( (sizeOfEachArray) + 1);
        }
    }else {
        /* Allocation failed */
        return NULL;
    }





    /* End program */
    return stringArray;
}
int main(void)
{
    int i;
    int j;
    char **stringArray2 = NULL;
    //char *point;
    /*while ( ( point = getDirectoryFiles () ) != NULL ) {
        printf ( "in main \"%s\"\n", point);
    }*/
    stringArray2 = makeString ( 5, 50  );

    strcpy ( stringArray2 [0], "This is a string 0" );
    strcpy ( stringArray2 [1], "This is a string 1" );
    strcpy ( stringArray2 [2], "This is a string 2" );
    strcpy ( stringArray2 [3], "This is a string 3" );
    strcpy ( stringArray2 [4], "This is a string 4" );

    /* Print data */
    for (i = 0; i < 5; i++) {

        printf("%s\n", stringArray2[i]);
    }

    makeString ( (int)NULL, (int)NULL );
  return(0);
}

I have this code it works but I want to know if I am having a memory leak...
I can't free stringArray2 from main and if I free it in the makeString function I can't return it (it would have been freed)... Do I have a memory leak?, How can I prevent this? Thanks in advance... (the problem is I can't find a way to free stringArray in makeString and return the information it has...)

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by ShortYute
1

The macro NULL is intended for pointers, not integers, Instead of casting NULL to int why not just pass 0? It would make more sense. Example: line 11 should be like this: if ( numOfArrays == 0 && sizeOfEachArray == 0) Remember: integers are not pointers (normally) so using NULL for them is an inappropriate use of that macro.

>>I can't free stringArray2 from main
why not?

>> makeString ( (int)NULL, (int)NULL );
The string can not be free'ed that way. Make another function to free the array void FreeString(char **string, const int numOfArrays, const int sizeOfEachArray )

Edited by Ancient Dragon: n/a

0

/* Ohh thanks about the NULL should I use the function to free the memory or should i free it in main? (see below) */
I tried that in main (codeblocks) and nothing happened (debugging)... How do I know it is deallocated? So let me get this straight:
0. I'm in main i call x = boo (); /* x was defined as char **x; */
1. I go into char **boo ();
2. I make char **p;
3. I do p = malloc(somerandomnumber* sizeof *p);
4. Then return p;
5. x should have the pointer of p now
6. then I free (x); and i'm fine? and will not have a memory leak?

Edited by ShortYute: Just saw previous post

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.