RadarData * rd = NULL;
 
rd = GetRadarData(sFileName);
/* 
                     Here the debugger says that :
                     rdp->llBdContent.bdContent.next = 0xa13f080.
                     This is ok,but...
*/
 
        CreateHVFERadarBUFR(&rd);

GetRadarData is here:

RadarData * GetRadarData(char * sFileName)   
{                                                                                        
 
        short int nRadarSubtypeInFile;
        short int nRadarTypeInFile; 
 
        /* Radar Data Structure */       
        RadarData rd;
 
        /* Radarin adi */
        char * sRadarName =  (char *) malloc(4);                                         
 
        /* Radar tarih,saat bilgisi. */                                                  
        char * sYear = (char *) malloc(2);    
        char * sMonth = (char *) malloc(2);
        char * sDay = (char *) malloc(2);     
        char * sHour = (char *) malloc(2);      
        char * sMinute = (char *) malloc(2);      
        char * sSecond = (char *) malloc(2);      
 
        rd.sRadarName = (char *) malloc(4);         
 
        rd.llBdContent.bdContent.data = (char *) malloc(MAXSIZE);
        rd.llBdContent.bdContent.next = NULL;         
 
        /* Radar Name */                                                                 
        sRadarName = strncpy(sRadarName,sFileName,3);
        sYear = strncpy(sYear,sFileName+3,2);
        sMonth = strncpy(sMonth,sFileName+5,2);
        sDay = strncpy(sDay,sFileName+7,2);
        sHour = strncpy(sHour,sFileName+9,2);
        sMinute = strncpy(sMinute,sFileName+11,2);
        sSecond = strncpy(sSecond,sFileName+13,2);
 
        /* ASSIGNMENT TO STRUCTURE */
        rd.sRadarName = strcpy(rd.sRadarName,sRadarName);
        rd.sRadarName = strcat(rd.sRadarName,"1");
 
        rd.ddYearMonthDay.nYear = atoi(sYear);
        rd.ddYearMonthDay.nMonth = atoi(sMonth);
        rd.ddYearMonthDay.nDay = atoi(sDay);
        rd.dtHourMinuteSecond.nHour = atoi(sHour);
        rd.dtHourMinuteSecond.nMinute = atoi(sMinute);
        rd.dtHourMinuteSecond.nSecond = atoi(sSecond);
        nRadarSubtypeInFile = GetRdbSubtype(rd.sRadarName);
        rd.nRdbSubtype = nRadarSubtypeInFile;
        nRadarTypeInFile = GetRdbType(rd.sRadarName);
        rd.nRdbType = nRadarTypeInFile;
        free(sRadarName);
        sRadarName = NULL;
        free(sYear);
        sYear = NULL;
        free(sMonth);
        sMonth = NULL;
        free(sDay);
        sDay = NULL;
        free(sHour);
        sHour = NULL;
        free(sMinute);
        sMinute = NULL;
        free(sSecond);
        sSecond = NULL;
        /* Bulk Data buradan itibaren okunup, long size, char data[] bilgisi giriliyor. */
        SetBulkData(&rd,sFileName);
        return &rd;
}
 
