Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a memory address in my array?

Tags:

c++

I'm writing a C++ application that has a user enter a 0 (zero) or a one (1) as input, then stores the numbers as an array and sorts them so that the zeros come first and the ones come last.

However, I think I'm getting a memory address in my array that's messing up the sorting operation.

The function that accepts input looks like this:

cout << "Please enter number " << i+1 << ":\n";
cin >> ar[i];

Then there's a function that's called that sorts the input and displays the sorted list:

sort_elements(ar, number);

... and that function looks like this:

void sort_elements(int ar[], long int num_elements) {
    int temp_num;
    num_elements -= 1; //since the array starts at 0

    cout << "num_elements is " << num_elements << "\n";
    for (int i=0; i < (num_elements/2); i++ ) {
        if (ar[i] > ar[num_elements-i]) {
            temp_num = ar[i];
            ar[i] = ar[num_elements-i];
            ar[num_elements-i] = temp_num;
        }
    }   
    cout << "Here's your neatly sorted list of numbers: \n";
    for (int j=0; j <= num_elements; j++) {
        cout << ar[j] << ", ";   
    }
    cout << "\n";   
}

For a five number input, starting with three "1"s, and ending with two "0"s, this results in an output that looks like this:

1, 0, 1, 1, 1892218304, 

I'm assuming the 1892218304 is a memory address, that's messing up the input. Though I don't really know.

Can anyone figure out why my sort operation is getting messed up?

Thanks in advance.

like image 754
rottendevice Avatar asked Aug 12 '26 09:08

rottendevice


2 Answers

Suggestion Use vector and sort in standard library

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>

int main()
{
  std::vector<int> v;
  for(int i=0; i < 10; i++)
  {
    v.push_back(i);
  }

  std::sort(v.begin(), v.end());

  return 0; 
}
like image 146
Jim Avatar answered Aug 14 '26 21:08

Jim


The number you are seeing is not a memory address, but the value of the 4 bytes either immediately before or immediately after your array, interpreted as an int. Your code has an off-by-one error that causes an access to just outside the array. That much I suspect even though I don't have proof.

However, I can't find anything wrong with the code you posted that would cause it to access outside the array bounds.

Are you sure that num_elements has the correct value when this function is called?

Update to address the pastebin code

Things are going wrong already from the start:

int number;
int ar[number]

This is called a variable-length array and it's not legal in C90 or any flavor of C++. That your program compiles is probably "thanks to" a compiler extension... which helpfully raises a bug: the value of number is not initialized before the array is allocated.

You need to do one of the following:

  • declare ar as an array of constant size (a hard limit on the number of inputs you can accept)
  • dynamically allocate ar with new[] after number is entered from the user
  • (by far preferable) use an std::vector instead of an array
like image 41
Jon Avatar answered Aug 14 '26 22:08

Jon