Variables in the data structure changes when they were sent to CreateRawDataBUFR at runtime. With gdb debugger, just one line before CreateRawDataBUFR, I print the value of rdi.sIdentName, rdi.sRawDataFilePath and rdi.sRawDataFileName to the screen, they are "IST" and "data/" "IST050906125859.RAWADM9" accordingly. They are true. But after i send the address of rdi to CreateRawDataBUFR, under CreateRawDataBUFR statement, i again print them with debugger. rdi.sIdentName is "IST0509061", rdi.sRawDataFilePath is "RAWADM9" , rdi.sRawDataFileName is something like "*\000\000\000\000ïÿ¿". why did they mixed each other.

Where did i do wrong?

Data structure is shown below.

typedef struct raw_data_interface
{
        char sIdentName[10];
        int nIdentCharLength;
        char sRawDataFilePath[200];
        int nFilePathCharLength;
        char sRawDataFileName[200];
        int nFileNameCharLength;
        int nYear;
        int nMonth;
        int nDay;
        int nHour;
        int nMinute;
        int nSecond;
        char sDataDescriptorIdentifier[7];
        char sDataDescriptorRawData[7];
}RawDataInterface;

Test.c function :

#include <stdio.h>
#include <stdlib.h>
#include <mscc.h>
#include "RawDataInterface.h"
extern void * CreateRawDataBUFR(unsigned char **, int *, char *, RawDataInterface *);
void AcquireBUFRData(RawDataInterface *, char *);
 
int main(int argc,char *argv[])
{
/********************************************************************************/
/*                      EMPRESS Initialization                                  */
/********************************************************************************/
        if(!msinit())
        {
                printf("msinit failed!\n");
                msend();
                return 1;
        }
        else
        {
                printf("msinit basarili\n");
        }
        int i = 0;
        int * bufrSize = malloc(4);
        unsigned char * ucBufrMessage = NULL;
        /* RawDataInterface * rdi = malloc(sizeof * rdi); */
        RawDataInterface rdi;
        unsigned char * ucBinary = NULL;
 
        char sFileName[24] = "IST050906125859.RAWADM9";
        sFileName[23] = '\0';
 
/********************************************************************************/
/*              Filling Raw Data Interface to be sent to HVFE                   */
/********************************************************************************/
        AcquireBUFRData(&rdi,sFileName);
 
/********************************************************************************/
/*                      HVFE Trigger to create bufr message                     */
/********************************************************************************/
        CreateRawDataBUFR(&ucBufrMessage, bufrSize, sFileName, &rdi);
 
        free(bufrSize);
        bufrSize = NULL;
        free(ucBufrMessage);
        ucBufrMessage = NULL;
 
        msend();
        return 0;
}
 
void AcquireBUFRData(RawDataInterface * rdi, char * sFileName)
{
        /* Radarin adi */
        char sRadarName[4];
        /* Radar tarih,saat bilgisi. */
        char sYear[3];
        char sMonth[3];
        char sDay[3];
        char sHour[3];
        char sMinute[3];
        char sSecond[3];
        strcpy(rdi->sRawDataFileName,sFileName);
        rdi->nFileNameCharLength = strlen(sFileName);
        strcpy(rdi->sRawDataFilePath,"data/");
        rdi->nFilePathCharLength = strlen("data/");
        /* Radar Name */
        strncpy(sRadarName,sFileName,3);
        sRadarName[3]='\0';
        strncpy(sYear,sFileName+3,2);
        sYear[2]='\0';
        strncpy(sMonth,sFileName+5,2);
        sMonth[2]='\0';
        strncpy(sDay,sFileName+7,2);
        sDay[2]='\0';
        strncpy(sHour,sFileName+9,2);
        sHour[2]='\0';
        strncpy(sMinute,sFileName+11,2);
        sMinute[2]='\0';
        strncpy(sSecond,sFileName+13,2);
        sSecond[2]='\0';
        /* ASSIGNMENT TO STRUCTURE */
        strcpy(rdi->sIdentName,sRadarName);
        rdi->nIdentCharLength = strlen(sRadarName);
        rdi->nYear = atoi(sYear);
        rdi->nMonth = atoi(sMonth);
        rdi->nDay = atoi(sDay);
        rdi->nHour = atoi(sHour);
        rdi->nMinute = atoi(sMinute);
        rdi->nSecond = atoi(sSecond);
        strcpy(rdi->sDataDescriptorIdentifier,"049192");
        rdi->sDataDescriptorIdentifier[6] = '\0';
        strcpy(rdi->sDataDescriptorRawData,"049193");
        rdi->sDataDescriptorRawData[6] = '\0';
}

