Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. int **p = (int**)new int(5); Confusing !!!

int **p = (int**)new int(5); Confusing !!!

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit

    Richard Andrew x64R Z 2 Replies Last reply
    0
    • A Amrit Agr

      Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Amrit Agr wrote:

      So why I need to do in case of double pointer??

      Who said you do? This ought to work just fine: ``` int **p = new (int*); *p = new int; **p = 5; cout << **p; delete *p; delete p; ```

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • A Amrit Agr

        Hey Guys, I am trying to execute the following chunk of code. int **p = (int**)new int(5); cout << *p; I am getting output 00000005 As per my understanding , we don't need to typecast return value from new, as it automatically does. Like in the following statement int *ap = new int(20); We don't need to do that, So why I need to do in case of double pointer?? Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005 Please someone help me. Thanks Amrit

        Z Offline
        Z Offline
        Zabir Al Nazi Nabil
        wrote on last edited by
        #3

        here, p is a pointer to int*.that means p can point to an int pointer and that pointer will point an int type of data. when you do this , (int**) new int (5) ; you are actually forcing (casting) p to point to an int pointer (memory address) with value 5 ..(It actually should not be done because you don't know anything about memory address 0x5 )... however and as you made p to store something(a memory address with value 5) you also allocated memory for that purpose ...... and the typecasting was necessary as first case is very different than the second case.... in the second case --- ap is an int pointer and with new command you allocated 4 bytes of memory for ap .. and ap is now pointing to that memory location and as you passed 20 in the constructor so now that memory address is storing 20.... But, p is not an int pointer ..it's a pointer to int pointer ...so it should be passed a valid memory address in the constructor call .. not an int(what you did -- by passing 5)..that's why you had to cast... and the answer to your last question is ... p is just a pointer (it maybe a pointer to another pointer but it still is a pointer)..and when we want to know what value a pointer is pointing to we put * (deference op.) before that pointer (only once)! Hope,you got that :-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups