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 6754361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:16:14+00:00 2026-05-26T13:16:14+00:00

I need a string array which dynamically resizes when more items are added to

  • 0

I need a string array which dynamically resizes when more items are added to it. I got the basic code working, but valgrind reports memory leaks.

The implementation which should work with static and dynamically allocated strings looks like this:

typedef struct {
    char **items;
    int num;
} StringArray;

StringArray* string_array_init() {
    StringArray *arr = malloc(sizeof(StringArray));
    arr->items = NULL;
    arr->num = 0;
    return arr;
}

void string_array_add(StringArray *arr, char *str, int str_len) {
    void *new_items = realloc(arr->items, (arr->num + 1) * sizeof(char *));

    arr->items = (char**)new_items;
    arr->items[arr->num] = strndup(str, str_len);
    arr->num++;
}

void string_array_cleanup(StringArray *arr) {
    int i;

    for (i = 0; i < arr->num ; i++) {
        free(arr->items[i]);
    }

    free(arr);
}




int main() {
    StringArray *arr = string_array_init();

    string_array_add(arr, "item 1", strlen("item 1"));
    string_array_add(arr, "item 2", strlen("item 2"));
    string_array_add(arr, "item 3", strlen("item 3"));

    string_array_cleanup(arr);

    return 0;
}

Valgrind reports:

==31443== HEAP SUMMARY:
==31443==     in use at exit: 12 bytes in 1 blocks
==31443==   total heap usage: 7 allocs, 6 frees, 53 bytes allocated
==31443== 
==31443== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31443==    at 0x4025CCD: realloc (vg_replace_malloc.c:525)
==31443==    by 0x80484A6: string_array_add (in ...)
==31443==    by 0x8048593: main (in ...)
==31443== 
==31443== LEAK SUMMARY:
==31443==    definitely lost: 12 bytes in 1 blocks
==31443==    indirectly lost: 0 bytes in 0 blocks
==31443==      possibly lost: 0 bytes in 0 blocks
==31443==    still reachable: 0 bytes in 0 blocks
==31443==         suppressed: 0 bytes in 0 blocks
==31443== 

Why is realloc leaking and how can I fix it? I thoght freeing every string separately and then freeing the struct would be enough, but I´m missing something.

  • 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-26T13:16:15+00:00Added an answer on May 26, 2026 at 1:16 pm

    You free the strings contained in arr->items, but you don’t free arr->items itself. (You allocated it in string_array_add).

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

Sidebar

Related Questions

I need to search a string in the string array. I dont want to
I need an advice about how to create new string array from 4 different
I need just dictionary or associative array string => int . There is type
I need to split my string input into an array at the commas. Is
I need to create an array by searching a string for occurrences of '['
I have a string that I need to convert to the equivalent array of
In C# I need to split a string (a log4j log file) into array
How do i explode this string '||25||34||73||94||116||128' i need to have a array like
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I have a string. I need to replace all instances of a given array

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.