CreateRawDataBUFR :

void CreateRawDataBUFR(unsigned char ** ucppBUFR, int * bufrSize, RawDataInterface * rdi)
{
        MAIN_DATA MainData;
        /* Tam Dosya yolu */
        char sRawFilePath[120];
        char sTableBName[23];
        char sTableCName[23];
        char sTableNamesExtension[18];
        char sTableNamesExtensionAfterDot[4];
........

Recommended Answers

All 8 Replies

Where did i do wrong?

I don't know. It looks like CreateRawDataBUFR is corrupting your object, but you didn't post the logic for CreateRawDataBUFR. Without the code I can't even guess where you went wrong.

void CreateRawDataBUFR(unsigned char ** ucppBUFR, int * bufrSize, RawDataInterface * rdi)
{
        MAIN_DATA MainData;
        /* Tam Dosya yolu */
        char sRawFilePath[120];
        char sTableBName[23];
        char sTableCName[23];
        char sTableNamesExtension[18];
        char sTableNamesExtensionAfterDot[4];
        int nDDWOR;
        int nDDWR;
        int nVWOR;
        int nAdditiveBufrBitCount = 0;
        int nVWR;
        long lBufrSize;
        char sPieceFileName[10];
        char sPieceBufrFileName[12];
        char sPieceFileNameNumber[3];
        int nMaxBufrMessageSize = 16777215;
        /* int nMaxBufrMessageSize = 99; */
        /* int nMaxBufrMessageSize = 1600; */
        int nMoreBufrMessageFileSize = 31;
        InitializeCharArray(sPieceFileName,10);
        InitializeCharArray(sPieceBufrFileName,12);
        InitializeCharArray(sPieceFileNameNumber,3);
        /* int * nFileSize = malloc(4); */
        /* int * nAdditiveBulkDataSize = malloc(4); */
        int i=0;
 
        InitializeCharArray(sTableBName,23);
        InitializeCharArray(sTableCName,23);
        InitializeCharArray(sTableNamesExtension,18);
        InitializeCharArray(sTableNamesExtensionAfterDot,4);
        strcpy(sTableNamesExtensionAfterDot,"TXT");
        /* MainData initialization */
        InitializeMainData(&MainData);
        InitializeCharArray(sRawFilePath,120);
        /* *nAdditiveBulkDataSize = 0; */
/********************************************************/
        CopyRawDataInterface(&MainData,&rdi);
/********************************************************/
        GetRdbType(&MainData.nRdbType,MainData.rdi.sIdentName);
        GetRdbSubtype(&MainData.nRdbSubtype,MainData.rdi.sIdentName);
        strcpy(sRawFilePath,MainData.rdi.sRawDataFilePath);
        sRawFilePath[strlen(sRawFilePath)] = '\0';
        strcpy(MainData.sFullFilePath,sRawFilePath);
        strcat(MainData.sFullFilePath,MainData.rdi.sRawDataFileName);
        MainData.sFullFilePath[strlen(MainData.sFullFilePath)] = '\0';
        printf("Aranacak dosya yolu : %s\n",MainData.sFullFilePath);
        printf("(HVFE.c) MainData.sFullFilePath(%d) = %s\n",strlen(MainData.sFullFilePath), MainData.sFullFilePath);
        /* File Size hesaplaniyor */
        CalculateFileSize1(MainData.sFullFilePath,&MainData.nFileSize);
        printf("(HVFE.c) MainData.nFileSize = %d\n",MainData.nFileSize);
        /* Creating B,C reference tables */
        CreateTableNamesExtension1(sTableNamesExtension);
        CreateTableBName1(sTableBName,sTableNamesExtension,sTableNamesExtensionAfterDot);
        CreateTableCName1(sTableCName,sTableNamesExtension,sTableNamesExtensionAfterDot);
        printf("(HVFE.c) sTableBName = %s\n",sTableBName);
        printf("(HVFE.c) sTableCName = %s\n",sTableCName);
        SetDataDescriptors(&MainData);
        printf("(HVFE.c) MainData.saDataDescriptor[0,1]=%s(%d),%s(%d)\n",MainData.saDataDescriptor[0],strlen(MainData.saDataDescriptor[0]),MainData.saDataDescriptor[1],strlen(MainData.saDataDescriptor[1]));
        SetTableElements(&MainData, sTableBName, sTableCName);
 
        CalculateChunkCount1(&MainData.nChunkCount,MainData.nFileSize,MainData.nMaxSize);
/*
        printf("md->nFileSize = %d\n",MainData.nFileSize);
        printf("md->nMaxSize = %d\n",MainData.nMaxSize);
        printf("md->nAddditiveChunkSize = %d\n",MainData.nAdditiveChunkSize);
        printf("md->nChunkCount = %d\n", MainData.nChunkCount);
*/
 
/*
        for(i=0;i<2;i++)
        printf("(HVFE.c) MainData.tBElem[%d] = %s,%s,%s,%s(%d),%s(%d),%s(%d)\n",i,MainData.tBElem[i].sDataDescriptor,MainData.tBElem[i].sElementName,MainData.tBElem[i].sElementType,MainData.tBElem[i].sExp,MainData.tBElem[i].nExp,MainData.tBElem[i].sMinus,MainData.tBElem[i].nMinus,MainData.tBElem[i].sBitsToRead,MainData.tBElem[i].nBitsToRead);
*/
        CalculateDataDescriptorByteSizeOtherThanRawData(&MainData,&nDDWOR);
        printf("nDDWOR = %d\n", nDDWOR);
        CalculateDataDescriptorByteSizeOfRawData(&MainData,&nDDWR);
        printf("nDDWR = %d\n", nDDWR);
        CalculateValueByteSizeOtherThanRawData(&MainData,&nVWOR);
        MainData.nValueByteSizeOtherThanRawData = nVWOR;
        printf("nVWOR = %d\n", nVWOR);
        printf("nAdditiveBufrBitCount = %d\n",nAdditiveBufrBitCount);
        CalculateValueByteSizeOfRawData(&MainData, &nVWR, MainData.nChunkCount);
        printf("nVWR = %d\n", nVWR);
        CalculateBufrSize(&lBufrSize,&MainData,nDDWOR,nVWOR,nDDWR,nVWR,nAdditiveBufrBitCount);
        printf("lbufrSize = %ld\n",lBufrSize);
/*
*/
        if(lBufrSize > nMaxBufrMessageSize)
        {
                /* Dosya parcalara bolunecek. */
                DivideFileIntoPieces(&MainData,lBufrSize,nMaxBufrMessageSize);
                printf("(HVFE.c) Total BUFR Message Count = %d\n",MainData.nTotalBufrMessageCount);
                for(i=0; i<MainData.nTotalBufrMessageCount; i++)
                {
                        strcat(sPieceFileName,"raw");
                        sprintf(sPieceFileNameNumber,"%d",(i+1));
                        strcat(sPieceFileName,sPieceFileNameNumber);
                        strcat(sPieceFileName,".dat");
                        strcpy(MainData.sFullFilePath,sPieceFileName);
                        CalculateFileSize1(MainData.sFullFilePath,&MainData.nFileSize);
                        CalculateChunkCount1(&MainData.nChunkCount,MainData.nFileSize,MainData.nMaxSize);
                        CalculateAdditive1(MainData.nFileSize,MainData.nMaxSize,&MainData.nAdditiveChunkSize);
 
                        CreateHVFEMachineBUFR(ucppBUFR,bufrSize,&MainData);
                        nMoreBufrMessageFileSize = nMoreBufrMessageFileSize + *bufrSize;
                        remove(sPieceFileName);
                        InitializeCharArray(sPieceFileName,10);
                        strcat(sPieceBufrFileName,"bufr");
                        sprintf(sPieceFileNameNumber,"%d",(i+1));
                        strcat(sPieceBufrFileName,sPieceFileNameNumber);
                        strcat(sPieceBufrFileName,".bufr");
                        printf("sPieceBufrFileName = %s\n",sPieceBufrFileName);
                        WriteBinaryDataToFile(*ucppBUFR,*bufrSize,sPieceBufrFileName);
                        InitializeCharArray(sPieceBufrFileName,12);
                }
                AddBufrMessagesTogether(&MainData);
                /* WriteBUFRToReportsDB2(nMoreBufrMessageFileSize); */
        }
        else
        {
                printf("(HVFE.c) Total BUFR Message Count = 1\n");
                CreateHVFEMachineBUFR(ucppBUFR, bufrSize, &MainData);
                WriteBinaryDataToFile(*ucppBUFR,*bufrSize,"total.bufr");
                /* WriteBUFRToReportsDB(*ucppBUFR,*bufrSize); */
        }
 
}

Set a soft break at CreateRawDataBUFR then a watchpoint at rdi, or set the same softbreak and a watchpoint to the location where rdi is stored and another at the contents that rdi points too. Then by single stepping see which changes at what point int the subroutine.

One thing that may cause that behavior is if the two functions are in different program units (*.c files) or libraries and compiled with different packing factors. Another cause is two functions in different program units using different versions of the same structure. Check your computer's file system to see if it has more than one copy of the header file in which the structure is defined. (Ben there, done that :) ).

Another debugging hint: write a quick lump of code to dump the structure contents, say something like this.

void show(const RawDataInterface *rdi)
{
   printf("rdi->sIdentName[10]               = %s\n", rdi->sIdentName);
   printf("rdi->nIdentCharLength             = %d\n", rdi->nIdentCharLength);
   printf("rdi->sRawDataFilePath[200]        = %s\n", rdi->sRawDataFilePath);
   printf("rdi->nFilePathCharLength          = %d\n", rdi->nFilePathCharLength);
   printf("rdi->sRawDataFileName[200]        = %s\n", rdi->sRawDataFileName);
   printf("rdi->nFileNameCharLength          = %d\n", rdi->nFileNameCharLength);
   printf("rdi->nYear                        = %d\n", rdi->nYear);
   printf("rdi->nMonth                       = %d\n", rdi->nMonth);
   printf("rdi->nDay                         = %d\n", rdi->nDay);
   printf("rdi->nHour                        = %d\n", rdi->nHour);
   printf("rdi->nMinute                      = %d\n", rdi->nMinute);
   printf("rdi->nSecond                      = %d\n", rdi->nSecond);
   printf("rdi->sDataDescriptorIdentifier[7] = %s\n", rdi->sDataDescriptorIdentifier);
   printf("rdi->sDataDescriptorRawData[7]    = %s\n", rdi->sDataDescriptorRawData);
}

Sprinkle calls to this function inside of CreateRawDataBUFR. Then you can narrow down to a bit of code that causes the contents of the structure to be changed. Continue to do so until you find the culprit.

[edit]Also, if CreateRawDataBUFR is not supposed to modify the contents of the struct, consider making the first parameter a const RawDataInterface *rdi . Corresponding changes may echo through other code, but it may help the compiler point out where such an issue lies.

commented: Thanks for the tip about debugging +3

CreateRawDataBUFR function takes 3 parameters, but i send it 4. I saw it in my dream today in the morning :)

Thanx everyone.

So how come your compiler didn't warn you of the mis-match?

Put the prototype in a header file, and include that file where you define the function, and wherever you use the function. Then you will get a warning if any declaration or use is mis-matched.

Simply writing extern void * CreateRawDataBUFR.... in the C file where you use it offers no prospect for error checking.

So how come your compiler didn't warn you of the mis-match?

Because CreateRawDataBUFR function is in a library, in the Makefile there is a linker flag.

Put the prototype in a header file, and include that file where you define the function, and wherever you use the function. Then you will get a warning if any declaration or use is mis-matched.

Simply writing extern void * CreateRawDataBUFR.... in the C file where you use it offers no prospect for error checking.

And thank you so much, I'll do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.