void SetBulkData(RadarData * rd, char * sFileName)
{
        int nFileSize;
 LLBulkData * llBulkDataDummy = NULL;
        LLBulkData * llBulkDataDummy2 = NULL;
        BulkData * bdDummy = NULL;
        int nMaxByteCount = MAXSIZE * 1000000;
        double dBulkDataCount;
        int nLastBulkDataSize;
        int nBulkDataCount = 0;
        int nDummyFileSize = 0;
        /* Tam Dosya yolu */
        char * sRawFilePath =  (char *) malloc(120);
        /* fopen source */
        char * sFile =  (char *) malloc(200);
        int i;
        FILE * DataFile = NULL;
        char * caFileContent = (char *) malloc(nMaxByteCount);
        char * sDummyFileContentFirst = (char *) malloc(MAXSIZE);
        char * sDummyFileContent = NULL;
        int nAdditiveBulk = 0;
        sRawFilePath = strcpy(sRawFilePath,"data/");
        sFile = strcat(sRawFilePath,sFileName);
        DataFile = fopen(sFile,"rb");
        if(DataFile == NULL)
                printf("DataFile NULL\n");
        nFileSize = fread(caFileContent,sizeof(char),nMaxByteCount,DataFile);
        nDummyFileSize = nFileSize;
fclose(DataFile);
        dBulkDataCount = nDummyFileSize / MAXSIZE;
        nBulkDataCount = (int) dBulkDataCount;
        if(nDummyFileSize % MAXSIZE > 0)
                nBulkDataCount ++ ;
        nLastBulkDataSize = nMaxByteCount % MAXSIZE;
        for(i=0; i<nBulkDataCount; i++)
        {
                if(i==0)
                {
                        llBulkDataDummy = &rd->llBdContent;
                        memcpy(sDummyFileContentFirst,caFileContent,MAXSIZE);
                        bdDummy = (BulkData *) CreateNewBulkDataNode();
                        memcpy(bdDummy->data,sDummyFileContentFirst,MAXSIZE);
                        AssignRootOfLinkedList(&llBulkDataDummy,&bdDummy);
                        llBulkDataDummy->size = 1;
                        nDummyFileSize = nDummyFileSize - MAXSIZE;
                        free(sDummyFileContentFirst);
                        sDummyFileContentFirst = NULL;
                }
                else
                {
                        sDummyFileContent = (char *) EraseCopiedBytes(caFileContent+i*MAXSIZE, nDummyFileSize);
                        if(sDummyFileContent == NULL)
                                break;
                        bdDummy = (BulkData *) CreateNewBulkDataNode();
                        memcpy(bdDummy->data,sDummyFileContent,MAXSIZE);
                        AddBulkDataToLinkedList(&llBulkDataDummy,bdDummy);
                        free(bdDummy);
bdDummy = NULL;
                        nDummyFileSize = nDummyFileSize - MAXSIZE;
                        free(sDummyFileContent);
                        sDummyFileContent = NULL;
                        sDummyFileContent = realloc(sDummyFileContent,nDummyFileSize-MAXSIZE);
                }
        }
        nAdditiveBulk = nDummyFileSize;
        printf("nAdditiveBulk = %d\n",nAdditiveBulk);
        if(nLastBulkDataSize > 0)
        {
                bdDummy = (BulkData *) CreateNewBulkDataNode();
                memcpy(bdDummy->data,sDummyFileContent,nLastBulkDataSize);
                realloc(bdDummy->data,nAdditiveBulk);
        }
        free(sFile);
        sFile = NULL;
        free(caFileContent);
        caFileContent = NULL;
        PrintLinkedList(&llBulkDataDummy);
}
 
void AssignRootOfLinkedList(LLBulkData ** llBulkDest, BulkData ** bdSource)
{
        LLBulkData * llBulkDummy = NULL;
        BulkData * bdDummy = NULL;
        BulkData * bdDummy2 = NULL;
        llBulkDummy = *llBulkDest;
        bdDummy = &llBulkDummy->bdContent;
        bdDummy2 = *bdSource;
        bdDummy2->next = NULL;
 
        bdDummy->next = *bdSource;
        *bdSource = NULL;
        llBulkDummy = NULL;
        bdDummy = NULL;
        bdDummy2 = NULL;
}
void AddBulkDataToLinkedList(LLBulkData ** llBulk, BulkData * bdNode)
{
        LLBulkData * llBulkDummy = NULL;
        int i;
        int nCounter = 0;
        BulkData * bdDummy = NULL;
        BulkData * bdWaitingToBeCopied = (BulkData *) malloc(16);
        bdWaitingToBeCopied->data = (char *) malloc(MAXSIZE);
        bdWaitingToBeCopied->next = NULL;
        llBulkDummy = *llBulk;
        memcpy(bdWaitingToBeCopied,bdNode,sizeof(bdNode));
        printf("(Radar.c)(AddBulkDataToLinkedList) evet1\n");
                for(bdDummy = llBulkDummy->bdContent.next; bdDummy->next != NULL; bdDummy = bdDummy->next)
                {
                        nCounter ++;
                }
                bdDummy->next = bdWaitingToBeCopied;
        bdWaitingToBeCopied = NULL;
        bdDummy = NULL;
        printf("(Radar.c)(AddBulkDataToLinkedList) evet2\n");
        llBulkDummy->size++;
}
 
