0

Why the following guess is wrong ? (a software engineering company manager told me that it's almost correct but I don't understand why and I can't search for the answer in the Internet..)

int* ptr = new int;      // Sorry I mistyped before

My claim :

  1. left part (ptr) is of static memory allocation.
  2. right part (new int) is of dynamic memory allocation.

// new edited : 1 Jan 2015 17:39 (UTC +08:00)
what I am thinking is, It moves the pointer of stack down(or up?) to free a space for ptr. And find a empty space for a new int. And then store the address of this new int to ptr.

Hon Lun Chan
  • 148
  • 2
  • 16
  • 2
    you must read about pointers, here is a good videos to understand pointers. https://www.youtube.com/playlist?list=PL2_aWCzGMAwLZp6LMUKI3cc7pgGsasm2_ – houssam Dec 31 '14 at 14:53
  • Its wrong in every level... first of all, why do you think dynamic allocated memory can be assigned to static (which is actually not static) ? – SwiftMango Dec 31 '14 at 15:52
  • Your left part is not a static memory allocation if it occurs within a function block. – user3344003 Dec 31 '14 at 20:15
  • What does that code even mean? The left side says `ptr` is an `int`, but then the thing on the right side of the `=` is a pointer. – David Schwartz Dec 31 '14 at 22:40
  • to David, sorry I wanted to type int* ptr = new int; – Hon Lun Chan Jan 01 '15 at 09:31
  • _ptr_ itself is an *automatic* variable (address on the stack), while the int* type address it points to is allocated by the C runtime in the heap or similar memory structure (dynamic memory). In C/C++ jargon , *static refers to other kind of allocation*. Your example is stack (automatic) vs. heap allocation (dynamic). – Hernán Sep 19 '16 at 16:48

4 Answers4

3

The left side is an int, the right side is creating an int * (pointer)

You want:

int * ptr = new int;

Or if you want to do static allocation

int num; // = 5; Will initialize num to 5
sedavidw
  • 11,116
  • 13
  • 61
  • 95
0

Your claim is correct.

  1. left part (ptr) is of static memory allocation.

  2. right part (new int) is of dynamic memory allocation.

In other words, the right part return an int *, a (dynamically allocated) pointer to int. You can't assign it to a (statically allocated) variable of int type.

Community
  • 1
  • 1
Igor
  • 26,650
  • 27
  • 89
  • 114
0

I would like to explain some things about your code.

The part

new int

is considered 'dynamic allocation' but actually it allocated in the heap memory.

declaring

int ptr

is not considered 'static allocation' rather it's considered 'automatic' because it is allocated on the stack memory. Note that the heap is a lot bigger than the stack (default stack size per thread in Windows is 1MB), so you won't be able to allocate large arrays on the stack. The heap on the other hand is theoretically has 4GB of memory address space although only 2GB are available to a process, and it is also a virtual memory and not physical of'course.

Now, since you denote ptr as just an integer type (and not a pointer), the compiler will fail because you try to assign a memory address to a non-pointer type. Thus, you need to tell the compiler explicitly that this is your intention, and you tell the compiler by casting the memory allocation (which is just a 32-bit address) to int:

int ptr = (int)new int; //allocate an int on the heap and cast the address to an int value

The thing is that now ptr will hold a 32-bit number which is the start address of the memory allocation in the heap. You can't do much with this ptr now because the compiler treat it as a simple integer and not a pointer, so in order to do something meaningful with it (rather than just save the address in an integer value), you will need to cast it to a pointer and only than use it. For instance, initialize the address with some value (let's say the value is 5):

(*(int*)ptr) = 5; //cast to pointer and dereference it.

As you can see the syntax now becomes 'ugly' and very hard to read/understand. If you do things the 'right' way, you would just write it like this:

int* ptr = new int;

*ptr = 5;  //dereference the pointer

One more thing, about pointer arithmetics: Since your ptr is just an integer number and not a pointer, when you increment it:

int ptr = new int; //Assume ptr got the address 0x010
ptr++;
//Now ptr will hold the value of 0x011 because the value just got incremented by 1.

But if ptr was a real pointer:

int* ptr = new ptr; //Assume ptr got the address 0x010
ptr++;
//Now ptr will hold the value of 0x014 because the size of an int is 4 bytes
//and the pointer now points to the next address after the allocated integer.
AlphaP8
  • 17
  • 2
-3

Because you initaliaze an variable of type 'int' with an rvalue of type 'int *' which is invalid. You should cast the later to 'size_t' and it should work.

int ptr = (size_t)new int;

After that to access the actual value of 'ptr' you should write:

*(int*)ptr

Life example.

AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66