Thread: Using Dynamic Memory allocation for arrays

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    Unhappy Using Dynamic Memory allocation for arrays

    How am I supposed to use dynamic memory allocations for arrays?

    For example here is the following array in which i read individual words from a .txt file and save them word by word in the array:

    Code:
    char words[1000][15];
    Here 1000 defines the number of words the array can save and each word may comprise of not more than 15 characters.

    Now I want that that program should dynamically allocate the memory for the number of words it counts. For example, a .txt file may contain words greater that 1000. Now I want that the program should count the number of words and allocate the memory accordingly.

    Since we cannot use a variable in place of [1000], I am completely blank at how to implement my logic. Please help me in this regard.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can make things simple for you with a typedef:
    Code:
    typedef char Word[15];
    Now, you can write something like:
    Code:
    Word *words = malloc(sizeof(*words) * words_capacity);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    How can I apply malloc to multidimensional arrays ??
    Code:
    words[1000][15];
    I want to dynamically allocate this [1000] part and the [15] part may remain constant.
    So that the program is not limited to 1000 words only. I hope you got wat I am trying to ask.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you not understand about my answer in post #2?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    I don't know what typedef is and I have studied malloc a bit.....So it wud be very kind of u if u wud elaborate....Thanks in advance..

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, what you want to do is to define a pointer to an array of 15 chars, then use malloc with it. This is the direct way of doing it:
    Code:
    char (*words)[15] = malloc(sizeof(*words) * words_capacity);
    By using typedef, I simplify the syntax by declaring that Word is a synonym for the array of 15 chars type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by mrafay View Post
    How can I apply malloc to multidimensional arrays ??
    Code:
    words[1000][15];
    I want to dynamically allocate this [1000] part and the [15] part may remain constant.
    So that the program is not limited to 1000 words only. I hope you got wat I am trying to ask.

    Hmmm. Are you sure you want to make the first dimension dynamic? You will need
    some place to store the pointers for the dynamically allocated memory. If you need
    this to be all dynamic, maybe a linked list would be a better solution.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kona49er
    You will need some place to store the pointers for the dynamically allocated memory.
    Considering that you only need one pointer given the requirements, that's rather easy
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by laserlight View Post
    Considering that you only need one pointer given the requirements, that's rather easy
    With a single pointer, how would he know how much memory to malloc? I seems like
    he wants this array to grow dynamically (no bounds on the first dimension).

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kona49er View Post
    With a single pointer, how would he know how much memory to malloc? I seems like
    he wants this array to grow dynamically (no bounds on the first dimension).
    Since the second dimension is fixed at 15 (and more importantly, fixed at compile-time) no further pointers are needed. ptr[0] is at the returned value from malloc. ptr[1] is fifteen bytes further along. ptr[2] is fifteen bytes further along from that, etc. Also: since ptr is of type pointer-to-array, ptr[0] is of type array, meaning further subscripts can be used.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by tabstop View Post
    Since the second dimension is fixed at 15 (and more importantly, fixed at compile-time) no further pointers are needed. ptr[0] is at the returned value from malloc. ptr[1] is fifteen bytes further along. ptr[2] is fifteen bytes further along from that, etc. Also: since ptr is of type pointer-to-array, ptr[0] is of type array, meaning further subscripts can be used.

    Sure but ... I'm still wondering how the guy would know how much memory to malloc.
    He does not know how many words he will be reading.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kona49er View Post
    Sure but ... I'm still wondering how the guy would know how much memory to malloc.
    He does not know how many words he will be reading.
    Code:
    number_of_words = 0;
    while (1==fscanf(infile, "%s", &scratch)) number_of_words++;
    You won't know until you read them in. (Alternatively, you could start with 10, then if you need more get 20, then get 40, then get 80, etc. using realloc.)

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by tabstop View Post
    Code:
    number_of_words = 0;
    while (1==fscanf(infile, "%s", &scratch)) number_of_words++;
    You won't know until you read them in. (Alternatively, you could start with 10, then if you need more get 20, then get 40, then get 80, etc. using realloc.)

    Yes, very good suggestions. Thanks.

  14. #14
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Isn't that a bit of a slow way? You read the entire file, then allocate an array big enough for the file, and THEN read the file into the array?

    That basically means reading over this thing 3 times. That is some pretty bad time complexity.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by \007 View Post
    Isn't that a bit of a slow way? You read the entire file, then allocate an array big enough for the file, and THEN read the file into the array?

    That basically means reading over this thing 3 times. That is some pretty bad time complexity.
    I would think twice. (And while it is inefficient, the time complexity would naturally be the same O(n) as nearly anything else, assuming n is the number of lines. The realloc method I gave is also O(n).)

    It would be nice if the first line of the file said how many other lines there were, but that doesn't appear to be the case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM