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

The Archive Base Latest Questions

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

I have been working on and off on an assignment for my C class

  • 0

I have been working on and off on an assignment for my C class for the last few days and encountered a curious crash concerning the realloc() function in C. Even the C/C++ programmers in house could not answer me right away what might be wrong with my code.

First i create the memory block in one function:

char *line = (char *)malloc( sizeof(char) * BUFSIZE);

Then i call getMoreBuf(start_of_block, end_of_block)

int getMoreBuf(char *start, char *end)
{
char *newBuf = 0;
int newSize = (end - start) + BUFSIZE;
    newBuf = (char *)realloc(start, sizeof(char) * newSize);
    if(NULL == newBuf) {
        printf("No virtual RAM available");
    }else{
        start = newBuf;
    }
    return newSize;
}

Depending on what i set the BUFSIZE to, it crashes after the 5th call (BUFSIZE = 1) or the 3rd call (BUFSIZE = 5) and replaces the read in characters with nonsense.

Could someone point me towards my error(s) and give suggestions where to read up on to fix them?
Any help is appreciated. 🙂

Bonus Question: I malloc a memory block with pointer 1 pointing to the start and later pointer 2 point2 towards a single block in the memory block. I realloc() the block and the block is moved due to size issues, does the pointer 2 still point to the old (now useless) block or does it “move” with the realloc to the new position of the memory block?

(Also for the future, should i put that extra question into a new question or can i leave it in here since it is strongly related to the first question?)

Thank you all for your input, it helped me a great deal to figure out what went wrong.
If i could i would mark each as the right answer since each helped my in some form to understand a bit more about all this damn pointer business. =)

  • 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-30T00:32:22+00:00Added an answer on May 30, 2026 at 12:32 am

    I think your problem is that you’re only changing the local variable start… You need to use a pointer.

    int getMoreBuf(char **start, char **end)
    {
        char *newBuf = 0;
        int newSize = (*end - *start) + BUFSIZE;
        newBuf = (char *)realloc(*start, sizeof(char) * newSize);
        if(NULL == newBuf) {
            printf("No virtual RAM available");
        } else {
            *start = newBuf;
            *end = *start + newSize;
        }
        return newSize;
    }
    

    And then you’d call it like:

    getMoreBuf(&start_of_block, &end_of_block)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working as a native C++ programmer for last few years. Now
I have been working on this frustrating bug for a few days now and
I have been working on this for a few days. I never messed with
I have been working on a web services related project for about the last
This is a weird one. On and off as I have been working on
Context: I have been working on and off on an application which uses a
I do html/css by trade, and I have been working on and off django
I have only been working with C# for a few weeks and I have
I have been working on a project that requires downloading a zipped file off
For background I have been working on an RPG based off Ray Wenderlich's tutorials.

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.