0

When I run my code the pointer becomes corrupted

main

#include <iomanip>
#include <iostream>
#include "Bucket.h"
using namespace std;

int main()
{
    const int numberCount = 11;

    int startingNumbers[numberCount] = { 100, 79, 255, 355, 70, 85, 10, 5, 47, 62, 75 };

    cout << "Initial Numbers" << endl;
    for (int count = 0; count < numberCount; count++)
    {
        cout << startingNumbers[count] << " ";
    }
    cout << endl;


    bucketSort(startingNumbers, numberCount);

    system("pause");
}

Bucket.h

void bucketSort (int startingNumbers[], int numberCount);

Bucket.cpp

#include <iostream>
#include "Bucket.h"
using namespace std;


void bucketSort(int startingNumbers[], int numberCount)
{
    const int cols = 10;
    const int rows = 11;

    int row = 0;
    int input = 0;

    int *finger[cols];

    int table[rows][cols];

    for (int count = 0; count < numberCount; count++)
    {
        row = startingNumbers[count] % 10;
        table[row][count] = startingNumbers[count];

        finger[count] = &table[row][count];
    }

    for (int count = 0; count < numberCount; count++)
    {
        for (int col = 0; col < cols + 1; col++)
        {
            if (table[count][col] == *finger[col])
            {
                startingNumbers[input] = *finger[col];
                input++;
            }
        }
    }
    for (int count = 0; count < numberCount; count++)
    {
        cout << startingNumbers[count] << endl;
    }
}

It doesn't become corrupt according to visual studio until the end, am I using the pointer wrong?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Evan
  • 37
  • 6
  • 2
    Here `input ++;` can be larger than the size of the array. You overwrite past the end of the array, corrupting the values of other variables. – Mustafa Ozturk May 04 '15 at 18:52
  • 1
    I would drop the c++14 from the tags. – drescherjm May 04 '15 at 18:55
  • @Evan, your code is very buggy, I don't understand what you're trying to do. – Mustafa Ozturk May 04 '15 at 18:57
  • @Mustafa sorry in my first year of C++ and just a project we have assigned and we need to make a bucket sort that reorganizes it. – Evan May 04 '15 at 18:59
  • @Evan You may use my pointer.:) – Vlad from Moscow May 04 '15 at 19:04
  • @Evan if you are still monitoring this question, I suggest using names and terminology that fits the task for variables, methods, and functions. Don't put data in `finger` and `table` during a bucket sort. Put it in `bucket` and `bucketList`. It doesn't mean a whit to the compiler, but it makes your intent much easier to interpret and almost self-documenting. – user4581301 May 04 '15 at 19:58
  • @user4581301 thank you will do next time.. is there a way to close it considering i have had it answered now? – Evan May 04 '15 at 20:17
  • Here is a link to a Question & Answer here on StackOverflow http://stackoverflow.com/q/9317248/1757805 – Francis Cugler May 04 '15 at 22:21

1 Answers1

1

One issue is here:

    const int cols = 10;
    //...
    int *finger[cols];
    //...
    for (int count = 0; count < numberCount; count++)
    {
        row = startingNumbers[count] % 10;
        table[row][count] = startingNumbers[count];
        finger[count] = &table[row][count];  // <---- Issue Here
    }

Your finger array is declared as having cols number of entries, but your loop access finger using count, which will exceed the cols value. In other words, you declared your fingers array with a certain size, but when you start to do things with it, you do not respect the boundaries of the fingers array.

The fix is to make sure you never go past the end of the fingers array, or if the intent was to make fingers have numberCount elements, then use std::vector:

#include <vector>
//...
std::vector<int *> fingers(numberCount);

Then the code stays the same and will not crash. Whether the results are what you're looking for in terms of having sorted numbers, that's another story. But your code will not crash if you make these changes.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thank you! This is one of those moments when i smack my self in the face for not figuring that out. – Evan May 04 '15 at 19:27