Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #28782

Re: No dynamic memory at all: newlib and sprintf

From pozz <pozzugno@gmail.com>
Newsgroups comp.arch.embedded
Subject Re: No dynamic memory at all: newlib and sprintf
Date 2019-08-30 10:23 +0200
Organization A noiseless patient Spider
Message-ID <qkamdp$2ka$1@dont-email.me> (permalink)
References <qkaj7s$gki$1@dont-email.me> <qkakta$qbg$1@dont-email.me>

Show all headers | View raw


Il 30/08/2019 09:57, David Brown ha scritto:
> On 30/08/2019 09:29, pozz wrote:
>> I'd like to avoid using dynamic memory (heap) at all. I'm using gcc for
>> ARM and newlib (really I'm using NXP MCUXpresso IDE, but I don't think
>> this is important).
>>
>> When the code is minimal, I noticed heap related functions (malloc, free
>> and so on) aren't included in the output binary file.
>> However, as soon as I use sprintf-like functions, heap is used.
>>
>> I looked at the newlib source code and indeed I noticed __ssputs_r(), a
>> common shared function between sprintf-like functions, that uses malloc.
>> I think it uses malloc for asprintf-like functions that I'm not using.
>>
>> How to remove completely the heap with sprintf functions? Maybe by
>> implementing a custom minimal sprintf function?
> 
> Check how __ssputs_r is using the heap.  Maybe you can get away with
> just adding these to your code somewhere:
> 
> static char heap[100];
> void * malloc(size_t size) {
> 	(void) size;
> 	return heap;
> }
> 
> void free(void *ptr) {
> 	(void) ptr;
> }
> 
> If the __ssputs_r function only ever calls malloc once while it works,
> and you don't use it from different threads or contexts (the "_r" in
> newlib is for reentrant code), that might do the job, while saving you
> from the effort of making your own printf stuff.
> 

IMHO, I will have an error from the linker, because of multiple 
definition of malloc and free.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 09:29 +0200
  Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-08-30 09:57 +0200
    Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 10:23 +0200
      Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-08-30 10:48 +0200
        Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 14:21 +0200
          Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 15:39 +0200
            Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-09-01 22:47 +0200
              Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-09-01 23:53 +0200
                Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-09-02 13:09 +0200
    Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 10:35 +0200
  Re: No dynamic memory at all: newlib and sprintf Paul Rubin <no.email@nospam.invalid> - 2019-08-30 01:46 -0700
  Re: No dynamic memory at all: newlib and sprintf Dave Nadler <drn@nadler.com> - 2019-08-30 06:17 -0700

csiph-web