-5

Is it possible to write a C++ program that allocates a user-inputted number of elements and gives them user-inputted values, and can then point directly to any element of those that have just been allocated, without having to use pointer arithmetic? I'm asking this because the human brain doesn't think that "the 7th element is the address of the first element + 6 steps ahead"; it can absolutely bind dynamically created elements and its current set of properties. If this is possible in C++, it would naturally speed up program execution.

Example code to clarify what I'm referring to:

//
//  main.cpp
//  This code doesn't compile
//  but it's for clarification purposes
//  Created by Måns Nilsson on 2017-01-02.
//  Copyright © 2017 Måns Nilsson. All rights reserved.
//

#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    int currval;
    int otherval;
    int arrlength;
    cout<<"How many elements would you like to allocate?"<<endl<<"Your input: ";
    cin >> arrlength;
    int numlist[arrlength];

    //User inputs the element values and variables are assigned to
    //point directly to each element
    for(int i=0;i<arrlength;i++) {
        cout<<"Enter a value for element "<<(i + 1)<<": ";
        cin >> currval;
        numlist[i] = currval;
        int &var(i) = numlist[i];
    }

        cout<<"Enter the index of the element you want to modify: ";
        cin >> currval;
        cout<<"Enter a new value for element "<<currval<<": ";
        cin >> otherval;
        var(currval-1) = otherval;

    return 0;
}
Måns Nilsson
  • 431
  • 1
  • 4
  • 16
  • 1
    *"If this is possible in C++, it would naturally speed up program execution."* Huh? Why would you think that? – Baum mit Augen Jan 02 '17 at 14:19
  • 1
    I'm not sure I understand the question. If you have, say, `int* arr = new int[someSize];`, you can write `arr[7]` - it has exactly the same meaning as `*(arr + 7)`. What again seems to be the problem? – Igor Tandetnik Jan 02 '17 at 14:23
  • You start at the address of the first element, and add 6 to it, so you get to *(arr+6) which is the address of the 7th element. But you can't dynamically create new variables to directly know the element addresses. It still uses pointer arithmetic. – Måns Nilsson Jan 02 '17 at 14:25
  • Dynamically allocating new variables to point to each of the previously dynamically allocated elements would probably result in slowing down your program, rather than the expected speedup. Pointer arithmetic is insanely fast. It's not a problem to fix. – DeiDei Jan 02 '17 at 14:26
  • 2
    Imagine that, somehow, the facility you wish for exists. How would you use it? Show a hypothetical code you would have written using such a facility, but cannot without. Color me dense, but I have no idea what it is you are complaining about; perhaps an example would help me understand. – Igor Tandetnik Jan 02 '17 at 14:26
  • Are you thinking about references to an array element? – Serge Ballesta Jan 02 '17 at 14:26
  • Yes. Immediately referencing any dynamically allocated element without using arithmetic. – Måns Nilsson Jan 02 '17 at 14:27
  • 1
    But which element is "any element"? How do you plan to identify a specific element you wish to work with, if not by its index within the array? – Igor Tandetnik Jan 02 '17 at 14:30
  • 1
    Hey, if you have the vision to reimplement all the logic that we've built upon for the last half a century, go for it. But you'll have to convince a great deal of people. – DeiDei Jan 02 '17 at 14:33
  • Igor: By its index, of course, but not by starting on the address of the first element and then incrementing that value. Imagine that listitem[6] did not require pointer arithmetic to grab. – Måns Nilsson Jan 02 '17 at 14:37
  • 1
    By what magic would you go from (a) an address returned by `new` and (b) an index of an element, to (c) the address of said element, without adding up (a) and (b)? I still don't follow the thrust of your argument, and still maintain that an example would go a long way towards explaining it, should you care to provide one. – Igor Tandetnik Jan 02 '17 at 14:40
  • @MånsNilsson: You seem to assume that C++ works the same as your CPU. That's plain untrue. For instance, the x86 _does_ actually have the method you describe, which is called `LEA` - Load Effective Address. But that's so convenient that C++ compilers use it to implement pointer arithmetic, and even non-pointer arithmetic (!!) – MSalters Jan 02 '17 at 15:05
  • Who is deleting my comments? – Måns Nilsson Jan 05 '17 at 14:58

