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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:50:18+00:00 2026-05-28T00:50:18+00:00

I’m trying to keep a dynamically allocated array of strings that are read in

  • 0

I’m trying to keep a dynamically allocated array of strings that are read in using the read system call in c. Here is a small sample of what I am trying to do:

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

void processInput() {
    char ** array = (char **) calloc(20, sizeof(char*));
    int arrayIndex = 0;
    while(1) {
        printf("Type something: ");
        fflush(stdout);

        char* buffer;
        int readResult = read(0, buffer, 100);

        array[arrayIndex] = (char*)calloc(readResult, sizeof(char));
    }
}    

However, this causes some strange issues:


    Type something: a
    Type something: Type something: a
    Type something: Type something: abcdefg
    Type something: Type something: Type something: Type something: Type something: Type something: Type something: Type something: 

Is there any explanation for this? I can’t seem to figure out why this is happening.

  • 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-28T00:50:18+00:00Added an answer on May 28, 2026 at 12:50 am

    Replace:

    char *buffer;
    

    with:

    char buffer[100];
    

    The dimension is consistent with the argument to read(), but your string will not be null terminated by read(), so you may prefer to allocate 101 bytes instead and forcibly null terminate it. You need to think about that extra byte when allocating the space.

    Note that the space that is allocated is not used.

    You should always error check memory allocations before using them.

    Since you only allocate 20 pointers before the loop, the while (1) loop is dangerous and could allow you to trample well beyond the allocated space if the user inputs more than 20 lines of data.

    (Note that if the user redirects input from a file, you won’t be reading 20 lines; you’ll be reading 2000 characters in 20 blocks of 100 characters, newlines and all, and then running into crash territory.)

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.