char * EraseCopiedBytes(char * caFileContent, int nSize)
{
        int i;
        int nNewSize = nSize;
        if(nNewSize == 0)
                return NULL;
        char * caFileContentNew = (char *) malloc(nNewSize);
        memcpy(caFileContentNew,caFileContent,nNewSize);
        return caFileContentNew;
}
 
BulkData * CreateNewBulkDataNode()
{
        BulkData * bdDummy = (BulkData *) malloc(sizeof(BulkData));
        bdDummy->data = (char *) malloc(MAXSIZE);
        bdDummy->next = NULL;
        return bdDummy;
}
 
LLBulkData * CreateBulkDataLinkedList()
{
        LLBulkData * llBdDummy = (LLBulkData *) malloc(sizeof(LLBulkData));
        llBdDummy->bdContent.next = NULL;
        llBdDummy->size = 0;
        return llBdDummy;
}

CreateHVFERadarBUFR function:

void CreateHVFERadarBUFR(RadarData ** rd)
{
 
        RadarData * rdp = * rd; 
        /* 
                     Here the debugger says that :
                     rdp->llBdContent.bdContent.next = 0x40a13f080.
        */
 
        BulkData * bdDummy = NULL;
 
        bdDummy = rdp->llBdContent.bdContent.next;
        printf("%s\n",rdp>llBdContent.bdContent.next->data);
        /* So I get segmentation fault here 
            Cannot access memory at address 0x40a13f080
        */
}

Structures:

typedef struct bulk_data
{
        char * data;
        struct bulk_data * next;
}BulkData;
 
typedef struct ll_bulk_data
{
        long size;
        BulkData bdContent;
}LLBulkData;
 
typedef struct radar_data
{
        char * sRadarName;
        DataDate ddYearMonthDay;
        DataTime dtHourMinuteSecond;
        LLBulkData llBdContent;
        short int nRdbType;
        short int nRdbSubtype;
}RadarData;

Why does the address of BulkData structure in the RadarData change from "0xa13f080" to "0x40a13f080" ?
Thank you.

Recommended Answers

All 3 Replies

I had no time to look at your data very thoroughly, so all i can say is that all that compiler has to do is to guarantee that your program works as it is written, ie all references refer to the right objects, the memory can be reallocated if this would not make the pointers and references incorrect. Where this may cause problems is in accessing the memory by another process, which again means that using shared memory is not always the right way for the interprocess communication, it may be better to use a simple server to manage shared information.

Your code is a catalogue of disasters! I'm amazed you've got this far to be honest.

I'm sure not going to catch them all here, but rest assured this is like shooting fish in a barrel.

Buffer problems

char * sYear = (char *) malloc(2);    
    sYear = strncpy(sYear,sFileName+3,2);

1. Do not cast malloc in C, it does nothing useful when the code is correct, and hides problems when the code is wrong.
2. You don't allocate space for the \0 to terminate the string.
3. Even if you did allocate the space, strncpy() would not necessarily add a \0 for you.
4. Given the use of these short strings, you would have been much better off simply declaring char sYear[3]; and saving yourself a bunch of malloc/free calls. I'm guessing you switched to malloc/free because you kept trashing your stack with buffer overruns.

Pointers to locals return &rd; As soon as the function ends, the stack space occupied by the local variable is reclaimed, and thus your pointer is no longer valid.
gcc with high enough warning levels would tell you that much.

