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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:01:17+00:00 2026-05-18T08:01:17+00:00

I am having a problem when free(position) is run. void printTree(nodeT node, int hieght)

  • 0

I am having a problem when free(position) is run.

void printTree(nodeT node, int hieght)
{
    int *position;
    position = (int*)malloc(hieght * sizeof(int*));
    for (int i = 0; i <= hieght; i++)
    {
        position[i] = 0;
    }
    BOOLEAN DONE = FALSE;
    while (DONE == FALSE)
    {
        printMoveDown(&node, position);
        printLEAFNode(&node, position);
        DONE = printMoveUp(&node, position);
        printSingleKey(&node, position);
    } 
    free(position);
    position = NULL;
}

The full error message I get from VS2010 is HEAP CORRUPTION DETECTED: after normal block (#64) at 0x00031390. CRT detected that the application wrote to memory after end of heap.

The debugger says the problem occurs while at in: dbgheap.c

extern "C" void __cdecl _free_dbg_nolock
line 1376:  if (!CheckBytes(pbData(pHead) + pHead->nDataSize, _bNoMansLandFill, nNoMansLandSize))
                if (pHead->szFileName) {..}
                else { this is where the program stops }

I tried setting up the same situation with less stuff going on to see if I could narrow the problem down.

void function (int y)
{
    int *x;
    x = (int*)malloc(y * sizeof(int*));
    free(x);
    x = NULL;
}

This is the same thing as above without the for loop and while loop. This works. Removing the for loop is what made it work. I don’t know why. I looked up what the CRT was but it was all pretty new concepts to me and I assume that I can solve this problem without knowing about these CRTs.

The for loop assigns values to the memory allocated for position, beyond that I can’t think of why this causes a problem…. actually now that I think about it. I changed the loop to be height + 1 which fixed the problem.

  • 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-18T08:01:17+00:00Added an answer on May 18, 2026 at 8:01 am

    It should be:

    position = malloc(hieght * sizeof(int));
    

    or:

    position = malloc(hieght * sizeof *position);
    

    It’s undefined behavior the way you have it. You’re probably just getting lucky because int and int* are the same size.

    And the typical correct way to write the loop is:

    for (int i = 0; i < hieght; i++)
    {
        position[i] = 0;
    }
    

    You can also use calloc here:

    position = calloc(hieght, sizeof *position);
    

    and the memory will be zeroed for you, so you don’t have to loop.

    Also, if this is really C, the cast is superfluous.

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

Sidebar

Related Questions

I'm having a problem with free() on a struct in my C program. When
Having problem with my application, I seem to be new in ios. My project
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
i having problem with draw image in applet. i want to display all images
I am having problem with Jquery tabs UI. <script type=text/javascript> $(document).ready(function() { $('.tabs').tabs(); $('.subtabs').tabs();
I am having problem rightnow because when I am logging in on mysql using
I'm having problem with getting location.hostname for newly created window in javascript. For example
I'm having problem with triggering function on delete_post action. I'm building functionality where i
I am having problem setting a variable in PHP for multiple use into if
I'm having problem with string redirection and python with a shell Ghostscript command. There

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.