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

  • Home
  • SEARCH
  • 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 7960155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:41:02+00:00 2026-06-04T04:41:02+00:00

I already don’t know what to think or what to do. Next code compiles

  • 0

I already don’t know what to think or what to do. Next code compiles fine in both IDEs, but in VC++ case it causes weird heap corruptions messages like:
“Windows has triggered a breakpoint in Lab4.exe.

This may be due to a corruption of the heap, which indicates a bug in Lab4.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Lab4.exe has focus.

The output window may have more diagnostic information.”

It happens when executing Task1_DeleteMaxElement function and i leave comments there.
Nothing like that happens if compiled in Borland C++ 3.1 and everything work as expected.

So… what’s wrong with my code or VC++?

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>

void PrintArray(int *arr, int arr_length);
int Task1_DeleteMaxElement(int *arr, int arr_length);

int main()
{
    int *arr = NULL;
    int arr_length = 0;

    printf("Input the array size: ");
    scanf("%i", &arr_length);

    arr = (int*)realloc(NULL, arr_length * sizeof(int));

    srand(time(NULL));

    for (int i = 0; i < arr_length; i++)
        arr[i] = rand() % 100 - 50;

    PrintArray(arr, arr_length);

    arr_length = Task1_DeleteMaxElement(arr, arr_length);

    PrintArray(arr, arr_length);

    getch();

    return 0;
}

void PrintArray(int *arr, int arr_length)
{
    printf("Printing array elements\n");

    for (int i = 0; i < arr_length; i++)
        printf("%i\t", arr[i]);

    printf("\n");
}

int Task1_DeleteMaxElement(int *arr, int arr_length)
{
    printf("Looking for max element for deletion...");

    int current_max = arr[0];

    for (int i = 0; i < arr_length; i++)
        if (arr[i] > current_max)
            current_max = arr[i];

    int *temp_arr = NULL;
    int temp_arr_length = 0;

    for (int j = 0; j < arr_length; j++)
        if (arr[j] < current_max)
        {
            temp_arr = (int*)realloc(temp_arr, temp_arr_length + 1 * sizeof(int)); //if initial array size more then 4, breakpoint activates here
            temp_arr[temp_arr_length] = arr[j];
            temp_arr_length++;
        }


    arr = (int*)realloc(arr, temp_arr_length * sizeof(int));
    memcpy(arr, temp_arr, temp_arr_length);
    realloc(temp_arr, 0); //if initial array size is less or 4, breakpoint activates at this line execution

    return temp_arr_length;
}
  • 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-04T04:41:03+00:00Added an answer on June 4, 2026 at 4:41 am

    My guess is VC++2010 is rightly detecting memory corruption, which is ignored by Borland C++ 3.1.

    How does it work?

    For example, when allocating memory for you, VC++2010’s realloc could well “mark” the memory around it with some special value. If you write over those values, realloc detects the corruption, and then crashes.

    The fact it works with Borland C++ 3.1 is pure luck. This is a very very old compiler (20 years!), and thus, would be more tolerant/ignorant of this kind of memory corruption (until some random, apparently unrelated crash occurred in your app).

    What’s the problem with your code?

    The source of your error:

    temp_arr = (int*)realloc(temp_arr, temp_arr_length + 1 * sizeof(int))
    

    For the following temp_arr_length values, in 32-bit, the allocation will be of:

    • 0 : 4 bytes = 1 int when you expect 1 (Ok)
    • 1 : 5 bytes = 1.25 int when you expect 2 (Error!)
    • 2 : 6 bytes = 1.5 int when you expect 3 (Error!)

    You got your priotities wrong. As you can see:

    temp_arr_length + 1 * sizeof(int)
    

    should be instead

    (temp_arr_length + 1) * sizeof(int)
    

    You allocated too little memory,and thus wrote well beyond what was allocated for you.

    Edit (2012-05-18)

    Hans Passant commented on allocator diagnostics. I took the liberty of copying them here until he writes his own answer (I’ve already seen coments disappear on SO):

    It is Windows that reminds you that you have heap corruption bugs, not VS. BC3 uses its own heap allocator so Windows can’t see your code mis-behaving. Not noticing these bugs before is pretty remarkable but not entirely impossible.

    […] The feature is not available on XP and earlier. And sure, one of the reasons everybody bitched about Vista. Blaming the OS for what actually were bugs in the program. Win7 is perceived as a ‘better’ OS in no small part because Vista forced programmers to fix their bugs. And no, the Microsoft CRT has implemented malloc/new with HeapAlloc for a long time. Borland had a history of writing their own, beating Microsoft for a while until Windows caught up.

    […] the CRT uses a debug allocator like you describe, but it generates different diagnostics. Roughly, the debug allocator catches small mistakes, Windows catches gross ones.

    I found the following links explaining what is done to memory by Windows/CRT allocators before and after allocation/deallocation:

    • http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9535/Inside-CRT-Debug-Heap-Management.htm
    • https://stackoverflow.com/a/127404/14089
    • http://www.nobugs.org/developer/win32/debug_crt_heap.html#table

    The last link contains a table I printed and always have near me at work (this was this table I was searching for when finding the first two links… :- …).

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

Sidebar

Related Questions

I know there are already some solutions to this question but they don't seem
I have already download the windows phone develop tools but I don't know how
I have read some threads about this already but I don't understand the code,
Please don't confuse with the title as it was already asked by someone but
If I don't know the input size already, what is the the way to
I already read the datasheet and google but I still don't understand something. In
I've already look at this post but I still don't understand how to just
NOTE: Please, don't code it for me. I already have a home-rolled function to
I don't know how to add new item to already existing hash. For example,
I don't know why it doens't work. My TextView already set lines=6. String is

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.