#1
  1. User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    UK
    Posts
    471
    Rep Power
    76

    Passing struct to function corrupts values


    Hi,

    I've got some quite complex code that results in data being corrupt when passing from one function to another.

    I can't really post much code due to the size of it all but I'll post snippets of the basic idea and see if it make sense.

    This is my file structure:

    Imgur: The most awesome images on the Internet

    essentially this is the file tree:

    ./main.c
    ./headers.h
    ./Application/app.c
    ./Application/app.h
    ./API/settings.c
    ./API/settings.h


    I have struct definition in ./API/settings.h that contains various members but a lot of members are enumerations that are also defined in ./API/settings.h. We'll call it:

    Code:
    struct settings
    {
    uint32_t number_1;
    enum my_enum setting_1;
    enum my_other_enum setting_2;
    };
    I then have a new instance of this struct declared in ./Application/app.h

    Code:
    struct settings my_settings;
    This instance is manipulated in ./Application/app.c no problem in a function called function() (for the sake of simplicity). Now I want to pass it to a function within ./API/settings.c so that it can be saved.

    The function definition is:

    Code:
    enum status_code set(struct settings set);
    it is called in ./Application/app.c

    Code:
    set(my_settings);
    If I break at the function call in app.c the values are correct, if I step into the function, the variable set has corrupted values in it.

    Now, there are lots of functions in settings.c that create their own instances of struct settings that don't have any problems. It seems to happen when I try to send data to function() from outside of settings.c

    I don't have any extern keywords or anything. I tried various stabs in the dark but outright couldn't work out what was happening anymore.

    I hope this made sense but if not I'll try to clarify further.

    Cheers,

    George.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    My guess is that you need to delete your obj files and recompile everything. Corruption usually happens if you change the structure of your struct in a header file and only recompile a few of the source files, instead of all of them that use the struct. Try cleaning your project and recompiling again.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    UK
    Posts
    471
    Rep Power
    76
    I tried it but no success.

    I tried to create a new project with the basic function, struct an enumerations to recreate the problem with simple code but couldn't re-create it. It also doesn't help that I'm programming an ARM cortex m0 so the environment is a bit different than using a PC.

    I'm going to try a whole new approach to this and see what happens. Thanks for your suggestion tho.

IMN logo majestic logo threadwatch logo seochat tools logo