1

I was messing around trying to understand pointers and the operator "new"

and I ended up getting even more confused on what these 2 codes should result to, which is other but its not, so I wanted to understand what happened here. thanks in advance.

#include <iostream>
using namespace std;
int main()
{
    int * p = new int(50);
    p[1] = 33;
    cout  << *p << endl;
}

Output: 50

and when I tried this

#include <iostream>
using namespace std;
int main()
{
    int * p = new int[50];
    p[1] = 33;
    cout  << *p << endl;
}

Output: -842150451

I was wondering about the meaning of each result.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Wall-ED
  • 51
  • 5
  • 5
    please post the code here, don't just link to it. and please use text, not pictures. – Cheers and hth. - Alf Mar 21 '14 at 17:54
  • You are probably trying to create an array, but you use () instead of [], so you get one integer initialized to 50 – sp2danny Mar 21 '14 at 18:01
  • @user3447258 Welcome to SO. If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved, and it gives the person that helps you credit for the assist. See [here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) for a full explanation. – wonko realtime Apr 07 '14 at 16:27

4 Answers4

3

In the first one, you created dynamically a single int with a value of 50. When you try to assign the value 33, you actually assign it in memory that is not yours. It is undefined behaviour. But when you print it, you print the original value you made, which was 50.

In the second one, you created dynamically an array of 50 int. You've then specified the second value of in the array should be 33.* So when you print the value with cout << *p << endl;, you end up printing only the first value, which is undefined. Try it again, you'll probably get another value.

*Edit: as pointed in the comments, I should have been more explicit about this. An array starts at 0. So if you want to access the first value p[0] would do it.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Rosme
  • 1,049
  • 9
  • 23
  • Good answer, but you might want to be explicit about the fact that he thinks p[1] is the first element. Otherwise this might just be confusing. – Cogwheel Mar 21 '14 at 18:09
  • I think you may have the words "first" and "second" swapped here. – Dan Mar 21 '14 at 18:17
3

In the first case

int * p = new int(50);  // allocates 1 int on heap, initialized to value of 50
p[ 1] = 33;             // p gives you the address of integer, 
                        // p[1] moves the p pointer forward and accessing 
                        // the pointed object results in undefined behavior,
                        // this means that at this moment anything can happen
                        // exception, crash, home war, nothing, everything,
                        // printing garbage value as well

In the second case:

int* p = new int[50];   // allocates 50 ints on heap, uninitialized
p[ 1] = 17;             // initializes element with index 1 to value of 17
std::cout << *p;        // p gives you the address of first element, with index 0
                        // which is still uninitialized, thus prints garbages

You should use

int* p = new int[50]();

to value-initialize ints to 0.

4pie0
  • 29,204
  • 9
  • 82
  • 118
0

In the first case you're creating an array of 50 ints, assigning a value to the SECOND int in the array, and then printing the first element in the array. Array indices start at 0, so when you dereference the pointer in your cout statement, it's printing whatever happened to be in memory at index 0.

In the second case you're creating a single integer, and initializing it with the value 50. When you dereference the pointer in the print statement, you're getting the integer you just created. The p[1] = 33 may or may not cause an error as your accessing unassigned memory.

R M
  • 96
  • 1
  • 4
0
int* p = new int[50];
  • allocates an array of int on the heap with 50 uninitialized elements, ranging from index 0 to 49. Setting p[1] to 33 doesn't change p[0] which is what you're printing with "cout << *p".

The value -842150451 (0xCDCDCDCD in hex) is a magic number "Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory", see this question on SO.

int* p = new int(50);
  • allocates exactly one int on the heap and initializes it with the value 50, so setting p[1] afterwards should result in undefined behavior since you didn't allocate that memory where p[1] is referring to.

I'd recommend to use the Visual Studio Memory Windows to see what happens to the memory you're allocating while stepping through your code.

Community
  • 1
  • 1
wonko realtime
  • 545
  • 9
  • 26