-1

I'm programming atmega8535 using C. I want to save data into flash disk using ALFAT OEM module. But, I have problem because the data that I want to save change into other variable in the middle program (Data saving is success, but the data is wrong). It occurs after malloc. I already malloc the variable data. I'm using hyperterminal to debugging my program

This is my code. I only show that related

// Declare your global variables here
char* reply = NULL;
char* directory = NULL;
char* fileName = NULL;
char* getFileName = NULL;

void writeCommand(char* command){ //to give command to ALFAT
    //not related
}

void readCommand(){ //to request reply from ALFAT
    //related (because contains malloc and also change my variable) but I give another example
}

void get_ErrorCode(char errorCode[4]){ //to get errorCode from ALFAT's reply
    //not related
}

void get_Version(){ //to know ALFAT's version
    //not related
}

void mountUSB0(){ //to mount USB port 0
    //not related
}

void mountUSB1(){ //to mount USB port 1
    //not related
}

void get_fileName(){ //to get fileName from ALFAT's reply after N command
    //not related
}

int check_File(char port[1]){  //to check whether file already exists or not
    //related (because contains malloc and also change my variable) but I give another example
}

void separate_Directory(char* fullDir, char* data){ //to separate directory and fileName from fullDirectory "fullDir"
    int i,j;
    int numSlash = 0;               //numberOfSlash
    int curNumSlash = 0;            //currentNumberOfSlash

    //CHECK THE DATA BEFORE MALLOC
    printf("1st GUNYUH data = %s, address data = %x, directory = %s, address directory = %x\n",data,data,directory,directory);

    //count backslash '\'=0x5C
    for (i=0;i<strlen(fullDir);i++){
        if(fullDir[i]== 0x5C ) numSlash++;
    }

    //count number of char for directory
    i=0;
    curNumSlash = 0;
    while (curNumSlash != numSlash){
        if(fullDir[i]== 0x5C) curNumSlash++;
        i++;
    }

    //i = number of char for directory
    //number of char for filename = strlen(fullDir)-total char directory
    do{
        directory = (char *) malloc (i+1);
    }while(directory==NULL);
    do{
        fileName = (char *) malloc (strlen(fullDir)-i+1);
    }while(fileName==NULL);

    //CHECK THE DATA AFTER MALLOC (ALREADY CHANGED)
    printf("2nd GUNYUH data = %s, address data = %x, directory = %s, address directory = %x\n",data,data,directory,directory);

    //save into directory until last backslash
    i=0;
    curNumSlash = 0;
    while (curNumSlash != numSlash){
        if(fullDir[i]== 0x5C) curNumSlash++;
        directory[i] = fullDir[i];
        i++;
    }
    directory[i] = '\0';

    //remaining fullDir into fileName
    j=0;
    while (i < strlen(fullDir)){
        fileName[j] = fullDir[i];
        i++;
        j++;    
    } 
    fileName[j] = '\0';

    //CHECK THE DATA AGAIN (CHANGED INTO directory)
    printf("3rd GUNYUH data = %s, address data = %x, directory = %s, address directory = %x\n",data,data,directory,directory);
    printf("separate directory = %s, fileName = %s, fullDir = %s\n",directory,fileName,fullDir);
}


void writeData (char* data, char* fullDir, char port[1], char statFileHandler[16]){
//I omit that not related

    printf("1)!!!!!!!!!!!!!!!!DATA = %s, ADDRESS DATA = %x, DIRECTORY = %s, ADDRESS DIRECTORY = %x\n",data,*data,directory,*directory);
    separate_Directory(fullDir,data);
    printf("2)!!!!!!!!!!!!!!!!DATA = %s, ADDRESS DATA = %x, DIRECTORY = %s, ADDRESS DIRECTORY = %x\n",data,*data,directory,*directory);

//omitted
}