Ignoring errors

DataFile = fopen(sFile,"rb");
        if(DataFile == NULL)
                printf("DataFile NULL\n");

Having detected the problem, you carry on regardless rather than exiting.

Useless casting bdDummy = (BulkData *) CreateNewBulkDataNode(); I'm guessing you don't prototype anything, otherwise there would be no possible reason to cast the result to the type that it already is.

Mis-use of realloc

free(sDummyFileContent);
sDummyFileContent = NULL;
sDummyFileContent = realloc(sDummyFileContent,nDummyFileSize-MAXSIZE);

Why don't you just call malloc here and be done with it?

Besides, if you were really wanting to call realloc, you need to use another variable

void *temp = realloc ( oldmem, newsize );
if ( temp != NULL ) {
  // oldmem has gone, and temp is the new pointer
  oldmem = temp;
} else {
  // realloc failed, but oldmem is still allocated
}

If you just do oldmem = realloc( oldmem, newsize ); then you risk losing everything if realloc fails.

Your code is a catalogue of disasters! I'm amazed you've got this far to be honest.

I'm sure not going to catch them all here, but rest assured this is like shooting fish in a barrel.

Buffer problems

char * sYear = (char *) malloc(2); 
sYear = strncpy(sYear,sFileName+3,2);

1. Do not cast malloc in C, it does nothing useful when the code is correct, and hides problems when the code is wrong.
2. You don't allocate space for the \0 to terminate the string.
3. Even if you did allocate the space, strncpy() would not necessarily add a \0 for you.
4. Given the use of these short strings, you would have been much better off simply declaring char sYear[3]; and saving yourself a bunch of malloc/free calls. I'm guessing you switched to malloc/free because you kept trashing your stack with buffer overruns.

Pointers to locals return &rd; As soon as the function ends, the stack space occupied by the local variable is reclaimed, and thus your pointer is no longer valid.
gcc with high enough warning levels would tell you that much.

Ignoring errors

DataFile = fopen(sFile,"rb");
if(DataFile == NULL)
printf("DataFile NULL\n");

Having detected the problem, you carry on regardless rather than exiting.

Useless casting bdDummy = (BulkData *) CreateNewBulkDataNode(); I'm guessing you don't prototype anything, otherwise there would be no possible reason to cast the result to the type that it already is.

Mis-use of realloc

free(sDummyFileContent);
sDummyFileContent = NULL;
sDummyFileContent = realloc(sDummyFileContent,nDummyFileSize-MAXSIZE);

Why don't you just call malloc here and be done with it?

Besides, if you were really wanting to call realloc, you need to use another variable

void *temp = realloc ( oldmem, newsize );
if ( temp != NULL ) {
  // oldmem has gone, and temp is the new pointer
  oldmem = temp;
} else {
  // realloc failed, but oldmem is still allocated
}

If you just do oldmem = realloc( oldmem, newsize ); then you risk losing everything if realloc fails.

I have updated my code, but stil the address of pointer changes which is returned from bitio_o_close into CreateHVFESectionZero. Further while trying to read vpSec0, i get segmentation fault. In another program, without these lines of code I'm sending, bitio_o_close function works fine with the same parameters. I think I frustrate something in the addresses or flow of statements in these codes. Here's my updated code. Could you plz check it, maybe you can see something wrong :

