0

I basically have a uint64_t[8][3] I however dont need all the positions in this array .I could create another node structure to dynamically only set proper positions but it would be harder because I rely on the indices of matrix for my particular program. How can I free selectively some indices of the matrix.

For example I don't need uint64_t[4][3] or uint64_t[7][3] for a particular node how do i free this?

SergeyA
  • 61,605
  • 5
  • 78
  • 137
Jon Bovi
  • 53
  • 9
  • 3
    You cannot. It's a small amout of unused space that is not worth worrying about. – R Sahu Nov 29 '18 at 18:31
  • 1
    Rather use a `std::vector>`, with raw arrays that's not possible. – πάντα ῥεῖ Nov 29 '18 at 18:31
  • 1
    One an array is allocated, it's allocated. You have to release all of it or none of it, and in this case you have an automatic allocation. The system releases automatic variables when the go out of scope. You cannot release them early but you may be able to adjust the code to reduce the scope. If you want to use less of an array, add a book-keeping variable to track capacity. – user4581301 Nov 29 '18 at 18:38
  • Thanks everyone. why has everyone commented instead of answering. Now I can't close the question without deleting it. – Jon Bovi Nov 29 '18 at 18:57
  • *why has everyone commented instead of answering* Most likely because writing an answer that's more complete than "You can't. Use a `vector`" would go on for pages, touching on many topics. That's a scope best left to books. Note: You can't actually do this with a `vector` either. `vector` is keeping the memory you're not using and holding a bunch of book-keeping keep track of the fact that it's not being used. – user4581301 Nov 29 '18 at 19:45

3 Answers3

0

You can free the elements in certain indices if they are dynamically allocated using delete(e.g. delete variableName[0][2]), but you can't remove the indices themselves.

k.S
  • 89
  • 9
0

This is not possible or at least not the way you want to it. If you are trying to implement a binary tree-like structure take a look at binary tree this way you have tree structure where you could allocate only what you need dynamically.

if you still need arrays do not use old c style arrays, the c++ committee has been actively updating the standard since 1998 and the modern way of working with arrays is using std::array. If you are working in an evironment where you are working with old C-APIs, write your logic in a modern c++ static library compile it and link against that library in your C-API.

That being said If you are worrying about performance or memory issues, you should reconsider your way of thinking, alot of young developers get caught up in this patterns of thinking and end up writing code that is neither fast nor elegant in terms of design (Its called pessimizing the code). When you write a program first start with readable and simple design, then do profiling and figure out what is slow and should be optimized.

if you really need some of the values in the array to be nulls so you can do crazy stuff with it (such as null checking, which I would highly discourage) you should wrap you integers in another class :

class Integer
{
public:
    explicit Integer(int value)
    {
        value_ = value;
    };
    Integer &operator=(Integer other)
    {
        value_ = other.value_;
    };

private:
    int value_;
};

class test
{
public:
    test()
    {
        Integer four(4);

        std::array<std::unique_ptr<Integer>, 2> arr = { std::make_unique<Integer>(four), nullptr};
    };
};

NOTE I: What I wrote above is only a trick, so you know everything is possible in c++, however the complexity of what I just wrote to achieve what you wanted, should convince you that its not a good approach for your simple case.

NOTE II: The code is not tested, dont use this as is in production code.

Armin
  • 134
  • 3
  • 16
-2

You can't free it, it's cpp that's how it works...

Akshat Tamrakar
  • 2,193
  • 2
  • 13
  • 21