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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:26+00:00 2026-06-12T19:07:26+00:00

Possible Duplicate: Realloc is not resizing array of pointers Can anyone tell me where

  • 0

Possible Duplicate:
Realloc is not resizing array of pointers

Can anyone tell me where my mistake is? this function should make an array of the characters supplied from stdin. I read some related questions but it was too complicated for me to understand.

char *readChar(void)
{
    int c;
    int len = 0;
    char* parr = malloc(sizeof(char));

    while((c = getchar()) != EOF)
    {
        ++len;
        int size = sizeof(char)*len;
        parr = (char *) realloc(parr,size);
        *(parr+size-1) = (char) c;
        printf("Done! Size should be: %dB, but real size is %dB\n",size,sizeof(parr));
    }

    return parr;
}

Output:

Done! Size should be: 1B, but real size is 8B
Done! Size should be: 2B, but real size is 8B
Done! Size should be: 3B, but real size is 8B
Done! Size should be: 4B, but real size is 8B

  • 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-12T19:07:27+00:00Added an answer on June 12, 2026 at 7:07 pm

    The mistake is that sizeof() won’t tell you the number of bytes you allocated. It tells you the size of the char *

    Sounds like what you wanted was to verify that you allocated the correct amount of memory. sizeof() won’t give you that information. It’s in the low level details at this point, you need to do something OS dependent to try and find that information. (ex. in the Linux kernel if you kmalloc() memory you can use ksize() to find out exactly how many bytes you got)

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

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: How do you realloc in C++? I know that C++ arrays can
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

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.