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

  • Home
  • SEARCH
  • 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 7905269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:24:31+00:00 2026-06-03T10:24:31+00:00

For the getline() function, I tried two method to allocate memory space for string,

  • 0

For the getline() function, I tried two method to allocate memory space for string, but the first one works, the second doesn’t. Can anyone explain why the second won’t work?

The first one

#include <stdio.h>

int main()
{
  int bytes_read;
  int nbytes = 100;
  char *my_string;

  puts ("Please enter a line of text.");

  /* These 2 lines are the heart of the program. */
  my_string = (char *) malloc (nbytes + 1);
  bytes_read = getline (&my_string, &nbytes, stdin);

  if (bytes_read == -1)
    {
      puts ("ERROR!");
    }
  else
    {
      puts ("You typed:");
      puts (my_string);
    }

  return 0;
}

The second one:

#include <stdio.h>
int main()
{
  int bytes_read;
  int nbytes = 100;
  char my_string[nbytes+1];

  puts ("Please enter a line of text.");

  /* These 2 lines are the heart of the program. */
  bytes_read = getline (&my_string, &nbytes, stdin);

  if (bytes_read == -1)
    {
      puts ("ERROR!");
    }
  else
    {
      puts ("You typed:");
      puts (my_string);
    }

  return 0;
}

The second one can be compiled, but when I execute it:

bash-3.2$ ./a.out 
Please enter a line of text.
lsdfa
Bus error: 10

It says Bus error: 10

I don’t know what is the possible reason, can anyone help me?

  • 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-03T10:24:32+00:00Added an answer on June 3, 2026 at 10:24 am

    The signature for getline requires a pointer to a char* so that it may be modified. This is because getline is supposed to be allowed to call realloc on the char* or allocate a char* if you pass a char* which points to 0:

    getline() reads an entire line from stream, storing the address of the buffer
    containing the text into *lineptr.

    …

    In either case, on a successful call, *lineptr and *n will be updated to
    reflect the buffer address and allocated size respectively

    In the first case everything is fine as the pointer you’ve passed to getline can be modified.

    In the second case you’re passing a pointer to a char array, which itself cannot be modified. As you’ve found, unfortunately &my_string ends up looking like char**, so the compiler does not complain (but it may with -Wall).

    Basically, since getline needs to be able to modify what lineptr points to, in your second case this cannot be done (hence the bus error).

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

Sidebar

Related Questions

Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
in VC++ environment Im using (string) getline function to read separate lines in opened
I'm writing a getline function below but I want the delimiting character to be
Hi i am trying to use getline() function by giving two arguments to it.
gcc 4.4.4 c89 I have the following function but I cannot free the memory.
The bug starts at cin.getline ( string, 25, '\n' ); or the line below
I am trying to write a C++ function that matches whether a string is
Right now I am only trying to get my getline() function to work. I
string add_text() { const int string_size(30); string add_string; cin.getline (add_string,string_size,'\n'); return add_string; } When
I implement a function that acts like getline( .. ). So my initial approach

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.