tags:

views:

244

answers:

5

Hi all,

Im working in a memory restricted environment and need to create strings dynamically, but still have them not take up heap memory. So does this make sense:

static char staticStringBuffer[10240];
static size_t staticStringWatermark = 0;

void createString( const char * something, const char * somethingElse ) {
    char buf[1024];
    strcat(buf, "test");
    strcat(buf, something);
    strcat(buf, somethingElse);

    strcat(&staticStringBuffer[staticStringWatermark], buf);
    staticStringWatermark += strlen(buf+1);
}

This probably dosent compile, but is what I am attempting sane - sacrificing static memory for heap memory?

Thank-you ^_^

+3  A: 

That of course depends on what your particular environment does when it loads your program; where is the program's static data put? On many operating systems, the program is loaded into heap memory and run from there, so your static data will still end up on the heap.

unwind
Ah, yes, i suppose there is still the same amount of memory available. Im just swapping where it is... thanks!
yuumei
However, in many *embedded systems*, programs are executed in Read-Only Memory and have restricted amount of stack and heap space. Constant static data will be placed in Read-Only Memory (ROM). Static and global variables will be placed in memory, usually in a different place than the Heap or the Stack.
Thomas Matthews
A: 

That will work. Some caution is needed not to write past the end of the static buffer, which would happen in your example if strlen(something) + strlen(somethingElse) >= 10240.

wallyk
A: 

If memory is really that restrictive, couldn't you append/strcat straight onto the staticStringBuffer rather than your temp buf?

I take it you know that this code isn't thread-safe? You probably don't need it to be but just making sure.

Also use strncat instead of strcat - it prevents buffer overflows.

Andy Shellam
A: 

If you need to create strings dynamically, you need a heap, though not necessarily the default one supplied with your C runtime library. If you are in a very restricted environment, don't create strings dynamically.

anon
In a restricted environment, such as *embedded systems*, strings may be allocated from a memory pool: a fixed array size. This adds more control to the data; preventing unwanted heap accesses which leads to memory fragmentation for the whole program vs. confined fragmentation for text.
Thomas Matthews
@Thomas memory pool == heap
anon
+1  A: 

I agree with unwind.

When forced to use static allocation, I usually allocate those blocks within the scope that they are used, i.e. within the function itself.

i.e.

static char *createstring(char *foo, char *bar)
{
    static char ret[size];

    /* do some work, make sure you pay attention to the printf sub system when it
       tells you how many bytes weren't printed ... */

    return ret;
}

.. of course, ensuring that entry into createstring() is protected by some sort of mutual exclusion and that callers don't need to modify the result.

Depending on your compiler, YMMV. Do you really need to make those global?

Tim Post