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. C / C++ / MFC
  4. problem with new and pointers

problem with new and pointers

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 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.
  • V Offline
    V Offline
    V_shr
    wrote on last edited by
    #1

    hello I want to create a 2D 170*140 arrey with new . I wrote :

    float **safhe;
    *safhe=new float[170];
    for(int i=0;i<170;i++)
    safhe[i]=new float[140];

    but I dont know what is wrong thank you

    S L 2 Replies Last reply
    0
    • V V_shr

      hello I want to create a 2D 170*140 arrey with new . I wrote :

      float **safhe;
      *safhe=new float[170];
      for(int i=0;i<170;i++)
      safhe[i]=new float[140];

      but I dont know what is wrong thank you

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #2

      V_shr wrote:

      *safhe=new float[170];

      You're dereferencing a pointer that you haven't assigned anything to yet. This is never what you want.

      float **safhe;
      safhe=new float[170];
      for(int i=0;i<170;i++)
      safhe[i]=new float[140];

      See the difference? Both *safhe and safhe[i] dereference a pointer, with safhe[i] effectively adding i * sizeof(float) to the address before dereferencing.

      ---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

      V 1 Reply Last reply
      0
      • V V_shr

        hello I want to create a 2D 170*140 arrey with new . I wrote :

        float **safhe;
        *safhe=new float[170];
        for(int i=0;i<170;i++)
        safhe[i]=new float[140];

        but I dont know what is wrong thank you

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Here is an easy way to 'new' a 2D array of ints. TCHAR szBuff[32]; const size_t Size = 10; int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size]; for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { *TwoDimensional[i][y] = y; } } for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { _stprintf(szBuff, _T("row %d col %d"), i, *TwoDimensional[i][y]); MessageBox(szBuff, _T("TwoDimension"), MB_OK); } } delete[] TwoDimensional;

        L 1 Reply Last reply
        0
        • S Shog9 0

          V_shr wrote:

          *safhe=new float[170];

          You're dereferencing a pointer that you haven't assigned anything to yet. This is never what you want.

          float **safhe;
          safhe=new float[170];
          for(int i=0;i<170;i++)
          safhe[i]=new float[140];

          See the difference? Both *safhe and safhe[i] dereference a pointer, with safhe[i] effectively adding i * sizeof(float) to the address before dereferencing.

          ---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

          V Offline
          V Offline
          V_shr
          wrote on last edited by
          #4

          hello thank you for your help BUT when I change that to "safhe=new float[170]" this error apears : cannot convert 'float *' to 'float * *' ???

          S 1 Reply Last reply
          0
          • V V_shr

            hello thank you for your help BUT when I change that to "safhe=new float[170]" this error apears : cannot convert 'float *' to 'float * *' ???

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #5

            Sorry, missed that bit. You don't want to allocate an array of 170 floats, you want 170 float*. So, do something like: safhe = new float*[170];

            ---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

            V 1 Reply Last reply
            0
            • S Shog9 0

              Sorry, missed that bit. You don't want to allocate an array of 170 floats, you want 170 float*. So, do something like: safhe = new float*[170];

              ---- Scripts i’ve known... CPhog 1.0.0.0 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

              V Offline
              V Offline
              V_shr
              wrote on last edited by
              #6

              Oh , OfCource thanks

              1 Reply Last reply
              0
              • L Lost User

                Here is an easy way to 'new' a 2D array of ints. TCHAR szBuff[32]; const size_t Size = 10; int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size]; for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { *TwoDimensional[i][y] = y; } } for(int i =0; i < Size;i++) { for (int y = 0; y < Size;y++) { _stprintf(szBuff, _T("row %d col %d"), i, *TwoDimensional[i][y]); MessageBox(szBuff, _T("TwoDimension"), MB_OK); } } delete[] TwoDimensional;

                L Offline
                L Offline
                Laxman Auti
                wrote on last edited by
                #7

                int (*TwoDimensional)[Size][Size] = new int[Size][Size][Size]; Is it 2D or 3D??:confused: Knock out 't' from can't, You can if you think you can :cool:

                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