This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

non-zero initialization in NCS

Dears,

How do I set a non-zero initialization variable in an NCS development environment? thanks!

Best Regards!

Ricky Yan

Parents
  • This seems be a more C/compiler-related question. In C, static and global variables are initialized to zero and variables declared inside functions (on the stack) can be set to anything. Check out these answers on Stack Overflow:

    https://stackoverflow.com/a/1597426

    https://stackoverflow.com/a/7975099

    I tested this by modifying the sample zephyr/samples/hello_world like shown below,

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    
    void testFunc(){
    	int x[10];
    	int y;
    	int z;
    	static int f[10];
    	for(int b=0;b < sizeof(x)/sizeof(int); b++){
    		printk("x[%d]: %d\n",b ,x[b]);
    	}
    	for(int l=0;l < sizeof(f)/sizeof(int); l++){
    		printk("f[%d]: %d\n",l ,f[l]);
    	}
    	printk("y: %d\n", y);
    	printk("z: %d\n", z);
    }
    
    int q[10];
    
    void main(void)
    {
    	for(int b=0;b < sizeof(q)/sizeof(int); b++){
    		printk("q[%d]: %d\n",b ,q[b]);
    	}
    
    	int a[10];
    	int b;
    	int c;
    	//printk("Hello World! %s\n", CONFIG_BOARD);
    	for(int b=0;b < sizeof(a)/sizeof(int); b++){
    		printk("a[%d]: %d\n",b ,a[b]);
    	}
    	printk("b: %d\n", b);
    	printk("c: %d\n", c);
    	testFunc();
    }
    

    and got the following result:

    *** Booting Zephyr OS build v2.4.99-ncs1  ***
    q[0]: 0
    q[1]: 0
    q[2]: 0
    q[3]: 0
    q[4]: 0
    q[5]: 0
    q[6]: 0
    q[7]: 0
    q[8]: 0
    q[9]: 0
    a[0]: 1
    a[1]: 1323
    a[2]: 19767
    a[3]: 46
    a[4]: 19805
    a[5]: 15761
    a[6]: 0
    a[7]: -536810240
    a[8]: 536873156
    a[9]: 0
    b: 0
    c: 0
    x[0]: 1
    x[1]: 1323
    x[2]: 19767
    x[3]: 46
    x[4]: 19805
    x[5]: 15761
    x[6]: 0
    x[7]: -536810240
    x[8]: 536873156
    x[9]: 0
    f[0]: 0
    f[1]: 0
    f[2]: 0
    f[3]: 0
    f[4]: 0
    f[5]: 0
    f[6]: 0
    f[7]: 0
    f[8]: 0
    f[9]: 0
    y: 0
    z: 0

    I also got the following warnings when building it for the nrf9160dk_nrf9160

    [1/8] Building C object CMakeFiles/app.dir/src/main.c.obj
    ../src/main.c: In function 'testFunc':
    ../src/main.c:21:2: warning: 'y' is used uninitialized in this function [-Wuninitialized]
       21 |  printk("y: %d\n", y);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c:22:2: warning: 'z' is used uninitialized in this function [-Wuninitialized]
       22 |  printk("z: %d\n", z);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c: In function 'main':
    ../src/main.c:40:2: warning: 'b' is used uninitialized in this function [-Wuninitialized]
       40 |  printk("b: %d\n", b);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c:41:2: warning: 'c' is used uninitialized in this function [-Wuninitialized]
       41 |  printk("c: %d\n", c);
          |  ^~~~~~~~~~~~~~~~~~~~

    It seems like non initialized variables that are not an array, will get defined as 'uninitialized' and will get a value of 0, while non-initialized arrays will conform to the behaviour I explained in my initial answer (initialized to 0 if static of global, or indeterminate if declared on the stack).

    Could you elaborate what you mean by non-zero initialization? What do you want to achieve? I would recommend you to just pay attention to the build log, and if get any warnings regarding uninitalized variables.

    Best regards,

    Simon

