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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:10:32+00:00 2026-05-30T13:10:32+00:00

Usually a malloc contains sizeof , but this one doesn’t and has i+1 instead:

  • 0

Usually a malloc contains sizeof , but this one doesn’t and has i+1 instead:

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);
  • 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-30T13:10:33+00:00Added an answer on May 30, 2026 at 1:10 pm

    If you wanted to allocate an array of some type, you would normally multiply the number of elements you wanted by the size of that type, because malloc takes the size of the array in bytes.

    However, an array of char is a special case; you do not need to multiply the number of elements you want by sizeof(char), because sizeof(char) is defined by the Standard to always be 1, and multiplication by 1 yields the other operand.

    The + 1 is to make room for the NUL terminator. If you want a string of length n, your array has to be of length n + 1; n spaces for the n characters of the string, and 1 space for the terminator.

    By the way, you shouldn’t cast the return value of malloc. It will make your code easier to change in the future.

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

Sidebar

Related Questions

I usually use pointers in the following manner char *ptr = malloc( sizeof(char) *
Usually when I need to fork in C, I do something like this: pid_t
Usually when I build a site, I put all the CSS into one file,
When I run this code through several entries of a directory, it usually returns
Usually, Django outputs a nice HTML traceback page once something goes wrong. This is
Usually I'd use Wireshark, but is there a really super quick way to determine
For example, articles usually say that if malloc and delete is paired up, or
Usually, when sending a http status header from PHP, one would send the HTTP
usually i simply develop executable (.exe) files of my MATLAB codes by using this
I have the problem in C program: char *str = (char *) malloc(20); strcpy_s(str,

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.