void main(){
    char* data;
    char* fullDir = NULL;
    char port[1]="";
    char statFileHandler[16];

    //omitted

    while(1){
         //omitted (also omit the mounting)


         do{
             data = (char *) malloc (strlen("meong")+1);   //+1 utk \0 
         }while(data==NULL);
         strcpy(data,"meong");
         data[strlen("meong")] = '\0';

         fullDir = (char *) malloc (strlen("\\f1\\nyan.txt")+1);
         strcpy(fullDir,"\\f1\\nyan.txt");
         fullDir[strlen("\\f1\\nyan.txt")] = '\0';
         for(i=0;i<strlen("\\f1\\nyan.txt");i++){
             fullDir[i] = toupper(fullDir[i]);
         } 

         //omit some printf for debugging
         printf("fullDir di main= %s\n",fullDir);   

         printf("data di main = %s\n",data);
         printf("address data di main = %x\n",*data);
         writeData (data, fullDir, port, statFileHandler);

         break;                        
      }

      while(1){}

      }
   }
}

Check the GUNYUH part. The output in HyperTerminal:

1st GUNYUH data = meong, address data = 196, directory = , address directory = 0
2nd GUNYUH data = , addressdata = 196, directory = , address directory = 196
3rd GUNYUH data = \F1\, address data = 196, directory = \F1\, address directory = 196

my data in main is "meong".

1st GUNYUH before malloc, the data still "meong"

2nd GUNYUH after malloc, the data already changed

3rd GUNYUH after defined the directory, the data also changed. (Well because the address also same so it point to the same address)

why it changed?

Is it because lack of memory problem? But, when there's no enough heap memory, the malloc will return NULL so it never go out from the loop. I already experienced the lack of heap memory before and it did can't go out from the loop.

I have also experience overlapping like this. But it is because I didn't use malloc. (but I didn't check the address and go for static array but not enough memory so back into dynamic and found that it need malloc)

some help please?

Kalkaneus
  • 39
  • 7

1 Answers1

1

This is not an answer but it is too big for comments.

You have the following bugs:

  • In four different printf lines you cause undefined behaviour by passing null pointer for %s. (the variable directory). After undefined behaviour has begun, all bets are off.
  • Printing a pointer with %x causes undefined behaviour. To print a pointer use %p and cast the pointer to (void *) .
  • You do *THING instead of THING in 3 different places, for printf
  • Don't cast malloc, the cast may be hiding an error message indicating a bug
  • On the line for(i=0;i<strlen("\\f1\\nyan.txt");i++){ , i is undeclared.
  • You failed to include stdio.h, stdlib.h string.h and ctype.h .
  • There are two more } than { in your code.
  • After the line with fullDir = (char *) malloc... , you do not check to see whether malloc failed.

This code should not compile. This leads me to believe that you are not posting your real code. It is important that you post exactly the code that is failing.

You need to create a minimal program, test that that program still shows the problem, and post the code of that program unaltered.

This is because there could be problems that are in the "real code" but not in the code you posted. Since you don't know where the problem is, you can't be sure that you have included the part that causes the problem.

In fact, if I fix all the bugs listed above and remove the infinite loop at the end of main(), your code compiles and runs successfully for me. That suggests that either one of the listed points is the problem, or the problem is in code that you didn't post.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • I'm posting my code. But I omit that I thought not related. May be some of {} accidently omitted. Include also omitted. And maybe some variable omitted. I already edit my code so they aren't *thing but thing. And also I already edit my result. All of program is about 800++ lines. I think it too much to post it here. But, you are right that the problem not lies there. Just recently (finally), I tried to NULL every variable that I free(). And my code working. Thanks for answering. =D – Kalkaneus Jul 15 '14 at 11:09
  • You need to post your REAL code, otherwise we might not be able to help. What seems to be unrelated to problem for you might even be the cause of problem. – zoska Jul 15 '14 at 11:56
  • I'm sorry. First I thought REAL code is different than ALL code. It seems ALL code is REAL code. – Kalkaneus Jul 15 '14 at 12:05
  • well... I just want to edit to add all of my code. But it seems the body has character limitation. – Kalkaneus Jul 15 '14 at 12:13
  • don't post ALL code. Delete all lines of code that are not relevant. **Then check the program still compiles and the problem still happens after deleting those lines**. Then post. – M.M Jul 15 '14 at 22:11