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...)

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 )

/* 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?

>>then I free (x); and i'm fine? and will not have a memory leak?
Yes, that's the way to do it.

Thanks I understand now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.