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. ATL / WTL / STL
  4. Pointers to vectors?

Pointers to vectors?

Scheduled Pinned Locked Moved ATL / WTL / STL
questiongraphicsdockerdata-structureshelp
10 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.
  • K Offline
    K Offline
    knapak
    wrote on last edited by
    #1

    Hi guys Before my question I remind you I'm an amateur... so be kind in your response :) Why is it that the following code works fine if I use a regular array and not a vector container... :mad: How do I make the function return a pointer to the beginning of the vector and use it on the call from another function...??? :confused: Thanks a lot for all your help, you guys are great! :cool: double *TakeThisVector::CreateVector(double such) { vector A_vector; double *pt_tovector = 0; int max=2; for(int i=0; i<= max; i++) { A_vector.push_back(such); such++; } pt_tovector = A_vector; return pt_tovector; } void GimmeTheVector::ShowVector() { TakeThisVector TTV; double A0, A1; double *Recover = 0; Recover = TTV.CreateVector(Lat, Lon); A0 = *Recover++; A1 = *Recover; }

    S 1 Reply Last reply
    0
    • K knapak

      Hi guys Before my question I remind you I'm an amateur... so be kind in your response :) Why is it that the following code works fine if I use a regular array and not a vector container... :mad: How do I make the function return a pointer to the beginning of the vector and use it on the call from another function...??? :confused: Thanks a lot for all your help, you guys are great! :cool: double *TakeThisVector::CreateVector(double such) { vector A_vector; double *pt_tovector = 0; int max=2; for(int i=0; i<= max; i++) { A_vector.push_back(such); such++; } pt_tovector = A_vector; return pt_tovector; } void GimmeTheVector::ShowVector() { TakeThisVector TTV; double A0, A1; double *Recover = 0; Recover = TTV.CreateVector(Lat, Lon); A0 = *Recover++; A1 = *Recover; }

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      You would return &A_vector[0]...except that A_vector doesn't exist after CreateVector exits. You probably want to return the vector rather than a pointer to its first element. Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

      K 1 Reply Last reply
      0
      • S Stuart Dootson

        You would return &A_vector[0]...except that A_vector doesn't exist after CreateVector exits. You probably want to return the vector rather than a pointer to its first element. Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

        K Offline
        K Offline
        knapak
        wrote on last edited by
        #3

        uh... pt_tovector IS &A_vector[0]... and the code works if instead of using a vector container I use an array. The error message is: error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::vector >' (or there is no acceptable conversion) which means that I can't assign &A_vector[0] to pt_tovector if A_vector is a container... ...or I'm more confused than I thought X| Thanks!!!

        M 1 Reply Last reply
        0
        • K knapak

          uh... pt_tovector IS &A_vector[0]... and the code works if instead of using a vector container I use an array. The error message is: error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::vector >' (or there is no acceptable conversion) which means that I can't assign &A_vector[0] to pt_tovector if A_vector is a container... ...or I'm more confused than I thought X| Thanks!!!

          M Offline
          M Offline
          markkuk
          wrote on last edited by
          #4

          knapak wrote: uh... pt_tovector IS &A_vector[0] No, the code you posted has pt_tovector = A_vector;. For ordinary arrays that would be same as pt_tovector = &A_vector[0]; but not for vectors.

          K 1 Reply Last reply
          0
          • M markkuk

            knapak wrote: uh... pt_tovector IS &A_vector[0] No, the code you posted has pt_tovector = A_vector;. For ordinary arrays that would be same as pt_tovector = &A_vector[0]; but not for vectors.

            K Offline
            K Offline
            knapak
            wrote on last edited by
            #5

            :omg: OHHHHHHHH .... So.... how do I solve the problem? The first comments suggest to return the vector itself instead of a pointer to its first element... EXCUSE MY NAIVETE, but I always thought that returning a pointer to the first element of an array was THE way to return an array... Of course now we are talking about Vectors... :sigh: Thank you!

            K M 2 Replies Last reply
            0
            • K knapak

              :omg: OHHHHHHHH .... So.... how do I solve the problem? The first comments suggest to return the vector itself instead of a pointer to its first element... EXCUSE MY NAIVETE, but I always thought that returning a pointer to the first element of an array was THE way to return an array... Of course now we are talking about Vectors... :sigh: Thank you!

              K Offline
              K Offline
              knapak
              wrote on last edited by
              #6

              OK... so I figured that if I make an iterator be A_vector.begin() and then pass the iterator to a pointer... should do the trick... right? WRONG! :mad: That works within the scope of the function that contains the vector, but apparently the container dissapears as the program exits the function call (just as the first folk said). That obviously is different to what happens when using regular arrays... and still have not found the way to do what I need... :~ Many thanks to all of you.

              S 1 Reply Last reply
              0
              • K knapak

                OK... so I figured that if I make an iterator be A_vector.begin() and then pass the iterator to a pointer... should do the trick... right? WRONG! :mad: That works within the scope of the function that contains the vector, but apparently the container dissapears as the program exits the function call (just as the first folk said). That obviously is different to what happens when using regular arrays... and still have not found the way to do what I need... :~ Many thanks to all of you.

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                As I said in my first reply, return the vector object - otherwise the storage associated with the vector is deallocated when the function exits. If you did a similar thing with arrays, like below, you'd have the same problem.

                double *CreateArray(double such)
                {
                   double pt_tovector[2];
                   pt_tovector[0] = such;
                   pt_tovector[1] = such;
                   return pt_tovector;
                }
                

                Of course, if you allocated the array on the heap like the example below, you *CAN* return the array pointer. The thing is, the vector encapsulates and manages a pointer to heap storage - if you think of the vector as a pointer to an array in heap storage with other useful properties (like knowing how big the array is - a pointer won't tell you that), that may help.

                double *CreateArray(double such)
                {
                   double* pt_tovector = new double[2];
                   pt_tovector[0] = such;
                   pt_tovector[1] = such;
                   return pt_tovector;
                }
                

                BTW - iterators in VC7.1 are not pointers. They were in VC6, but there is no guarantee that iterators are pointers. Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                K 1 Reply Last reply
                0
                • K knapak

                  :omg: OHHHHHHHH .... So.... how do I solve the problem? The first comments suggest to return the vector itself instead of a pointer to its first element... EXCUSE MY NAIVETE, but I always thought that returning a pointer to the first element of an array was THE way to return an array... Of course now we are talking about Vectors... :sigh: Thank you!

                  M Offline
                  M Offline
                  markkuk
                  wrote on last edited by
                  #8

                  Yes, you should return a pointer to the first element of the vector, but your code doesn't do that! You must actually write pt_tovector = &A_vector[0] in your program to return a pointer to the first element of the STL vector A_vector. Additionally, the vector can't be local to the function.

                  K 1 Reply Last reply
                  0
                  • S Stuart Dootson

                    As I said in my first reply, return the vector object - otherwise the storage associated with the vector is deallocated when the function exits. If you did a similar thing with arrays, like below, you'd have the same problem.

                    double *CreateArray(double such)
                    {
                       double pt_tovector[2];
                       pt_tovector[0] = such;
                       pt_tovector[1] = such;
                       return pt_tovector;
                    }
                    

                    Of course, if you allocated the array on the heap like the example below, you *CAN* return the array pointer. The thing is, the vector encapsulates and manages a pointer to heap storage - if you think of the vector as a pointer to an array in heap storage with other useful properties (like knowing how big the array is - a pointer won't tell you that), that may help.

                    double *CreateArray(double such)
                    {
                       double* pt_tovector = new double[2];
                       pt_tovector[0] = such;
                       pt_tovector[1] = such;
                       return pt_tovector;
                    }
                    

                    BTW - iterators in VC7.1 are not pointers. They were in VC6, but there is no guarantee that iterators are pointers. Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

                    K Offline
                    K Offline
                    knapak
                    wrote on last edited by
                    #9

                    Hello everyone First and foremost, THANKS TO ALL OF YOU WHO PROVIDED AN ANSWER/OPINION. I'm learning a lot. Now, as an academic curiosity, I think that if I make a vector container available outside the function (by making it global), then it should be equivalent to using: double* pt_tovector = new double[2]; without deallocating the memory at the end of the function. Memory will remain caught until the program exits which is something I need to avoid since my code is big. In which case, since for this particular example I only need to retrieve two values, I rather make the whole function void with the two values of interest private and then include a couple of tiny accessor functions, one for each value... is this solution too ugly a programming style??? ;P Many thanks again!!!

                    1 Reply Last reply
                    0
                    • M markkuk

                      Yes, you should return a pointer to the first element of the vector, but your code doesn't do that! You must actually write pt_tovector = &A_vector[0] in your program to return a pointer to the first element of the STL vector A_vector. Additionally, the vector can't be local to the function.

                      K Offline
                      K Offline
                      knapak
                      wrote on last edited by
                      #10

                      Thank you!!!

                      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