/* Radar ham datasi oldugu anlasildiktan sonra bu metod cagiriliyor.*/
void GetRadarData(RadarData * rd, char * sFileName)
{
        short int nRadarSubtypeInFile;
        short int nRadarTypeInFile;
        /* 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];
        /* char sFileName[24] = "IST050906114237.RAWADM9";*/
        rd->llBdContent.bdContent.next = NULL;
        /* 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(rd->sRadarName,sRadarName);
        strcat(rd->sRadarName,"1");
        rd->sRadarName[4]='\0';
        rd->ddYearMonthDay.nYear = atoi(sYear);
        rd->ddYearMonthDay.nMonth = atoi(sMonth);
        rd->ddYearMonthDay.nDay = atoi(sDay);
        rd->dtHourMinuteSecond.nHour = atoi(sHour);
        rd->dtHourMinuteSecond.nMinute = atoi(sMinute);
        rd->dtHourMinuteSecond.nSecond = atoi(sSecond);
        nRadarSubtypeInFile = GetRdbSubtype(rd->sRadarName);
        rd->nRdbSubtype = nRadarSubtypeInFile;
        nRadarTypeInFile = GetRdbType(rd->sRadarName);
        rd->nRdbType = nRadarTypeInFile;
        /* Bulk Data buradan itibaren okunup, long size, char data[] bilgisi giriliyor. */
        SetBulkData(&rd,sFileName);
}
 
void SetBulkData(RadarData ** rdp, char * sFileName)
{
        int nFileSize;
        LLBulkData * llBulkDataDummy = NULL;
        LLBulkData * llBulkDataDummy2 = NULL;
        BulkData * bdDummy = NULL;
        /* 3 byte for section4 size (3 byte max integerdan 4 byte cikar) */
        /* int nMaxByteCount = 16777211;  */
        int nMaxByteCount = MAXSIZE * 1000000;
        double dBulkDataCount;
        int nLastBulkDataSize;
        int nBulkDataCount = 0;
        int nDummyFileSize = 0;
 
        /* Tam Dosya yolu */
        char sRawFilePath[120];
        /* fopen source */
        char sFile[200];
        RadarData * rd = * rdp;
        int i;
        FILE * DataFile = NULL;
        char caFileContent[nMaxByteCount];
        char sDummyFileContentFirst[MAXSIZE];
        char * sDummyFileContent = NULL;
        int nAdditiveBulk = 0;
        strcpy(sRawFilePath,"data/");
        sRawFilePath[strlen(sRawFilePath)] = '\0';
        strcpy(sFile,sRawFilePath);
        strcat(sFile,sFileName);
        sFile[strlen(sFile)] = '\0';
        DataFile = fopen(sFile,"rb");
        if(DataFile == NULL)
        {
                printf("DataFile NULL\n");
        }
        else
        {
                printf("(Radar.c) nMaxByteCount = %d\n",nMaxByteCount);
                printf("(Radar.c) MAXSIZE = %d\n",MAXSIZE);
                nFileSize = fread(caFileContent,sizeof(char),nMaxByteCount,DataFile);
                nDummyFileSize = nFileSize;
                fclose(DataFile);
                printf("nFileSize = %d\n",nFileSize);
 
                dBulkDataCount = nDummyFileSize / MAXSIZE;
                printf("(Radar.c) dBulkDataCount = %2.2f\n",dBulkDataCount);
                nBulkDataCount = (int) dBulkDataCount;
                if(nDummyFileSize % MAXSIZE > 0)
                        nBulkDataCount ++ ;
                printf("(Radar.c) nBulkDataCount = %d\n",nBulkDataCount);
                nLastBulkDataSize = nMaxByteCount % MAXSIZE;
                printf("(Radar.c) nLastBulkDataSize = %d\n",nLastBulkDataSize);
                printf("nBulkDataCount = %d\n", nBulkDataCount);
                for(i=0; i<nBulkDataCount; i++)
                {
                        if(i==0)
                        {
                                llBulkDataDummy = &rd->llBdContent;
                                memcpy(sDummyFileContentFirst,caFileContent,MAXSIZE);
                                bdDummy = CreateNewBulkDataNode();
                                memcpy(bdDummy->data,sDummyFileContentFirst,MAXSIZE);
                                AssignRootOfLinkedList(&llBulkDataDummy,&bdDummy);
                                llBulkDataDummy->size = 1;
                                nDummyFileSize = nDummyFileSize - MAXSIZE;
                        }
                        else
                        {
                                sDummyFileContent = EraseCopiedBytes(caFileContent+i*MAXSIZE, nDummyFileSize);
                                if(sDummyFileContent == NULL)
                                        break;
                                bdDummy = CreateNewBulkDataNode();
                                memcpy(bdDummy->data,sDummyFileContent,MAXSIZE);
                                AddBulkDataToLinkedList(&llBulkDataDummy,&bdDummy);
                                free(bdDummy);
                                bdDummy = NULL;
                                nDummyFileSize = nDummyFileSize - MAXSIZE;
                                free(sDummyFileContent);
                                sDummyFileContent = malloc(nDummyFileSize - MAXSIZE);
                        }
                }
                nAdditiveBulk = nDummyFileSize;
                printf("nAdditiveBulk = %d\n",nAdditiveBulk);
                if(nLastBulkDataSize > 0)
                {
                        bdDummy = CreateNewBulkDataNode();
                        memcpy(bdDummy->data,sDummyFileContent,nLastBulkDataSize);
                        realloc(bdDummy->data,nAdditiveBulk);
                }
 
                PrintLinkedList(&llBulkDataDummy);
        }
}
 
