vector and pointers to a newbie

This is a discussion on vector and pointers to a newbie within the C Programming forums, part of the General Programming Boards category; Too much time without C programming (15+ years) so I'm a totally newbie now. My problem, sure it's simply, is ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    vector and pointers to a newbie

    Too much time without C programming (15+ years) so I'm a totally newbie now.

    My problem, sure it's simply, is that I'm storing pointers in a vector but they are modified.

    I've a Point* point; which contains 4 points and I can access with point, point+1...
    I've a vector <Point*> v, and I want to compare each Point* with the stored in the vector and under some conditions to store (a copy) of this point in the vector.

    The code is something like

    for( i = 0; i < total_points; i += 4 ) { //i+=4 because Point* is a pointer with 4 Points...
    Point* rect =next point //each iteration I've checked I got the next and correct point
    if(v.empty()) {
    v.push_back(rect); //empty vector store the first rectangle
    }
    else{
    check(rect,v); //ERROR inside function, rect and v[0] are the SAME
    }
    }

    the problem that each time I call Point* rect =next point, rect changes BUT the point stored in v[0] changes TOO, because I stored the pointer to rect and it has changed, my silly question is, how can store in v pointers to Point* but they dont change in the next iteration when read a new value for rect.


    Best regards.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Erm, vector and push_back is C++, not C.

    Which language are you writing in?


    > //i+=4 because Point* is a pointer with 4 Points...
    This makes almost no sense, and without a struct declaration, it's meaningless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    The main code is C, to store Point* I googled for some code to implement list or vectors and this was the first and simpliest way I found, the compiler is VC so I've no problem using some C++ code, but I didn't know it was a C++ part of code (until now).

    Do I have to ask in the C++ forum then? or can anybody help me.

    Oh, and don't take care the i+=4 part, because I skipped the code to manage the 4 points stored in the Point*, the code works, I've checked, I try to isolate the problem for easy understanding, the problem is that I'm storing in vector a pointer, and in the next iteration I'm reading another structure in the pointer, so when I call a function the structure and the vector store the same data, not as I wanted to do, that vector stored previous values.

    So in iteration 1:
    Point* rect is a pointer to 4 points defining rectangle1
    I insert in v rectangle1

    Iteration 2, rect point to rectangle 2 BUT v now changes and v[0] contains rectangle2 not rectangle1 as I wanted.
    How to do this is the only question I have.

    Best regards.
    Last edited by z80z80; 03-15-2011 at 07:56 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    My guess, since you're pushing pointers, is that everything is pointing at the same object.

    But to be sure, you need to post more code, and don't forget to use [code][/code] tags (see intro threads).

    Try doing this without using any pointers at all. If your struct has 4 points, then make it an array of 4 points.


    > but I didn't know it was a C++ part of code (until now).
    Well that's one of the hazards of being "out of the loop" for 15 years, then bashing in random bits of code that just happen to compile. Making something compile is trivial, compared to making something that will run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Yes Salem, I'm SURE my problem is that everything is pointing at the same object, the pointer in the loop and the one stored in the vector, so my silly question is how to do what I'm trying to do, that is getting a pointer in the loop and storing a COPY, not the same pointer, inside the vector so when I store "rectangle1" and in the next loop, my pointer points to rectangle two, I still got rectangle1 stored inside vector (and not changed to rectangle2).

    Sure it's a stupid question but, how can I do this, can you give me a simple example?


    Best regards.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Point *p = new Point;
    p->members = values;
    v.push_back(p);

    The downside to this is that you have to MANUALLY empty the vector and delete each element yourself. You can't just let the vector go out of scope and let it do all the deallocations for you.
    You could do this IF you stored Points directly though.

    Again, think hard as to why it's still a good idea to be manually managing memory yourself.

    If any of your thoughts are "performance", then I might counter with how much time you've wasted so far trying to get your ad-hoc memory management to work, and how many times you have to run your program to recoup that investment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21