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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:33:23+00:00 2026-06-18T11:33:23+00:00

A part of my code will read in an unknown number of lines from

  • 0

A part of my code will read in an unknown number of lines from a text file, parse that line into a structure (tempSomeStruct), resize the SomeStruct_Array, and then add that tempSomeStruct into the newly opened spot in memory.

However after a few times through the while loop, my program stops and says

myApplication.exe has triggered a breakpoint.

I did not set a breakpoint, and doing some digging, it LOOKS like the breakpoint is due to heap corruption from my call to realloc. I am pretty new to dynamic allocation, so while I have searched and found a few possible causes, so far no fixes have worked.

How am I corrupting the heap in this situation, and what do I do differently to avoid doing so?

I have a function like this:

int growArray(SomeStruct **SomeStruct_Array,int currentSize, int numNewElements)
{
    const int totalSize = currentSize + numNewElements;
    SomeStruct *temp = (SomeStruct*)realloc(*SomeStruct_Array,(totalSize * sizeof(SomeStruct)));
    if (temp == NULL)
    {
        printf("Cannot allocate more memory.\n");
        return 0;
    }
    else
    {
        *SomeStruct_Array = temp;
    }

    return totalSize;
}

and it is called in elsewhere like this:

SomeStruct* SomeStruct_Array = (SomeStruct *) calloc(1,sizeof(SomeStruct));
int Error_Array_Size = 0;

if(SomeStruct_Array == NULL)
{
   printf("Cannot allocate initial memory for data\n");
   return;
}

while(fgets(line,sizeof(line), file) != NULL)
{
   parseTextIntoSomeStruct(line, &tempSomeStruct);
   SomeStruct_Array_Size = growArray(&SomeStruct_Array,SomeStruct_Array_Size,1);
   if(SomeStruct_Array_Size > 0)
   {
      SomeStruct_Array[SomeStruct_Array_Size] = tempSomeStruct;
   }
}
  • 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-18T11:33:24+00:00Added an answer on June 18, 2026 at 11:33 am

    Your new array’s size is SomeStruct_Array_Size, and you immediately write to SomeStruct_Array[SomeStruct_Array_Size] which is one past the end of the array! Remember, C arrays are zero-indexed.

    Use

    SomeStruct_Array[SomeStruct_Array_Size-1] = tempSomeStruct;
    

    instead.

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

Sidebar

Related Questions

I have a C++ program that will read in data from a binary file
I have some code that at one part will get executed a lot, and
I am currently writing a program that will read part of the windows system
I wrote this rake task that enables me to read a csv file from
I have this part of code that takes a file and puts it in
Part of my code will output HTML code to a file. I have a
I'm Building an application that will read every new sms from inbox and it
I'm doing some Wave file handling and have them read from disk into an
I will keep this quick. The attached code for the most part works i
I want to test my part of code that returns the users password question.

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.