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. New'ing a pointer to an array [Solved]

New'ing a pointer to an array [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelptutorial
8 Posts 2 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.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    Without using typedef, what is the syntax for dynamically allocating a pointer to an array? I tried the following, but can't get it to work:

    long (*const val)[16] = new long()[16]; // C2440: Cannot convert from long* to long(*const)[16]
    long (*const val)[16] = new long[16](); // This creates a (long*) with 16 elements

    This is a question for the sake of curiosity, as I am actually using a typedef in the code to make it more clear. Thanks, EDIT: I actually can't figure out how to do this even when using typedefs :(. I have tried the following:

    typedef long *myType[16]; // myType is an array of 16 pointers to long
    myType *val = new myType(); // C2440: Cannot convert from long** to myType(*)

    Any help with or without typedef's is appreciated. Thanks,

    Sounds like somebody's got a case of the Mondays -Jeff

    modified on Monday, November 29, 2010 3:27 PM

    _ 1 Reply Last reply
    0
    • S Skippums

      Without using typedef, what is the syntax for dynamically allocating a pointer to an array? I tried the following, but can't get it to work:

      long (*const val)[16] = new long()[16]; // C2440: Cannot convert from long* to long(*const)[16]
      long (*const val)[16] = new long[16](); // This creates a (long*) with 16 elements

      This is a question for the sake of curiosity, as I am actually using a typedef in the code to make it more clear. Thanks, EDIT: I actually can't figure out how to do this even when using typedefs :(. I have tried the following:

      typedef long *myType[16]; // myType is an array of 16 pointers to long
      myType *val = new myType(); // C2440: Cannot convert from long** to myType(*)

      Any help with or without typedef's is appreciated. Thanks,

      Sounds like somebody's got a case of the Mondays -Jeff

      modified on Monday, November 29, 2010 3:27 PM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      A pointer to an array is simple -

      long* ptr = new long[16];

      What you've done above is to create an array of pointers. So you actually have 16 pointers.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++)

      Polymorphism in C

      S 1 Reply Last reply
      0
      • _ _Superman_

        A pointer to an array is simple -

        long* ptr = new long[16];

        What you've done above is to create an array of pointers. So you actually have 16 pointers.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        I simplified my example. I am actually trying to dynamically create a 2D array, where one dimension is known (the unknown dimension needs to be consecutive in memory), hence I have:

        long *var[16];

        However, I would like to create a value for "var" in a function, and return it. Hence I have:

        long *(&myFunc())[16] {
        long *(*rval)[16] = new ?????;
        ...
        return *rval;
        }

        Then I declare my variable as a reference and delete it at the end of the code

        long *(&var)[16] = myFunc();
        ...
        delete &var;

        Is there some better way to go about this? I could just make it doubly-dynamic, but feel like I should retain known dimensions for the sake of readability. Any ideas? Thanks,

        Sounds like somebody's got a case of the Mondays -Jeff

        _ 1 Reply Last reply
        0
        • S Skippums

          I simplified my example. I am actually trying to dynamically create a 2D array, where one dimension is known (the unknown dimension needs to be consecutive in memory), hence I have:

          long *var[16];

          However, I would like to create a value for "var" in a function, and return it. Hence I have:

          long *(&myFunc())[16] {
          long *(*rval)[16] = new ?????;
          ...
          return *rval;
          }

          Then I declare my variable as a reference and delete it at the end of the code

          long *(&var)[16] = myFunc();
          ...
          delete &var;

          Is there some better way to go about this? I could just make it doubly-dynamic, but feel like I should retain known dimensions for the sake of readability. Any ideas? Thanks,

          Sounds like somebody's got a case of the Mondays -Jeff

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Here is how it can be done, but its real ugly -

          #define FIXED_SIZE 16

          void allocate(long (**ptr)[FIXED_SIZE])
          {
          *ptr = new long[10][FIXED_SIZE];
          }

          int _tmain(int argc, _TCHAR* argv[])
          {
          long (*ptr)[FIXED_SIZE];

          allocate(&ptr);
          
          delete \[\] ptr;
          
          return 0;
          

          }

          The clean and recommended way is to use a standard container - std::vector<std::vector<long>> longArr;

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          modified on Monday, November 29, 2010 2:46 PM

          S 1 Reply Last reply
          0
          • _ _Superman_

            Here is how it can be done, but its real ugly -

            #define FIXED_SIZE 16

            void allocate(long (**ptr)[FIXED_SIZE])
            {
            *ptr = new long[10][FIXED_SIZE];
            }

            int _tmain(int argc, _TCHAR* argv[])
            {
            long (*ptr)[FIXED_SIZE];

            allocate(&ptr);
            
            delete \[\] ptr;
            
            return 0;
            

            }

            The clean and recommended way is to use a standard container - std::vector<std::vector<long>> longArr;

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            modified on Monday, November 29, 2010 2:46 PM

            S Offline
            S Offline
            Skippums
            wrote on last edited by
            #5

            I guess the answer is, "you can't allocate a single pointer to an array without the new[] operator" (with brackets). So, to allocate a single pointer to an array you have to do:

            long (*val)[16] = new long[1][16];
            ...
            delete[] val;

            Thanks,

            Sounds like somebody's got a case of the Mondays -Jeff

            _ 1 Reply Last reply
            0
            • S Skippums

              I guess the answer is, "you can't allocate a single pointer to an array without the new[] operator" (with brackets). So, to allocate a single pointer to an array you have to do:

              long (*val)[16] = new long[1][16];
              ...
              delete[] val;

              Thanks,

              Sounds like somebody's got a case of the Mondays -Jeff

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              This is a single pointer to an array -

              long* ptr = new long[16];
              delete [] ptr;

              Now I'm not sure what you're looking for.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++)

              Polymorphism in C

              S 1 Reply Last reply
              0
              • _ _Superman_

                This is a single pointer to an array -

                long* ptr = new long[16];
                delete [] ptr;

                Now I'm not sure what you're looking for.

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++)

                Polymorphism in C

                S Offline
                S Offline
                Skippums
                wrote on last edited by
                #7

                Your last post solved it... I just can't allocate a SINGLE pointer to an array of pointers without dynamically allocating an array of 1 pointer to the array. For example:

                // Allocate a single pointer to an array of 16 pointers to long
                // Can't be done without using operator new[]
                long *(*val)[16] = new ???;
                ...
                delete val;

                There is nothing I can put in the "???" area to make the above code valid, because there is no valid syntax to dynamically create a single pointer to array using operator new(). I must do the following instead:

                // Allocate a single pointer to an array of 16 pointers to long
                long *(*val)[16] = new long*[1][16];
                ...
                delete[] val;

                In short, you have answered my question. Thanks,

                Sounds like somebody's got a case of the Mondays -Jeff

                _ 1 Reply Last reply
                0
                • S Skippums

                  Your last post solved it... I just can't allocate a SINGLE pointer to an array of pointers without dynamically allocating an array of 1 pointer to the array. For example:

                  // Allocate a single pointer to an array of 16 pointers to long
                  // Can't be done without using operator new[]
                  long *(*val)[16] = new ???;
                  ...
                  delete val;

                  There is nothing I can put in the "???" area to make the above code valid, because there is no valid syntax to dynamically create a single pointer to array using operator new(). I must do the following instead:

                  // Allocate a single pointer to an array of 16 pointers to long
                  long *(*val)[16] = new long*[1][16];
                  ...
                  delete[] val;

                  In short, you have answered my question. Thanks,

                  Sounds like somebody's got a case of the Mondays -Jeff

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  :thumbsup:

                  «_Superman_»  _I love work. It gives me something to do between weekends.

                  _Microsoft MVP (Visual C++)

                  Polymorphism in C

                  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