Thread: delete part of allocated array

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

    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
    28,413
    Why don't you use a std::vector?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    you cannot call delete on pointer that was not allocated by new
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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,694
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    5,108
    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
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is a checkbox on the advanced reply form to disable smilies.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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,694
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 06:06 PM
  4. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  5. Replies: 5
    Last Post: 12-05-2002, 02:27 AM