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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:32:11+00:00 2026-06-16T05:32:11+00:00

I am trying to use realloc function as making the array bigger as the

  • 0

I am trying to use realloc function as making the array bigger as the user entered names. It gives me an error when I add 5. element, the error like :
* glibc detected ./a.out: realloc(): invalid next size: 0x00000000017d2010 **
And the code is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
  char **mtn = NULL;
  char x[30];
  int i = 0;

  while ( strcmp(gets(x), "finish") ){
    mtn = realloc( mtn, i*sizeof(char) );
   // mtn[i] = realloc( mtn[i], sizeof(x) ); // tried but didnt work
    mtn[i] = x;
    i++;
  }
  puts(mtn[1]);

  return 0;
}
  • 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-06-16T05:32:12+00:00Added an answer on June 16, 2026 at 5:32 am
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char **mtn = NULL;
        char x[30];
        int i = 0;
    
        /* Never use gets, it's dangerous, use fgets instead */
        while (strcmp(fgets(x, sizeof(x), stdin), "finish\n")){
            /*
            your previous realloc was realloc(mtn, 0)
            and you have to take space for <char *>
            */
            mtn = realloc(mtn, (i + 1) * sizeof(char *));
            /* always check return of xalloc */
            if (mtn == NULL) {
                perror("realloc");
                exit(EXIT_FAILURE);
            }
            /* you still need space for store x */
            mtn[i] = malloc(strlen(x) + 1);
            if (mtn[i] == NULL) {
                perror("malloc");
                exit(EXIT_FAILURE);
            }
            strcpy(mtn[i], x); /* mtn[i] = x is not valid */
            i++;
        }
        printf("%s", mtn[1]);
        /* always free xallocs in order to prevent memory leaks */
        while (i--) free(mtn[i]);
        free(mtn);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use epoch time dates in my series data. The array looks like
I revieving the following error when trying use mysql_real_escape() function Fatal error: Call to
I have been trying use the edit_post_link() function to contain an image. All of
I'm new to C and am trying to use the read function. I want
I'm trying use to selenium with firefox on CentOS from command line like this:
I am trying use a from a multi-dimensional array that I create in another
I am trying use the onClick() function in an activity for an android app.
I am trying use strtotime and date functions within a class to add a
In my program I am trying to resize array using malloc function. #include <stdio.h>
I'm trying use jquery to remove a class from an element (not knowing ahead

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.