5

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 ^_^

ashleysmithgpu
  • 1,867
  • 20
  • 39
  • Can you tell us what architecture and OS? Your compiler of choice and optimization settings also matter equally. – Tim Post Feb 16 '10 at 10:11
  • Search the web for "memory pool", which is my understanding of your objective. A common technique for constraining fragmentation in restrictive platforms. – Thomas Matthews Feb 16 '10 at 18:31

5 Answers5

4

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
  • 391,730
  • 64
  • 469
  • 606
  • Ah, yes, i suppose there is still the same amount of memory available. Im just swapping where it is... thanks! – ashleysmithgpu Feb 16 '10 at 10:19
  • 1
    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 Feb 16 '10 at 18:26
  • @ThomasMatthews Please See This Question "http://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation" .The Answer Selected As Best Answer is wrong and will confuse beginner. If you could confirm that the answer is wrong it would be of great help. – Suraj Jain Aug 16 '16 at 17:17
  • @SurajJain: How is the selected answer in the linked question wrong? – Thomas Matthews Aug 16 '16 at 17:48
  • @ThomasMatthews It has been edited now by Jonathan Leffler one hour ago. See the edit history the earlier answer was entirely wrong. – Suraj Jain Aug 16 '16 at 18:25
1

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
  • 56,922
  • 16
  • 83
  • 148
1

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
  • 33,371
  • 15
  • 110
  • 174
0

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
  • 15,403
  • 1
  • 27
  • 41
0

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.

  • 1
    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 Feb 16 '10 at 18:29
  • @anon I think Thomas is referring to the technique where a large array of fixed size is allocated statically at compile time, following which any dynamic usage is handled by the program from this large array (memory pool). This is not equivalent to the heap, because the heap is under OS control, while the memory pool is under the program's control... – Assad Ebrahim Oct 07 '12 at 05:31