Thread: Dynamic Memory Allocation Question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    27

    Dynamic Memory Allocation Question

    I have to set up an array of strings dynamically that I will read words in from a dictionary file (dictionary.txt). I know that the maximum length of letters for any given word is 19 letters. Also, maxWord will be given the value of the total number of words in the dictionary.txt file that I will be reading into memory.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char **dictionary;
    const MAX_LENGTH = 19;
    int maxWord;
    int i;
    
    FILE *ifp = fopen("dictionary.txt", "r"); // open dictionary file
    
    fscanf (ifp, "%d", &maxWord);  // read number of words in dictionary
    
    dictionary = malloc(maxWord * sizeof(char*));  // allocate memory
    
    for (i = 0; i < maxWord; i++)
    
        dictionary[i] = malloc((MAX_LENGTH) * sizeof(char));
    
    for (i = 0; i < maxWord; i++)  // read dictionary words into dictionary array
    
        {
    
        fscanf (ifp, "%s", dictionary[i]); 
        printf ("%s\n", dictionary[i]);  // temp. confirms dictionary array working
    
        }
    
    fclose(ifp); // close dictionary.txt
    
    }
    My program compiles, and I confirmed with the printf statements that the array is working, but I get a program error when I get to a function that I send the dictionary array to. I send it in the following format.

    Code:
    myFunction (dictionary, otherVar, otherVar2);
    
    
    
    void myFunction (char **dict[][MAX_LENGTH], int var, int var2) {
    
    blah blah blah
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Puhlease! That code as shown would not compile. It has executable statements outside functions. It is better to provide small samples of actual code that illustrates your problem. The fact most of your first sample of code actually belongs in a function means your code does not compile. Unnecessarily cutting out such details does waste people's time finding irrelevant concerns.

    As to the particular problem with myFunction(), the first argument is a 2D array of pointers to pointers. The variable dictionary is of type pointer to pointer. A 2D array of X is not an X. The compiler reports that as a mismatch of types, because it cannot convert a "pointer to pointer to char" into what the function expects.

    You either need to change the type of argument that myFunction() expects, or change the type of argument you supply so it is what the function expects. Do not use a typecast to force the issue, as then your code will compile but (probably) fail to run.
    Last edited by grumpy; 10-12-2012 at 03:58 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    I posted what a thought was important for brevity's sake. Here is the full code so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    const MAX_LENGTH = 19;
    int maxDictWords;
    char **dictionary;
    
    //
    void readDictionary (int *wordCount) {
    //
    
    int i;
    int maxWord;
    
    
    FILE *ifp = fopen("dictionary.txt", "r"); // open dict file
    
    fscanf (ifp, "%d", &maxWord); // read number of words in dictionary
    
    dictionary = malloc(maxWord * sizeof(char*));
    
    for (i = 0; i < maxWord; i++)
    
        dictionary[i] = malloc((MAX_LENGTH) * sizeof(char));
    
    for (i = 0; i < maxWord; i++) // load dictionary array with words
    
        {
    
        fscanf (ifp, "%s", dictionary[i]); // input words into dictionary array
        //printf ("%s\n", dictionary[i]);
    
        }
    
    fclose(ifp); // close dictionary.txt
    
    }
    
    
    //
    void testingDictionary (char dict[][MAX_LENGTH], int max) {
    //
    
    
    //testing dict
    
    int i;
    
    for (i = 0; i < max; i++)
    
        printf ("%s\n", dict[i]);
    
    
    }
    
    
    // MAIN
    int main(void) {
    //
    
    readDictionary(&maxDictWords);  // Read dictionary.txt into memory
    
    testingDictionary(*dictionary, maxDictWords);
    
    return 0;
    
    }
    Presently, the code prints the whole dictionary, but also a ton of erroneous data beyond the boundaries of the array.

    How should I send dictionary to the testingDictionary function so that it only prints what is in the dictionary array?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Firstly, the maxDictWords you pass to testingDictionary doesn't get initialized. You're using three variables for one thing - get rid of two and this confusion will go away.

    Secondly, make testingDictionary take a "char **" as its first argument and call it like testingDictionary(dictionary, maxDictWords) - there's no need to dereference dictionary, that will just give you the first string, not a pointer to the array of strings like you want.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm confused - you're using "maxDictWords" to determine how many words to print - but you're not giving it any values. It's declared, and passed to "readDictionary()", but "readDictionary()" stores that information in the local variable "maxWord". "maxDictWords" isn't even initialized!

    Why are you declaring those variables globally? You're passing them to the functions correctly, so why not just declare them in main?

    And I see some memory being allocated, but none of it being freed.

    There's a few other things I see, but let's start there.

    [edit] Dang! Too slow again!

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Excellent. Thanks very much.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by somniferium View Post
    I posted what a thought was important for brevity's sake.
    Brevity is important, but providing all relevant information is also important.

    Aim for conciseness: a code sample that is SMALL, COMPLETE, and REPRESENTATIVE of your problem. That does not mean taking out details that other people would reasonably expect.

    Brevity is not the only criterion. Making something small, incomplete, and not representative of your problem wastes time.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  2. Dynamic Memory Allocation
    By BoneXXX in forum C Programming
    Replies: 11
    Last Post: 03-26-2007, 01:43 AM
  3. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 07:54 AM
  4. Dynamic Memory Allocation Question
    By Piknosh in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2004, 01:55 PM
  5. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM