Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:23:39+00:00 2026-05-19T04:23:39+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:23:40+00:00Added an answer on May 19, 2026 at 4:23 am

    You use pointers.

    Specifically, you use a pointer to an address, and using a standard c library function calls, you ask the operating system to expand the heap to allow you to store what you need to.

    Now, it might refuse, which you will need to handle.

    The next question becomes – how do you ask for a 2D array? Well, you ask for an array of pointers, and then expand each pointer.

    As an example, consider this:

    int i = 0;
    char** words;
    words = malloc((num_words)*sizeof(char*));
    
    if ( words == NULL )
    {
        /* we have a problem */
        printf("Error: out of memory.\n");
        return;
    }
    
    for ( i=0; i<num_words; i++ )
    {
        words[i] = malloc((word_size+1)*sizeof(char));
        if ( words[i] == NULL )
        {
            /* problem */
            break;
        }
    }
    
    if ( i != num_words )
    {
        /* it didn't allocate */
    }
    

    This gets you a two-dimensional array, where each element words[i] can have a different size, determinable at run time, just as the number of words is.

    You will need to free() all of the resultant memory by looping over the array when you’re done with it:

    for ( i = 0; i < num_words; i++ )
    {
        free(words[i]);
    }
    
    free(words);
    

    If you don’t, you’ll create a memory leak.

    You could also use calloc. The difference is in calling convention and effect – calloc initialises all the memory to 0 whereas malloc does not.

    If you need to resize at runtime, use realloc.

    • Malloc
    • Calloc
    • Realloc
    • Free

    Also, important, watch out for the word_size+1 that I have used. Strings in C are zero-terminated and this takes an extra character which you need to account for. To ensure I remember this, I usually set the size of the variable word_size to whatever the size of the word should be (the length of the string as I expect) and explicitly leave the +1 in the malloc for the zero. Then I know that the allocated buffer can take a string of word_size characters. Not doing this is also fine – I just do it because I like to explicitly account for the zero in an obvious way.

    There is also a downside to this approach – I’ve explicitly seen this as a shipped bug recently. Notice I wrote (word_size+1)*sizeof(type) – imagine however that I had written word_size*sizeof(type)+1. For sizeof(type)=1 these are the same thing but Windows uses wchar_t very frequently – and in this case you’ll reserve one byte for your last zero rather than two – and they are zero-terminated elements of type type, not single zero bytes. This means you’ll overrun on read and write.  

    Addendum: do it whichever way you like, just watch out for those zero terminators if you’re going to pass the buffer to something that relies on them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use jQuery validation for a dynamic form I'm setting up. In
Can jquery-ajax reload a div WITH DYNAMIC CONTENTS. For example if i have a
I have this infoflyout.ascx <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<dynamic> %> <script type=text/javascript> $(document).ready(function () {
Suppose (for the sake of argument) that I have a view class which contains
I'm trying to use the JQuery Validation's addClassRules method in my ASP.NET MVC application.
I need to create a type at runtime using the TypeBuilder. This type should
Is there anything else that the code must do to sanitize identifiers (table, view,
Suppose I am creating an Android application that's like an SMS app. The requirements
Suppose I want to completely take over the open() system call, maybe to wrap
Is it possible to optionally serialize the properties of a class via JAX-B using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.