void AssignRootOfLinkedList(LLBulkData ** llBulkDest, BulkData ** bdSource)
{
        LLBulkData * llBulkDummy = NULL;
        BulkData * bdDummy = NULL;
        BulkData * bdDummy2 = NULL;
        llBulkDummy = *llBulkDest;
        bdDummy = &llBulkDummy->bdContent;
        bdDummy2 = *bdSource;
        bdDummy2->next = NULL;
        bdDummy->next = *bdSource;
        *bdSource = NULL;
        llBulkDummy = NULL;
        bdDummy = NULL;
        bdDummy2 = NULL;
}
 
void AddBulkDataToLinkedList(LLBulkData ** llBulk, BulkData ** bdpNode)
{
        LLBulkData * llBulkDummy = NULL;
        int i;
        int nCounter = 0;
        BulkData * bdDummy = NULL;
        BulkData * bdNode = * bdpNode;
        BulkData * bdWaitingToBeCopied = malloc(16);
        bdWaitingToBeCopied->next = NULL;
        llBulkDummy = *llBulk;
        memcpy(bdWaitingToBeCopied,bdNode,sizeof(bdNode));
        printf("(Radar.c)(AddBulkDataToLinkedList) evet1\n");
                for(bdDummy = llBulkDummy->bdContent.next; bdDummy->next != NULL; bdDummy = bdDummy->next)
                {
                        nCounter ++;
                }
                bdDummy->next = bdWaitingToBeCopied;
        bdWaitingToBeCopied = NULL;
        bdDummy = NULL;
        printf("(Radar.c)(AddBulkDataToLinkedList) evet2\n");
        llBulkDummy->size++;
}
char * EraseCopiedBytes(char * caFileContent, int nSize)
{
        int i;
        int nNewSize = nSize;
        if(nNewSize == 0)
                return NULL;
        char * caFileContentNew = malloc(nNewSize);
        memcpy(caFileContentNew,caFileContent,nNewSize);
        return caFileContentNew;
}
 
BulkData * CreateNewBulkDataNode()
{
        BulkData * bdDummy =  malloc(sizeof(BulkData));
        bdDummy->next = NULL;
        return bdDummy;
}
 
void AssignBdContentToBdContent(BulkData ** bdDest, BulkData * bdSource)
{
        *bdDest = bdSource;
}
 
void PrintLinkedList(LLBulkData ** llBulk)
{
        BulkData * bdDummy = NULL;
        LLBulkData * llBulkDummy = NULL;
        int i;
        llBulkDummy = *llBulk;
        for(bdDummy = llBulkDummy->bdContent.next; bdDummy != NULL; bdDummy = bdDummy->next)
        {
                for(i=0;i<MAXSIZE;i++)
                        printf("%d,",bdDummy->data[i]);
                printf("\n...\n");
        }
 
        bdDummy = NULL;
        llBulkDummy = NULL;
}
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.