Thread: passing data

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    passing data

    Hi All;

    I have problem with passing the data. Anyone please help!
    I have something like this (I know it's long and need to pay closed attention.) Is there any way to check that the data is passed correctly? Yhank you very much!

    Code:
     
    typedef struct {
      int re;
      int im;
    }CFIXED;
    
    int BIT_REVERSE[4096];
    
    void AccessData(CFIXED* f, int p, int* array)
    {
       // Doing something
    }
    
    void reverse_data(CFIXED* input, int* bit_reverse, int veclength)
    {
       //Doing something
    }
    
    void FillBuffer(unsigned char ware, void *data)
    {
      FILE *file_in;
    
      // Open data file
      file_in = fopen("test_in.dat", "rb");
      if (file_in == NULL)
          printf("error opening file \n");
    
      // Read the data into the buffer data 
      fread(data, sizeof(CFIXED), 4096, file_in);
    
    
      printf("*****Filling the buffer.***** \n");
    
      fclose(file_in);
    }
    
    
    void ProcessAAT(void *data)
    {
       FILE *file_out;
      void *buffer_ptr;
    
      file_out = fopen("data_out.dat", "wb");
      if(file_out == NULL)
         printf("error opening file \n");
      buffer_ptr = data;
    
      AccessData(buffer_ptr, 12, REDUCE);
      reverse_data(buffer_ptr, BIT_REVERSE, 4096);
    
      // write to output file 
      fwrite(data, sizeof(CFIXED), 4096, file_out);
    
      printf("****Running Process AAT ****\n");
    
      fclose(file_out);
    }
    
    
    }
    void Allocate(unsigned char ware, void *buf)
    {
    switch(ware)
            {
            case BBT:
                       FillBuffer(ware, buf);          
                       break;
            case AAT:
                       ProcessAAT(buf);
                       break;
            }
    
    }
    
    void CheckPending(void *data)
    {
    Allocate(ID, data);
    }
    
    void DeAllocate(void)
    {
         // Doing something
    }
    
    void CallFunction(unsigned char ware, void *buf)
    {
    DeAllocate();
    CheckPending(buf);
    Allocate(ware, buf);
    }
    
    int main (void)
    {
    
    void * buffer;
    buffer = malloc(sizeof(CFIXED)*4096);
    
    CallFunction(BBT,buffer);
    CallFunction(AAT, buffer);
    
    }

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    In the first place, don't use "void *" everywhere. That defeats the purpose of prototype checking. It doesn't matter what type of RAM object it is, you may _always_ define it as atleast an "unsigned char *".

    Secondly, don't pass large things by value, pass their address. Much faster program operation. Safer.

    Thirdly, if you have something you want to verify, run a 32-bit CRC on the object and pass that value along with it's address. The receiving algorithm can run a CRC check again and compare its result with the caller's.

    Lastly, you're interested in checking integrity of passed data, yet the first few lines of your code (see main()) fail to even check whether or not the "buffer" RAMblock was successfully allocated... gotta get used to performing that little check with every allocation.

    good luck.
    It is not the spoon that bends, it is you who bends around the spoon.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thanks!
    Would you please show me how to pass the address instead of passing value? Sorry, I don't understand your third thing, would you explain it a little bit more? Thank!
    Last edited by vnrabbit; 11-12-2002 at 02:53 PM.

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    passing by address .v. value
    ---------------------------------------

    Let's first understand that everything resides in memory (RAM). RAM is just consecutive bytes. Every byte resides at a specific position in RAM-- from the first byte to the last byte. That position is that byte's "address".

    When you pass arguments to a function, compilers allow you to pass (generally speaking) the item either by value-- which is to say it copies the whole variable's contents onto the stack and expects the called function to work with that, or by address-- which means you just pass the address of the first byte of the variable's contents.

    Let's say you have a structure:

    Code:
    typedef struct
       {
       char   name[50];                     /* customer name */
       long   hairs;                             /* hairs on head */
       int      fingtoes;                       /* number of fingers & toes */
       }custRec;
    Now, if we count the size of each field, and the alignment padding the compiler adds to align each field, that structure is 58 bytes in length.

    If you pass the structure by value, you pass 58 bytes of data onto the stack-- which is slow to do (in computer time).

    Code:
    custRec   myRecord;
    
    /* stuff info into myRecord */
    
    ProcessCustomer(myRecord);                 /* <-- pass by value */
    However, if you pass the address of the structure's first byte instead, it's much faster, because only 4 bytes need stored on the stack (in other words, a "long word"). Like so:

    Code:
    ProcessCustomer(&myRecord);
    It's operationally faster, more stable because you're doing less stackwork and data manipulation.


    Don't Be a Niller
    ---------------------
    In your main(), you perform an allocation, but never check to see if that allocation was valid-- how do you know malloc() handed you a RAMblock? Maybe it didn't-- here's how to tell:

    Code:
    int main (void)
       {
       char     *buffer;
    
       buffer = 0L;                           /* Nill/Null the variable to known state */
       buffer = (char*)malloc(sizeof(CFIXED)*4096);
    
       if(buffer)                                /* <-- need to ALWAYS check */
          {
          CallFunction(BBT,&buffer);
          CallFunction(AAT,&buffer);
          }
       else
          ErrHandler("OUT OF MEMORY");     /* terminate gracefully */
       }
    See? The above checks to see if the allocation worked. If not, we display an error (that's called exiting "gracefully").
    Last edited by Sayeh; 11-20-2002 at 01:15 PM.
    It is not the spoon that bends, it is you who bends around the spoon.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Hi Sayeh,

    Thank you very much for your info.. I appreciate your help a lot!

    Regards,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Data member passing
    By wirefree101 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2006, 01:41 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing structure data to a function?
    By diddy02 in forum C Programming
    Replies: 8
    Last Post: 08-17-2002, 04:15 PM