0

I'm sure it's something small but I keep getting an initialize error about how I keep trying to use it before it's initialized.

#include <iostream>
using namespace std;
int main()
{
    int* ordered;

    ordered[0] = 5;
    cout << ordered[0];
    return 0;
}

Bonus question, can I use *ordered to access the beginning address and loop through the array using *ordered++?

1
  • You have not created an dynamic integer array anywhere. All you have created is a pointer to int. Commented Feb 8, 2013 at 8:46

2 Answers 2

5
int* ordered;
ordered[0] = 5;

ordered is an uninitialized pointer. It points to any random address. Dereferncing such a pointer results in Undefined behavior and will most likely crash your program.
To be able to do something meaningful with this pointer it needs to point to some valid memory region. You can do so with:

int *ordered = new[x];

Now, ordered points to a memory region big enough to hold x integers. But, you have to remember to deallocate the memory after usage:

delete []ordered;       

In C++, you are much better off using std::vector instead of a dynamically allocated array because you do not have to the manual memory management that comes with using new []. Simply said, it is difficult to go wrong with std::vector.

Sign up to request clarification or add additional context in comments.

3 Comments

@Howdy_McGee: They do not. You can only dereference a pointer if it points to a valid object. Unless all you get is undefined behavior. And an UB means that program can show any behavior but the behavior is not guaranteed, So your program may work sometimes but it is not guaranteed to do so always.
I'm unsure I understand what you mean but I know if I use char* asdf = "asdf" I can access each letter using an index asdf[0]= a.
@Howdy_McGee: That is still Undefined Behavior. You have a pointer asdf pointing to a string literal, the string literal "asdf" is placed in read only memory and you cannot modify the contents of this string literal. But note that the pointer does point to a valid (read only)memory location in this case.Good read: What is the difference between char a[] = “string”; and char *p = “string”;
4

The problem is there's no memory associated with ordered. You have some options:

  • Assign some memory to ordered using new[]
  • Use a std::vector<int> instead

If you use the vector you can allocate memory right at the beginning or use its push_back method which will cause it to grow as needed.

4 Comments

Can I not declare other data-type arrays like character arrays?
@Howdy_McGee You can but vector is preferred unless you have a clear and defensible reason not to use it.
can't use vectors - because of the course im taking won't allow it :/
@Howdy_McGee Do they also disallow std::string in favor of character arrays ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.