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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:49:56+00:00 2026-05-24T16:49:56+00:00

I have an integer array sorting program here but I have a problem: whenever

  • 0

I have an integer array sorting program here but I have a problem: whenever I run the program I sometimes get a “The stack around the variable ‘numbers’ was corrupted” message and sometimes it just repeatedly prints out the number 8. Here’s my code (compiled in Visual C++ 2010):

#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;

void swap(int *x, int *y)
{
    int tmp=0;
    tmp = *x;
    *x  = *y;
    *y  = tmp;
    tmp = 0;
}

int main()
{
    int numbers[13] = {8,16,23,487,2,301,48,0,13,10,644,12};

    int size = sizeof(numbers) / sizeof(int);

    //sort

    int i = 0;
    int* a = &numbers[0];
    int* b = &numbers[1];


    while(i < size){

        if(*a > *b){
            swap(a, b);
        }

        *a++;
        *b++;
         i++;
    }

    //Print our results
    int loopIterator = 0;
    int numToPrint = 0;
    while(loopIterator < size){
        cout << numbers[numToPrint] << endl;
        loopIterator++;
    }


    system("PAUSE");

}
  • 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-24T16:49:57+00:00Added an answer on May 24, 2026 at 4:49 pm

    First, you never increment numToPrint, therefore you are never going to print more than the value of numbers[0]. At the very least change your code to:

    while(loopIterator < size){
        cout << numbers[numToPrint++] << endl;
        loopIterator++;
    }
    

    Secondly, since your while loop uses the test i < size, you are, on the last iteration of the loop, going to be accessing memory outside of numbers for your b pointer, and possibly swapping that value into the last slot of numbers (i.e., where a is pointing to). You want to change your test to i < (size - 1) to avoid that scenario. For instance, if at i == 0 you have a = &numbers[0] and b = &numbers[1], then by the time that i == 12, you are going to end up with a = &numbers[12] and b = &numbers[13] … the value that b is pointing to in this instance is past the end of the array. Depending on how your compiler has setup the stack, and the way you’ve allocated numbers on the stack, this could actually play some havoc with your program should you end up with b pointing into the activation record data-structures for your main() function, and in-turn corrupting it.

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

Sidebar

Related Questions

Welcome. I have a radix sorting method that uses an array to go through,
Suppose you have an array of 1000 random integer numbers and you need to
Ok, so I have a simple c++ program that's supposed to run a couple
I have a randomly ordered C array of integer numbers. Each of these numbers
I have an array containing multiple integers, is there a common way for sorting
I have an array and an integer value. $amenityIds = array('1','2','3','4','5'); $propertyId = 1;
I have an array which have this structure: $queues[n] Array ( [id] => integer
I have an integer array like {1,2,3,4,5,0,0,0,0,0,2,3,4,5,7,8,0,0,0,0,0,1} I want to check if the array's
This should be simple. I have an integer array that I want to encode
I have a 2-dimensional integer array (say 1000 by 1000), let's call it matrix.

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.