Reply
  • This seems be a more C/compiler-related question. In C, static and global variables are initialized to zero and variables declared inside functions (on the stack) can be set to anything. Check out these answers on Stack Overflow:

    https://stackoverflow.com/a/1597426

    https://stackoverflow.com/a/7975099

    I tested this by modifying the sample zephyr/samples/hello_world like shown below,

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    
    void testFunc(){
    	int x[10];
    	int y;
    	int z;
    	static int f[10];
    	for(int b=0;b < sizeof(x)/sizeof(int); b++){
    		printk("x[%d]: %d\n",b ,x[b]);
    	}
    	for(int l=0;l < sizeof(f)/sizeof(int); l++){
    		printk("f[%d]: %d\n",l ,f[l]);
    	}
    	printk("y: %d\n", y);
    	printk("z: %d\n", z);
    }
    
    int q[10];
    
    void main(void)
    {
    	for(int b=0;b < sizeof(q)/sizeof(int); b++){
    		printk("q[%d]: %d\n",b ,q[b]);
    	}
    
    	int a[10];
    	int b;
    	int c;
    	//printk("Hello World! %s\n", CONFIG_BOARD);
    	for(int b=0;b < sizeof(a)/sizeof(int); b++){
    		printk("a[%d]: %d\n",b ,a[b]);
    	}
    	printk("b: %d\n", b);
    	printk("c: %d\n", c);
    	testFunc();
    }
    

    and got the following result:

    *** Booting Zephyr OS build v2.4.99-ncs1  ***
    q[0]: 0
    q[1]: 0
    q[2]: 0
    q[3]: 0
    q[4]: 0
    q[5]: 0
    q[6]: 0
    q[7]: 0
    q[8]: 0
    q[9]: 0
    a[0]: 1
    a[1]: 1323
    a[2]: 19767
    a[3]: 46
    a[4]: 19805
    a[5]: 15761
    a[6]: 0
    a[7]: -536810240
    a[8]: 536873156
    a[9]: 0
    b: 0
    c: 0
    x[0]: 1
    x[1]: 1323
    x[2]: 19767
    x[3]: 46
    x[4]: 19805
    x[5]: 15761
    x[6]: 0
    x[7]: -536810240
    x[8]: 536873156
    x[9]: 0
    f[0]: 0
    f[1]: 0
    f[2]: 0
    f[3]: 0
    f[4]: 0
    f[5]: 0
    f[6]: 0
    f[7]: 0
    f[8]: 0
    f[9]: 0
    y: 0
    z: 0

    I also got the following warnings when building it for the nrf9160dk_nrf9160

    [1/8] Building C object CMakeFiles/app.dir/src/main.c.obj
    ../src/main.c: In function 'testFunc':
    ../src/main.c:21:2: warning: 'y' is used uninitialized in this function [-Wuninitialized]
       21 |  printk("y: %d\n", y);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c:22:2: warning: 'z' is used uninitialized in this function [-Wuninitialized]
       22 |  printk("z: %d\n", z);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c: In function 'main':
    ../src/main.c:40:2: warning: 'b' is used uninitialized in this function [-Wuninitialized]
       40 |  printk("b: %d\n", b);
          |  ^~~~~~~~~~~~~~~~~~~~
    ../src/main.c:41:2: warning: 'c' is used uninitialized in this function [-Wuninitialized]
       41 |  printk("c: %d\n", c);
          |  ^~~~~~~~~~~~~~~~~~~~

    It seems like non initialized variables that are not an array, will get defined as 'uninitialized' and will get a value of 0, while non-initialized arrays will conform to the behaviour I explained in my initial answer (initialized to 0 if static of global, or indeterminate if declared on the stack).

    Could you elaborate what you mean by non-zero initialization? What do you want to achieve? I would recommend you to just pay attention to the build log, and if get any warnings regarding uninitalized variables.

    Best regards,

    Simon

Children
No Data
Related