• AVR Freaks

Lockedcan't initialize string array

Author
Chaabane
Super Member
  • Total Posts : 196
  • Reward points : 0
  • Joined: 2009/10/10 11:45:01
  • Location: 0
  • Status: offline
2014/04/03 03:02:23 (permalink)
0

can't initialize string array

Hello,
I set up a new project for PIC32MX795F512L and added FreeRTOS, MDD library.
 
I added this line of code before starting the freeRTOS scheduler :
        char StringToWrite[] = "this is a test"; 
And while debugging the code I was surprised that the StringToWrite value in the watch window is equal to 0, the compiler reserved 14 bytes in the memory and filled them with 0x00 , the same size as "this is a test" string.
I use XC32 version 1.31 with no optimization and mplab IDE 2.00
I also added the string.h library.
 
I created a new project from scratch using C32 C Template from microchip samples and added the same line of code as above, It worked just fine.
 
I'm not sure what i missed ? do you guys have any ideas or suggestions ?
 
#1

11 Replies Related Threads

    bosco
    Super Member
    • Total Posts : 2119
    • Reward points : 0
    • Joined: 2004/07/24 10:17:53
    • Location: Idaho
    • Status: offline
    Re: can't initialize string array 2014/04/03 06:07:15 (permalink)
    0
    A string literal is const.
    perhaps
    char const StringToWrite[] = "this is a test"; 
     
     
    #2
    Chaabane
    Super Member
    • Total Posts : 196
    • Reward points : 0
    • Joined: 2009/10/10 11:45:01
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/03 08:38:42 (permalink)
    +1 (1)
    bosco
    A string literal is const.
    perhaps
    char const StringToWrite[] = "this is a test"; 
     
     


    Thank you for the suggestion, but it didn't work.
    #3
    bosco
    Super Member
    • Total Posts : 2119
    • Reward points : 0
    • Joined: 2004/07/24 10:17:53
    • Location: Idaho
    • Status: offline
    Re: can't initialize string array 2014/04/03 11:39:56 (permalink)
    0
    look at the map file generated by the linker.
    the symbol StringToWrite should be listed in this file.
    post the lines for StringToWrite from the linker map file
     
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11424
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: can't initialize string array 2014/04/03 13:50:31 (permalink)
    +1 (1)
    bosco
    A string literal is const.



    Not in the context of initialization.  It is no different than initializing the array with a brace-enclosed list of chars.
    #5
    Chaabane
    Super Member
    • Total Posts : 196
    • Reward points : 0
    • Joined: 2009/10/10 11:45:01
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/04 04:33:06 (permalink)
    0
    bosco
    look at the map file generated by the linker.
    the symbol StringToWrite should be listed in this file.
    post the lines for StringToWrite from the linker map file
     




    I didn't find it in the map file, I found other variables that i declared, mostly struct types
     
    Any ideas ?
    #6
    bosco
    Super Member
    • Total Posts : 2119
    • Reward points : 0
    • Joined: 2004/07/24 10:17:53
    • Location: Idaho
    • Status: offline
    Re: can't initialize string array 2014/04/04 12:48:27 (permalink)
    +1 (1)
    It might not be used, optimizer remove it.
    it might not be compiled into the project, project settings
    it is in an include file that is not included
    something like that
     
    #7
    Chaabane
    Super Member
    • Total Posts : 196
    • Reward points : 0
    • Joined: 2009/10/10 11:45:01
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/06 02:02:39 (permalink)
    0
    bosco
    It might not be used, optimizer remove it.
    it might not be compiled into the project, project settings
    it is in an include file that is not included
    something like that
     


    Optimization level is always at 0. never used it
    I put it in the main.c file, and i make sure to manipulate it with sprintf
     
    I'm really stack here,
     
    #8
    Chaabane
    Super Member
    • Total Posts : 196
    • Reward points : 0
    • Joined: 2009/10/10 11:45:01
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/08 03:05:37 (permalink)
    0
    Here are the assembly code generated by the compiler
     
    it doesn't work in this case
     
    char StringToWrtire[] = "this is a test";
    0x9D01F788: LUI V0, -25342
    0x9D01F78C: LW A1, 6980(V0)
    0x9D01F790: ADDIU V1, V0, 6980
    0x9D01F794: LW A0, 4(V1)
    0x9D01F798: ADDIU V1, V0, 6980
    0x9D01F79C: LW V1, 8(V1)
    0x9D01F7A0: SW A1, 316(S8)
    0x9D01F7A4: SW A0, 320(S8)
    0x9D01F7A8: SW V1, 324(S8)
    0x9D01F7AC: ADDIU V0, V0, 6980
    0x9D01F7B0: LHU V1, 12(V0)
    0x9D01F7B4: SH V1, 328(S8)
    0x9D01F7B8: LBU V0, 14(V0)
    0x9D01F7BC: SB V0, 330(S8)
     
    it worked with new project based on a xC32 template
     
    char variable[] = "this is a test";
    0x9D000010: LUI V0, -25344
    0x9D000014: LW A1, 1488(V0)
    0x9D000018: ADDIU V1, V0, 1488
    0x9D00001C: LW A0, 4(V1)
    0x9D000020: ADDIU V1, V0, 1488
    0x9D000024: LW V1, 8(V1)
    0x9D000028: SW A1, 84(S8)
    0x9D00002C: SW A0, 88(S8)
    0x9D000030: SW V1, 92(S8)
    0x9D000034: ADDIU V0, V0, 1488
    0x9D000038: LHU V1, 12(V0)
    0x9D00003C: SH V1, 96(S8)
    0x9D000040: LBU V0, 14(V0)
    0x9D000044: SB V0, 98(S8)
     
    The blocks look the same, yet in the face case i always get an empty array.
     
    Any suggestions ?
    #9
    andersm
    Super Member
    • Total Posts : 2675
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/08 04:50:03 (permalink)
    0
    In the first case, the base address of the source string is 0x9d021b44. Look at the map file and see what is stored at that address. Also use memcmp or some other code to verify the array contents, the watches are not always reliable.
    #10
    Chaabane
    Super Member
    • Total Posts : 196
    • Reward points : 0
    • Joined: 2009/10/10 11:45:01
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/08 05:54:09 (permalink)
    +1 (1)
    One stupid question, how did you get the base adress of the string ? 0x9d021b44 
    #11
    andersm
    Super Member
    • Total Posts : 2675
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: can't initialize string array 2014/04/08 06:19:24 (permalink)
    +2 (2)
    From the instruction pair
    0x9D000010: LUI V0, -25342
    0x9D000014: LW A1, 6980(V0)

    The first instruction loads the value 0x9d02 into the upper 16 bits of register v0, the second instruction loads the first 4 bytes of the string from the address v0+0x1b44. The other load instructions also use v0 as base register, with increasing indexes. Enabling optimization level -O1 should produce clearer assembly code, with most redundant operations eliminated.
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5