best structure for this struct to save memory?

I have a struct like

1
2
3
4
5
6
struct foo1{
char* stuff1;
char* stuff2;
char* stuff3;
char* stuff4;
};



but its taking up a bit of memory when i have a full array these! is there a better way store this in the most compact way possible.
Last edited on
That depends on what that data is supposed to be. What's taking up space is most likely what they're pointing to, not the pointers themselves. So you need to elaborate.
closed account (zb0S216C)
twoencore wrote:
"is there a better way store this in the most compact way possible."

I can think of 1 solution that requires a single char*. It's simple. Let's say I wanted 4 10 byte blocks of memory. At the end each block would be a null-character, which marks the end of the proceeding block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Memory
{
    Memory();
    ~Memory();

    char *Raw__;
};

Memory::Memory()
    : Raw__(new(std::nothrow) char[(4 * 10)])
{
    for(int I(0); I < 3; ++I)
        this->Raw__[((I * 10) - 1)] = '\0';
}

Memory::~Memory()
{
    delete [] this->Raw__;
}

That above is simple. In Memory's constructor, a block of 40 bytes is allocated (the std::nothrow is rather self-explanatory. Instead of throwing an exception, new will return null). In the body of the constructor, the 9th character of each block is set to the null-character.

Whenever you want to extract the data, simply pull out all of the chars until you encounter the null character.

Wazzak
Last edited on
@Framework
Very nice. Is the increase in processing worth the compacting? Honest question; it may be a moot point.

Also, depending on the use of your struct, i've run into problems where the data was padded with bytes by the system and with Framework's solution, you can make sure everything is aligned as you intend.
closed account (zb0S216C)
cantide5ga wrote:
"Is the increase in processing worth the compacting?"

In my opinion, it depends on the program's needs. You can extract each block then use them, treat each block as an array, or provide a simple interface for the block itself. Though, there may be an algorithm to enhance processing.

I should say that my solution allows the use of placement new, which I recommend.

Wazzak
Last edited on
Topic archived. No new replies allowed.