Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 20, 2024, 12:54:46 00:54


Login with username, password and session length


Pages: [1]
Print
Author Topic: XC16 v1.20 -> v1.25 - memset issue with optimization -s  (Read 4133 times)
0 Members and 1 Guest are viewing this topic.
Avataar120
Inactive

Offline Offline

Posts: 4

Thank You
-Given: 0
-Receive: 3


« on: December 16, 2015, 03:57:21 15:57 »

Hello everybody,

I have an issue with all XC16 version from 1.20 to 1.25 with the memset instruction + optimization level s in a very typical situation ...
I tried to simplify my code at maximum ...


Code:
/* 
 * File:   newmain.c
 * Author:
 *
 * Created on 17 décembre 2015, 00:05
 */
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

        _CONFIG1(WDTPS_PS1 & FWPSA_PR32 & WINDIS_OFF & FWDTEN_OFF & ICS_PGx3 & GWRP_OFF & GCP_OFF & JTAGEN_OFF)
        _CONFIG2(POSCMOD_HS & I2C1SEL_PRI & IOL1WAY_OFF & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_PRIPLL & PLL96MHZ_ON & PLLDIV_DIV8 & IESO_OFF)
        _CONFIG3(WPFP_WPFP0 & SOSCSEL_IO & WUTSEL_LEG & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM)
        _CONFIG4(DSWDTPS_DSWDTPS3 & DSWDTOSC_LPRC & RTCOSC_SOSC & DSBOREN_OFF & DSWDTEN_OFF)
/*
 *
 */
typedef struct
{
    unsigned char BeginHour;
    unsigned char EndHour;
}tHoraire;

typedef struct
{
        unsigned char
        KeyAutorisation : 1,
        PushAutorisation: 1,
        UnlockAutorisation : 1,
 OpeningConstraints: 1,
 Dummy: 4;
} tBitFieldOptions;

typedef union
{
 unsigned char Content [50];
        
        struct {
            unsigned long FingerprintID [4];
            unsigned short PINCode;
            unsigned char Name[20];
            tHoraire Days [7];
            tBitFieldOptions Options;
            unsigned char OpeningTempo;
        };
}tConfigUser;

void Function2 ( unsigned char * User )
{
    *User = 0;
}

void Function1 ( tConfigUser User )
{
 Function2 ( User.Content );
}

void main ()
{
 tConfigUser User;

        memset ( ( void* )&User, 0xFF, sizeof ( tConfigUser ) );

        Function1 ( User );
}

After memset, the content of User is completely random.
If I comment the line "Function1" -> no more issue
If I don't make call to Function2 with User.Content parameter -> no more issue
If I compile this code without optimization -> no more issue
 
Has somebody an idea ?
 
Thanks !
Logged
FTL
Junior Member
**
Offline Offline

Posts: 83

Thank You
-Given: 170
-Receive: 33


« Reply #1 on: December 17, 2015, 02:25:18 02:25 »

I think there is an issue here with passing by value vs. passing by reference.

When you do "function1 ( User );" you are passing User by value, so any change to User made in the function cannot be returned. You must pass by reference, or explicitly use a pointer to the data to be able to modify the data in the structure (instead of a copy).

I agree that optimized and unoptimized code should do the same thing, but the compiler may be validly removing "useless" code.

When I look at the "*User = 0;", that should change the first byte in a character array to zero. If it is interpreted as a string, that is the same as initializing the whole string to a zero length string, and the bytes following the first byte do not matter. Could the compiler have seen that and decided that since you are initializing a string to "", that the memset of the rest of the string to 0xFF could be optimized out?

It has been a while since I've coded straight C with structs and unions, so I'm not absolutely sure, but I would look into what the C standard has to say about passing a struct by value.


Edit - Fixed typo.
« Last Edit: December 17, 2015, 02:30:05 02:30 by FTL » Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC