Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

User avatar
Rick Kimball
Posts: 754
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby Rick Kimball » Mon Oct 24, 2016 9:28 pm

RogerClark wrote:Initialisation of variables in global scope is always problematic, because the order in which things are setup is not defined in the C specification.

Actually using static variables in functions is a way of controlling the order of initialization.

https://isocpp.org/wiki/faq/ctors#static-init-order
https://isocpp.org/wiki/faq/ctors#stati ... -first-use

Code: Select all

$ cat check_order.cpp
/* vim: set tabstop=4 shiftwidth=4 autoindent
*/
#include <stdio.h>
#include <stdint.h>

int callme1() {
    fputs("in callme1\n",stderr);
   
    return 1;
}

void XYZ() {
    fputs("in XYZ\n",stderr);
}

void ABC() {
    static int foobar = callme1();
    fputs("in ABC\n",stderr);
}

int main(void)
{
    fputs("in main\n",stderr);

    XYZ();
    ABC();
}
$ make check_order
g++     check_order.cpp   -o check_order
$ ./check_order
in main
in XYZ
in callme1
in ABC
$


if the variable had been a global static variable, then yes the ctor would have been called by the c startup code in some random order.

-rick
-rick

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby RogerClark » Mon Oct 24, 2016 9:49 pm

Rick

My mistake

I thought that code was in global scope

I'll update my post.

I shouldnt post before my first cup of coffee in the morning ;-)

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby RogerClark » Mon Oct 24, 2016 9:57 pm

Rick

Any idea when the compiler would actually assign the variable that is calling millis() ?

Is this when the function is first called, or as part of global init.

If its global init, then calling millis() is going to be problematic - and probably pointless as its always going to be zero

User avatar
Rick Kimball
Posts: 754
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby Rick Kimball » Tue Oct 25, 2016 12:17 am

It falls under the first use rule I posted above
-rick

umerocks
Posts: 10
Joined: Mon Sep 19, 2016 9:02 am

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby umerocks » Wed Oct 26, 2016 4:02 pm

Looking at the code again, perhaps the code is just an example rather than the real code, as the variable names seem strange.
i.e not real names of things.


Yes Roger, you are right, its just an example code that i mentioned and the variable names are for an example aswel.

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Extended Code Size while migrating from Arduino ProMini board to STM32 Maple-Mini board.

Postby RogerClark » Wed Oct 26, 2016 8:11 pm

Ok.

Thanks.


Return to “Maple mini”

Who is online

Users browsing this forum: No registered users and 1 guest