Thread: garbage value

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Post garbage value

    I am writing my code in visual studio 2010 in C language ,

    my data is appending and i am getting garbage values,may be i think its due to buffer. Please explain me about buffer, garbage and memory allocation in C programming .

    I am facing difficulty in understanding buffer and organizing memory .

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'm having difficulty understanding your question. Please explain it to me.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Buffers are just arrays. If you don't initialize its elements then they may contain garbage.
    Exception is that if the array is declared global then it is initialized to all zeros for you.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll need to post the code. It MIGHT be that you are reading and writing data, to the same file, without either having the right data format specified, or an intervening call to set the file pointer correctly, between the reading and the writing of your data. Those are just guesses however. Post the code, and show an example of the input and output.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Post output is appendings and we are getting garbage values. Explain me ?

    Code:
    int main(void)
    {
    
    /*******program to register a device using serial port RS-232 in a local system******************/
    
    ////////////////////declaring variables///////////////////
    unsigned char buff[100]={0}; 
    unsigned char command1[100]="Registration";
    unsigned char command2[100]="Close";
    unsigned char a[8]={0}; 
    unsigned char m[100]={0};
    unsigned char string[32]={0};
    unsigned int i,im,j;
    //int id; 
    ////////////////////declaring variables complete///////////////////
    
    ReadSN(a);//read device serial no.
    DelayMs(200);
    ScrCls();
    
    printf("Open Port....");
    DelayMs(2000);
    ScrCls();
    
    ///////////////////////////Opening of port/////////////////////////////
    if(PortOpen(0,"9600,8,n,1")!=0)     //0=com1,baudrate, 8 data bits,no parity ,1 stop bit.
    {
    printf("Port Open Fail..");
    DelayMs(2000);
    return(0);
    }
    printf("Port Open succes..");
    DelayMs(2000);
    ///////////////////////////Opening of port/////////////////////////////
    
    if(PortSends(0,"New Device Registration",23)!=0) ///////send device id start
    {
    printf("Send Fail..");
    DelayMs(1000);
    return(0);
    }
    printf("Port Send success.."); 
    DelayMs(1000);
    
    ////////////////////////Receive data as Registation ////////////////////////////
    while(1)
    {
    ScrCls();
    if(PortRecvs(0,buff,12,10000)<0)
    {
    ScrCls();
    printf("Receive Fail..");
    DelayMs(1000);
    //return(0);
    }
    
    ScrCls();
    printf("Receive Data:\n%s\n",buff);
    DelayMs(1000);
    ////////////////////////Receive data as Registation completed ////////////////////////////
    
    
    im=0;
    i=0;
    while(im<12) ///////loop for checking Registration command 
    {
    m[im]=buff[i];
    i++;
    im++;
    }
    im=0;
    j=0;
    while(m[im]==command1[im])
    {
    im++;
    j++;
    } ///////loop for completed 
    printf(" j = %d ",j);
    DelayMs(1000);
    
    if(j==100)
    {
    int length = sizeof(a);
    if(PortSends(0,a,length)!=0) ///////send device id start
    {
    printf("Send Fail..");
    DelayMs(1000);
    
    return(0);
    }
    printf("Port Send success.."); ///////send device id complete
    //DelayMs(1000);
    ScrCls();
    printf("Waiting for dat receive..");
    memset(buff,0,sizeof(buff));
    
    ScrCls();
    if(PortRecvs(0,buff,12,10000)<0) /////Receive Registration id starts 
    { 
    ScrCls();
    printf("Receive Fail..");
    DelayMs(1000);
    //return(0);
    }
    ScrCls();
    printf("Receive Registration id:\n%s\n",buff); /////Receive Registration id ends
    DelayMs(1000);
    
    id=open("Registration",O_CREATE); ///////Store Registration Id in "Registration"File starts
    write(id,buff,sizeof(buff));
    if(id>=0)
    {
    printf("Id Success");
    }
    else
    {
    printf("Id Failure");
    }
    DelayMs(2000);
    close(id); ////Store Registration Id in "Registration"File ends
    ScrCls();
    if(PortSends(0,"Registration Completed",22)!=0) ///////send device id start
    {
    printf("Send Fail..");
    DelayMs(1000);
    return(0);
    }
    printf("Port Send success.."); 
    DelayMs(1000);
    }
    
    ScrCls(); //////////////////Receive close command
    if(PortRecvs(0,buff,12,10000)<0)
    {
    ScrCls();
    printf("Receive Fail..");
    DelayMs(1000);
    }
    printf("Receive Data:\n%s\n",buff);
    DelayMs(1000);
    
    j=0; 
    im=0;
    i=0;
    while(im<5) ///////loop for checking Close command 
    {
    m[im]=buff[i];
    i++;
    im++;
    }
    im=0;
    j=0;
    while(m[im]==command2[im])
    {
    im++;
    j++;
    } ///////loop for completed 
    ScrCls();
    printf(" j = %d \n",j);
    DelayMs(1000);
    
    if(j==6)
    {
    break;
    }
    
    }
    PortClose(0);
    ScrCls();
    printf("Registration Completed");
    DelayMs(1000);
    while(1);
    return(0);
    }OTE][/QUOTE]
    Last edited by parvathi; 12-29-2013 at 03:20 AM.

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    4
    Thank u all,please explain me the above code , and also explain me how to avoid overlapping of data and garbage values.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    SourceForge.net: Indentation - cpwiki
    Read it.

    Is this you as well?
    Avoid Buffer Underflow And Overflow - C And C++ | Dream.In.Code

    Anyway, here is your code indented.
    Code:
    int main(void)
    {
      ////////////////////declaring variables///////////////////
      unsigned char buff[100] = { 0 };
      unsigned char command1[100] = "Registration";
      unsigned char command2[100] = "Close";
      unsigned char a[8] = { 0 };
      unsigned char m[100] = { 0 };
      unsigned char string[32] = { 0 };
      unsigned int i, im, j;
      //int id;
      ////////////////////declaring variables complete///////////////////
    
      ScrAttrSet(0);
      ReadSN(a);
      DelayMs(200);
      ScrCls();
    
      Lcdprintf("Open Port....");
      DelayMs(2000);
      ScrCls();
    
      ///////////////////////////Opening of port/////////////////////////////
      if (PortOpen(0, "9600,8,n,1") != 0) {
        Lcdprintf("Port Open Fail..");
        DelayMs(2000);
        return (0);
      }
      Lcdprintf("Port Open succes..");
      DelayMs(2000);
    
      ///////////////////////////Opening of port/////////////////////////////
      if (PortSends(0, "New Device Registration", 23) != 0) ///////send device id start
      {
        Lcdprintf("Send Fail..");
        DelayMs(1000);
        return (0);
      }
      Lcdprintf("Port Send success..");
      DelayMs(1000);
    
      ////////////////////////Receive data as Registation ////////////////////////////
      while (1) {
        ScrCls();
        if (PortRecvs(0, buff, 12, 10000) < 0) {
          ScrCls();
          Lcdprintf("Receive Fail..");
          DelayMs(1000);
          //return(0);
        }
    
        ScrCls();
        Lcdprintf("Receive Data:\n%s\n", buff);
        DelayMs(1000);
    
        ////////////////////////Receive data as Registation completed ////////////////////////////
        im = 0;
        i = 0;
        while (im < 12)             ///////loop for checking Registration command
        {
          m[im] = buff[i];
          i++;
          im++;
        }
        im = 0;
        j = 0;
        while (m[im] == command1[im]) {
          im++;
          j++;
        }                           ///////loop for completed
        Lcdprintf(" j = %d ", j);
        DelayMs(1000);
    
        if (j == 100) {
          int length = sizeof(a);
          if (PortSends(0, a, length) != 0) ///////send device id start
          {
            Lcdprintf("Send Fail..");
            DelayMs(1000);
    
            return (0);
          }
          Lcdprintf("Port Send success.."); ///////send device id complete
          //DelayMs(1000);
          ScrCls();
          Lcdprintf("Waiting for dat receive..");
          memset(buff, 0, sizeof(buff));
    
          ScrCls();
          if (PortRecvs(0, buff, 12, 10000) < 0)  /////Receive Registration id starts
          {
            ScrCls();
            Lcdprintf("Receive Fail..");
            DelayMs(1000);
            //return(0);
          }
          ScrCls();
          Lcdprintf("Receive Registration id:\n%s\n", buff);  /////Receive Registration id ends
          DelayMs(1000);
    
    /*id=open("Registration",O_CREATE); ///////Store Registration Id in "Registration"File starts
    write(id,buff,sizeof(buff));
    if(id>=0)
    {
    Lcdprintf("Id Success");
    }
    else
    {
    Lcdprintf("Id Failure");
    }
    DelayMs(2000);
    close(id); ////Store Registration Id in "Registration"File ends*/
    
          ScrCls();
          if (PortSends(0, "Registration Completed", 22) != 0)  ///////send device id start
          {
            Lcdprintf("Send Fail..");
            DelayMs(1000);
            return (0);
          }
          Lcdprintf("Port Send success..");
          DelayMs(1000);
        }
    
        ScrCls();                   //////////////////Receive close command
        if (PortRecvs(0, buff, 12, 10000) < 0) {
          ScrCls();
          Lcdprintf("Receive Fail..");
          DelayMs(1000);
          //return(0);
        }
        Lcdprintf("Receive Data:\n%s\n", buff);
        DelayMs(1000);
    
        j = 0;
        im = 0;
        i = 0;
        while (im < 5)              ///////loop for checking Close command
        {
          m[im] = buff[i];
          i++;
          im++;
        }
        im = 0;
        j = 0;
        while (m[im] == command2[im]) {
          im++;
          j++;
        }                           ///////loop for completed
        ScrCls();
        Lcdprintf(" j = %d \n", j);
        DelayMs(1000);
    
        if (j == 6) {
          break;
        }
      }
    
      PortClose(0);
      ScrCls();
      Lcdprintf("Registration Completed");
      DelayMs(1000);
      //while(1);
      return (0);
    }

    > memset(buff, 0, sizeof(buff));
    > if (PortRecvs(0, buff, 12, 10000) < 0) /////Receive Registration id starts
    Why do you call memset here?

    > ScrCls(); //////////////////Receive close command
    > if (PortRecvs(0, buff, 12, 10000) < 0)
    But not here?

    Like I said on the other forum, PortRecvs() should return the number of characters written to buff.
    This you can use to place a proper \0 in the correct place in the buffer.

    Finally, read this
    A development process

    Your main is heading for 200+ lines of code. You need to start creating some functions.
    A clue when to do this is when you start copy/pasting code. Instead of blindly pasting it, make it a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Garbage appearing
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-29-2012, 03:21 AM
  2. Garbage appearing
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-27-2012, 02:47 PM
  3. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  4. File Garbage
    By Vicious in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2004, 03:34 PM
  5. Garbage data
    By mart_man00 in forum C Programming
    Replies: 2
    Last Post: 09-13-2003, 03:44 PM

Tags for this Thread