Like Tree3Likes

delete part of allocated array

This is a discussion on delete part of allocated array within the C++ Programming forums, part of the General Programming Boards category; The situation is like this: I know what the max number of the dimension of my data structure will be. ...

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675

    delete part of allocated array

    The situation is like this:
    I know what the max number of the dimension of my data structure will be. But in the case, that the actual space needed is less than max, I would like to delete the unused space.

    So, I was thinking something like this, but it won't work
    Code:
    #include <iostream>
    using namespace std;
    
    class foo{
        public:
            foo(){x=0;y=0;z=0;d=0;}
        double x;
        double y;
        double z;
        double d;
    };
    
    int main()
    {
        foo* p;
        p = new foo[8];
        for(int i = 0 ; i < 8 ; i++)
            cout << i << " " << p[i].x << " " << p[i].y << " " <<  p[i].z << " " << p[i].d << endl;
        foo* pp = &p[5];
        delete [] pp;
        
        return 0;
    }
    Of course, I made this code only to test this idea.

    Any approaches/ new ideas are welcome.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,628
    Why don't you use a std::vector?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use vector and resize

    you cannot call delete on pointer that was not allocated by new
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Maybe because I wrote C lately and did not have vector in mind. That will do the trick. But, is vector behaving as an array? What I want to say, does it store its data in adjacent memory cells?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by std10093 View Post
    I know what the max number of the dimension of my data structure will be. But in the case, that the actual space needed is less than max, I would like to delete the unused space.
    Not possible.
    Memory is allocated and deallocated in chunks.
    You have to pass the pointer that new gave you to delete, not some other pointer.
    Why don't you just make the array the proper size in the first place?
    If you need to resize the thing downwards, you'll have to reallocate a new space and copy what you need there, and delete the original space.
    There's really not much point in downsizing the array.
    In any case, the OS is unlikely to actually deallocate the memory from your process.
    If the memory is never used it'll be paged out and it'll almost be like it doesn't exist.

    And, yes, a vector store elements contiguously.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    The standard guarantees that vector's underlying memory is contiguous in most cases, except if the contained type is bool.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Then, it is obvious I should go for the vector. But, instead of resizing, what would be best?
    Set a vector with the max dimension and then resize once.
    Set a vector with no dimension and just push_back data.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    I like to set a max dimension if I know it and then resize, just because I can. It's not a bad thing to depend on vector's automatic reallocation, but if you know specific factors, why not. Allocating all at once can be memory efficient and cause less fragmentation.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,227
    Then, it is obvious I should go for the vector. But, instead of resizing, what would be best?
    Set a vector with the max dimension and then resize once.
    Set a vector with no dimension and just push_back data.
    O_o

    I agree with whiteflags, because you should always exploit as much information as you have available, but you should also be aware that `std::vector<???>:ush_back' is amortized constant time complexity.

    Basically, the internal growth during a `std::vector<???>:ush_back' operation is such that most `std::vector<???>:ush_back' operations do not allocate memory or copy elements around.

    [Edit]
    O_o

    Is it weird that I hate emoticons?
    [/Edit]

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Try using [noparse]std::vector<???>::push_back[/noparse].
    Don't let them smilies outsmart you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    There is a checkbox on the advanced reply form to disable smilies.
    phantomotap likes this.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,227
    O_o

    I always forget about "NOPARSE" and just didn't know about that the advanced editor had such an option.

    Thank you both.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Or you can just write it like:
    std::vector<???>:[I][/I]push_back
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Both methods will work. push_back already did.
    I want to know the pros and cons. What will be more efficient?

    Also, when we write
    Code:
    myvector.at(i)
    we accessing immediately the i-th element, like in an array, or do we have to traverse the vector, like we would do with a linked list?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,628
    Quote Originally Posted by std10093
    I want to know the pros and cons. What will be more efficient?
    Test using representative sample data and find out.

    Quote Originally Posted by std10093
    we accessing immediately the i-th element, like in an array, or do we have to traverse the vector, like we would do with a linked list?
    A std::vector is basically a dynamic array container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocated memory is reset later part of the execution
    By sanddune008 in forum C Programming
    Replies: 3
    Last Post: 09-04-2012, 06:29 AM
  2. Replies: 21
    Last Post: 06-26-2012, 03:33 PM
  3. How to delete part of an array?
    By Ash1981 in forum C Programming
    Replies: 11
    Last Post: 01-01-2006, 05:06 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 03:57 PM
  5. Replies: 5
    Last Post: 12-05-2002, 01:27 AM

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