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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:15:41+00:00 2026-06-11T09:15:41+00:00

I’m trying to write a program that will read characters from stdin into a

  • 0

I’m trying to write a program that will read characters from stdin into a character array, so I can then perform other operations on that array. I wrote the program to dynamically allocate more memory for the array when needed. However, I always get a segmentation fault once I end the input of the program.

Notes:

  • I’m using an int, not a char, to store the characters being read in, so I believe the comparison to EOF should be valid?
  • The ch == 'l' line is just there because I got tired of pressing Ctrl+D twice, it’ll be removed once I get this problem solved.

The following is inside main, with stdlib/stdio #included at the beginning of the program:

int arrCount = 0, arrSize = 200, ch, key_data_len;
char* input = malloc(arrSize * sizeof(char));

printf("Input is an array of %d characters that takes up %d bytes.\n", arrSize, arrSize * sizeof(char));

// Add characters to array until input is finished.
while ( (ch = (int) getchar()) != '\0' && ch != EOF) {
  if(arrCount >= arrSize)
  { 
    printf("GOT IN IF STATEMENT.");
    // If the array has not been initialized, fix that.
    if (arrSize == 0)
      arrSize = 200 * sizeof(char);

    // Make the reallocation transactional by using a temporary variable first
    char *_tmp = (char *) realloc(input, (arrSize *= 2));

    // If the reallocation didn't go so well, inform the user and bail out
    if (!_tmp)
    {
      fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
      return(-1);
    }

    // Things are looking good so far
    input = _tmp;
  }

  printf("\narrCount = %d; ch = %c; sizeof(ch) = %d\n", arrCount, ch, sizeof(ch));
  input[arrCount++] = ch;

  printf("&input[%d] = %p\n", arrCount-1, &input[arrCount - 1]);
  printf("input[%d] = %c\n", arrCount - 1, input[arrCount - 1]);
  if (ch == 'l') {
    break;
  }
}

Example output:

… $ ./db

Input is an array of 200 characters that takes up 200 bytes.

tl

arrCount = 0; ch = t; sizeof(ch) = 4

&input[0] = 0x827a008

input[0] = t

input[0] = t

arrCount = 1; ch = l; sizeof(ch) = 4

&input[1] = 0x827a00a

input[1] = l

input[1] = t

Segmentation fault

Something else that might be linked to this: I noticed if I inputted enough characters for my input array to get to index 399 / size 400, this error popped up as well:

*** glibc detected *** ./db: realloc(): invalid old size: 0x08a73008 ***
  • 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-11T09:15:43+00:00Added an answer on June 11, 2026 at 9:15 am

    This is wrong, you’re freeing the array you just allocated:

    input = _tmp;
    free(_tmp);
    

    You don’t need the free at all – realloc does that for you.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I need a function that will clean a strings' special characters. I do NOT
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.