CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding a debug error.

    Here is the code,
    Code:
    void MergeArray(int arr[], int l, int m, int r)
    {
    	int* b = new int[r-1+1];
    
    	int i, j;
    
    	int index = 0;
    	for(i=l, j=m+1;i<=m && j<=r;)
    	{
    		if(arr[i]<arr[j])
    		{
    			b[index] = arr[i++];
    			index++;
    		}
    		else
    		{
    			b[index] = arr[j++];
    			index++;
    		}
    	}
    
    	if(i>m)
    	{
    		while(j<=r)
    		{
    			b[index] = arr[j++];
    			index++;
    		}
    	}
    	else
    	{
    		while(i<=m)
    		{
    			b[index] = arr[i++];
    			index++;
    		}
    	}
    
    	index = 0;
    	for(i=l;i<=r;++i)
    	{
    		arr[i] = b[index];
    		index++;
    	}
    
    	delete[] b;
    }
    
    void MergeSort(int arr[], int l, int r)
    {
    	if(l>=r)
    		return;
    
    	int m = (l+r)/2;
    
    	MergeSort(arr, l, m);
    	MergeSort(arr, m+1, r);
    
    	MergeArray(arr, l, m, r);
    }
    
    void MergeSort(int arr[], int size)
    {
    	MergeSort(arr, 0, size-1);
    }
    
    int main()
    {
    	int arr[] = { 4, 1, 22, 5, 12, 9, 13, 19, 3};
    
    	MergeSort(arr, 9);
    	return 0;
    }
    Actually, there is a debug error related to the pointer b defined in MergeArray. If I change the original statement int* b = new int[r-1+1] to int* b = new int[r-1+2], then it will fix the problem. So obviously b is referenced with an index out of boundary, but I couldn't find out where it happens. Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding a debug error.

    When you step through the code in the debugger (like you always do before posting to the forum, right?), what is the passed in value of r and why is it out of bounds?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: A question regarding a debug error.

    You are effectively changing
    Code:
    int* b = new int[r];
    to
    Code:
    int* b = new int[r + 1];
    Code:
    for(i=l, j=m+1;i<=m && j<=r;)
    	{
    		if(arr[i]<arr[j])
    The condition is j <= r. As arrays are indexed starting at 0, should your conditions be 'less than' rather than 'less than or equal'? What are l, m and r used?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: A question regarding a debug error.

    Just a marginal remark, adding to what the two other contributors already posted correctly: You started this thread in the wrong forum section; your code quite obviously is native C++, not C++/CLI (AKA managed C++), so this thread would be correctly placed in the C++ (non-MS VC) section. Perhaps a moderator can move the thread.

    (Actually, it's way more common that mistakenly threads about C++/CLI get started in one of the native C++-related sections than the other way round like here.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured