This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430FR2355: Why isn't memset zeroing out the structure? ; Why aren't assignments working?

Part Number: MSP430FR2355

Can some tell me:

1. Why isn't memset zeroing out my structure?

2. Why are only some of the assignments pGPS_d being made?? (latitude is getting filled correctly, longitude mostly (missing E or W??), utc and date and satellite never get stuffed)

Code along with debugger showing variables:  (NMEAData is spot on correct)

typedef struct gpsLogValues
{
    char utc[10];
    char latitude[11];
    char longitude[12];
    char date[6];
    char satellites[3];
}gpsLogValues_s;


typedef struct gpsData
{
    gpsLogValues_s *gpsLog;
}gpsMssg_s;

  • You pass memset the address of a pointer along with the size of said pointer. So it sets the pointer to zero which isn't going to work out very well.

  • Ok....Can you tell me what I should do here?  I've changed the &pGPS_d to pGPS_d but left sizeof alone and still get the same results....Not sure what the correct thing to do is....

    Also any comments on why only some of the members of the structure are updating?

  • Hi Steve,

    1. For memset it takes in parameters of memset( *pointer, value, numberOfBytes). Because pGPS_d is already a memory address it should be directly put. You said you're getting the same results, can you repost the new image of your expressions.
    2. I would double check that the NMEAData you're using in the strncpy is the expected values. If its longitude and latitude are back to back it looks like your values would be off. I think you'd want &NMEAData [31] for longitude.
      1. This could be the reason date is off as well.
      2. UTC should get some data, I would step through this and double check what is happening on the strncpy
      3. For satellites I would check that you're getting into the if statement to begin with, same with the longitude I would double check if there is a buffer bit, otherwise you might be off by one.

    In general, since you are working with memory addresses it would be beneficial to step through the sections and verify that you have the correct values passed in. I recommend stepping into the strncpy and memset functions to explicitly see if the memory is getting properly updated.

    Regards,

    Luke

  • The use of fixed indexes into the NMEA string is going to fail badly since the fields are of variable length. The NMEA format uses commas to separate the fields and you should scan for those.

    This is the sort of problem that you could work on with the host computer a lot more easily. No need to program the target and connect with a debugger. Just compile and fire up gdb. Or whatever debugger you are using.

  • Per request....Here are some snapshots....

    the function in a driver library along with the results showing a breakpoint right after memset....As you can see nothing zeroes out....(guessing it has to do with sizeof pointer but I don't know what should go in here to zero out the structure??)...As you can see nothing zeroes out

    The driver file .h that goes with above function

    The call in main:

    GPSMssgParser(pGPSData);

    The definition in main:

    gpsMssg_s GPSData = {}, *pGPSData = &GPSData;

    So ultimately the driver is to fill the structure with values.....Knowing that structure passing is taboo I am passing a pointer from main....Below shows two pics....the NMEA data I am capturing and the arrays (I show date and utc)  after supposed updating (breakpoint set at P1OUT toggle in the driver c file....NMEA looks fine (I show the mssg and the entry prior to A is the utc), struct fields not completely updating, why (latitude field is all correct, longitude is almost complete, missing the E, W entry , date is all zeroes and UTC time doesn't populate the first chars)??

  • A big hint (OK, waving red flag) should be that in your screen dump the data starts at address 0x0000.

  • I agree....very ominous looking....is this a scope thing or a pointer without assignment?

  • Ok....I solved my issue.....I changed gpsLog from a pointer to just a type variable....Can you explain what I would have needed to do to keep gpsLog as a pointer and have it work??(as a HW guy hacking at FW I don't have the answer)

    typedef struct gpsData
    {
        gpsLogValues_s gpsLog;
    }gpsMssg_s;
    

  • Steve, 

    I think the problem is that you declared a pointer without creating an actual instance of the struct. A pointer must point to a memory location that has been initialized (or at least reserved) for that data type. 

    Regards,

    Evan

  • I suspect you wanted this in your Previous (not your New) code:

    > memset(pGPS_d->gpsLog, 0, sizeof(*pGPS_d->gpsLog));

    This idiom ("memset(ptr, 0, sizeof(*ptr))") isn't always what you want, but it usually is.

  • Interesting....I will try this one...

    Thank you

  • Hi Evan....

    I thought I did that with this assignment in main: (even though main never pushes data to the function and only receives data from the function)

    gpsMssg_s GPSData = {}, *pGPSData = &GPSData;

  • Right, but gpsMssg_s contains a pointer to gpsLogValues_s *gpsLog. *gpsLog doesn't have an associated structure in memory yet.

    Evan

**Attention** This is a public forum