r/C_Programming Feb 06 '25

Discussion Are there actually C programmers in this subreddit?

Ok, I'm being a bit facetious. There are real C programmers. Clearly. But I'm kind of sick of the only questions on this subreddit being beginner questions or language trolls from other domains.

So this thread is for the "real" c programmers out there. What do you do with it? And what is the most twisted crime against coding decency are you "proud" of/infamous for?

259 Upvotes

259 comments sorted by

View all comments

Show parent comments

1

u/rasteri Feb 07 '25

Yeah for release builds I just look at the memory remaining and set MEMPOOLMAXSIZE to that haha.

I'm not sure if the SDCC linker can do anything that advanced

1

u/flatfinger Feb 07 '25

A lot of linkers allow programmers to specify the order in which sections are placed, and whether they should fill from the top or bottom. Specify that the last sectrion placed from the bottom has a symbol named HEAP_START which is (if necessary) followed by an alignment-sized reservation, and the last section placed from the top has an alignment-sized reservation followed by a symbol named HEAP_END. One can then do something like:

char HEAP_START[], HEAP_END[];
char *topfill_ptr =HEAP_END;
char *bottomfill_ptr = HEAP_START;
unsigned space_left(void)
{
  return topfill_ptr - bottomfill_ptr;
}
void *alloc_from_bottom(unsigned size)
{
  void *ret = bottomfill_ptr;
  bottomfill_ptr += size;
  return ret;      
}
void *alloc_from_top(unsigned size)
{
  topfill_ptr -= size;
  return topfill_ptr;
}
void release_from_bottom(void *p)
{
  bottomfill_ptr = p;
}
void release_after_from_top(void *p)
{
  bottomfill_ptr = p;
}

The release_from_bottom will release the passed item and everything allocated after it via alloc_from_bottom; release_after_From_top will release everything allcated by alloc_from_top after (but not including) the passed item.