1 Answers1

0

C++ allows to set up a reference to an element in a newly allocated array:

int *arr = new int[10];
int &elt6 = arr[6];
...
elt6 = 12; // actually sets arr[6]

The point here is that once you have defined your reference, you use it exactly like a normal variable sharing the memory of the array. But at some moment, you have to use pointer arithmetics to tell the compiler what array element you want to reference.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • `int arr = new int[10];` wouldn't compile. – Igor Tandetnik Jan 02 '17 at 14:33
  • 2
    Now `int &elt6 = arr +6;` doesn't compile. – Igor Tandetnik Jan 02 '17 at 14:37
  • So the only way is to write two programs, one of which is modifying the other after the user has specified the number of elements to allocate? – Måns Nilsson Jan 02 '17 at 14:39
  • @MånsNilsson: I'm rather tired at the beginning of year (originally two mistakes on 3 lines...) but really cannot understand your comment. The snippet consists of 2 lines in one single program – Serge Ballesta Jan 02 '17 at 14:43
  • @MånsNilsson You keep talking about dynamically allocated array. What do you imagine happens with a static array? Suppose you have `int arr[10]; arr[6] = 42;`. Do you believe `arr[6]` somehow magically avoids pointer arithmetic? – Igor Tandetnik Jan 02 '17 at 14:44
  • @IgorTandetnik You could then just divide it up to 10 variables: `arr1 arr2 .... arr10`. Then you just make a switch with 10 cases. But switches aren't dynamically expandable. – Måns Nilsson Jan 02 '17 at 15:06
  • @MånsNilsson With 10 elements, perhaps. With 20, it would get cumbersome. With 100, it's bordering on insane. Would you really define a hundred variables, `arr1` through `arr100`, rather than a single array? And then, when you need to assign a value to each, you would write 100 separate assignment statements? I shudder to think what you'd do if you ever need to sort their values in ascending order. And how do you access `k`th element, with `k` entered by the user - with a 100-case `switch` statement? Do you really believe such a `switch` is faster than pointer arithmetic? – Igor Tandetnik Jan 02 '17 at 15:18
  • @IgorTandetnik That's how I memorize things when I study at school. – Måns Nilsson Jan 02 '17 at 15:20
  • 1
    @MånsNilsson For learning purposes, I'd like you to attempt to write a program that prompts the user to enter 10 integers (I'm not even asking for 100, or a dynamically determined number), then prints them back sorted in ascending order, smallest to largest - all without using an array. I think this exercise might prove illuminating. – Igor Tandetnik Jan 02 '17 at 15:23
  • @IgorTandetnik It wouldn't be slower. Faster and more code. – Måns Nilsson Jan 02 '17 at 15:30
  • @MånsNilsson: Pointer arithmetics is much faster than tests and branches. I really urge you to do the little program proposed by Igor. As a human being, it is much simpler to choose to go left or right that adding two 9 digit numbers, but for the processor, it is just the opposite – Serge Ballesta Jan 02 '17 at 15:36
  • @MånsNilsson "More code" is an understatement. It would be exponentially more code. Do try it - I wonder if you could finish it without losing patience (or making any mistakes; it would be "fun" to debug). As to faster - I have my doubts. It would have lots and lots and lots of `if` statements, and branches are much more expensive for modern CPUs than arithmetic. – Igor Tandetnik Jan 02 '17 at 15:38
  • Well, I am assuming bubble sort in my analysis and attempt. I don't know how the end result would be using other sorting algorithms, though. – Måns Nilsson Jan 02 '17 at 16:43