Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 395623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:28:21+00:00 2026-05-12T16:28:21+00:00

Hi I have written a code based upon a requirement. (field1_6) (field2_30) (field3_16) (field4_16)

  • 0

Hi I have written a code based upon a requirement.

(field1_6)(field2_30)(field3_16)(field4_16)(field5_1)(field6_6)(field7_2)(field8_1)…..
this is one bucket(8 fields) of data. we will receive 20 buckets at a time means totally 160 fields.
i need to take the values of field3,field7 & fields8 based upon predefined condition.
if teh input argument is N then take the three fields from 1st bucket and if it is Y i need
to take the three fields from any other bucket other than 1st one.
if argumnet is Y then i need to scan all the 20 buckets one after other and check
the first field of the bucket is not equal to 0 and if it is true then fetch the three fields of that bucket and exit.
i have written the code and its also working fine ..but not so confident that it is effctive.
i am afraid of a crash some time.please suggest below is the code.

int CMI9_auxc_parse_balance_info(char *i_balance_info,char  *i_use_balance_ind,char *o_balance,char *o_balance_change,char *o_balance_sign
)
{
  char *pch = NULL;
  char *balance_id[MAX_BUCKETS] = {NULL};
  char balance_info[BALANCE_INFO_FIELD_MAX_LENTH] = {0};
  char *str[160] = {NULL};
  int i=0,j=0,b_id=0,b_ind=0,bc_ind=0,bs_ind=0,rc;
  int total_bukets ;
  memset(balance_info,' ',BALANCE_INFO_FIELD_MAX_LENTH);
  memcpy(balance_info,i_balance_info,BALANCE_INFO_FIELD_MAX_LENTH);
  //balance_info[BALANCE_INFO_FIELD_MAX_LENTH]='\0';
  pch = strtok (balance_info,"*");
  while (pch != NULL && i < 160)
  {
     str[i]=(char*)malloc(strlen(pch) + 1);
     strcpy(str[i],pch);
     pch = strtok (NULL, "*");
     i++;
  }
total_bukets  = i/8  ;
  for (j=0;str[b_id]!=NULL,j<total_bukets;j++)
  {
  balance_id[j]=str[b_id];
  b_id=b_id+8;
  }
  if (!memcmp(i_use_balance_ind,"Y",1))
  {
     if (atoi(balance_id[0])==1)
     {
        memcpy(o_balance,str[2],16);
        memcpy(o_balance_change,str[3],16);
        memcpy(o_balance_sign,str[7],1);
        for(i=0;i<160;i++)
        free(str[i]);
        return 1;
     }
     else
     {
        for(i=0;i<160;i++)
        free(str[i]);
      return 0;
     }
  }
  else if (!memcmp(i_use_balance_ind,"N",1))
  {
      for (j=1;balance_id[j]!=NULL,j<MAX_BUCKETS;j++)
      {
        b_ind=(j*8)+2;
        bc_ind=(j*8)+3;
        bs_ind=(j*8)+7;
       if (atoi(balance_id[j])!=1 && atoi( str[bc_ind] )!=0)
       {
        memcpy(o_balance,str[b_ind],16);
        memcpy(o_balance_change,str[bc_ind],16);
        memcpy(o_balance_sign,str[bs_ind],1);
        for(i=0;i<160;i++)
        free(str[i]);
        return 1;
       }
      }
     for(i=0;i<160;i++)
     free(str[i]);
    return 0;
  }
 for(i=0;i<160;i++)
 free(str[i]);
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:28:21+00:00Added an answer on May 12, 2026 at 4:28 pm

    I had a hard time reading your code but FWIW I’ve added some comments, HTH:

    // do shorter functions, long functions are harder to follow and make errors harder to spot
    // document all your variables, at the very least your function parameters
    // also what the function is suppose to do and what it expects as input
    int CMI9_auxc_parse_balance_info
    (
      char *i_balance_info,
      char *i_use_balance_ind,
      char *o_balance,
      char *o_balance_change,
      char *o_balance_sign
    )
    {
      char *balance_id[MAX_BUCKETS] = {NULL};
      char balance_info[BALANCE_INFO_FIELD_MAX_LENTH] = {0};
      char *str[160] = {NULL};
      int i=0,j=0,b_id=0,b_ind=0,bc_ind=0,bs_ind=0,rc;
      int total_bukets=0; // good practice to initialize all variables
    
      //
      // check for null pointers in your arguments, and do sanity checks for any
      // calculations
      // also move variable declarations to just before they are needed
      //
    
      memset(balance_info,' ',BALANCE_INFO_FIELD_MAX_LENTH);
      memcpy(balance_info,i_balance_info,BALANCE_INFO_FIELD_MAX_LENTH);
      //balance_info[BALANCE_INFO_FIELD_MAX_LENTH]='\0';  // should be BALANCE_INFO_FIELD_MAX_LENTH-1
    
      char *pch = strtok (balance_info,"*"); // this will potentially crash since no ending \0
    
      while (pch != NULL && i < 160)
      {
        str[i]=(char*)malloc(strlen(pch) + 1);
        strcpy(str[i],pch);
        pch = strtok (NULL, "*");
        i++;
      }
      total_bukets  = i/8  ;
      // you have declared char*str[160] check if enough b_id < 160
      // asserts are helpful if nothing else assert( b_id < 160 );
      for (j=0;str[b_id]!=NULL,j<total_bukets;j++)
      {
        balance_id[j]=str[b_id];
        b_id=b_id+8;
      }
      // don't use memcmp, if ('y'==i_use_balance_ind[0]) is better
      if (!memcmp(i_use_balance_ind,"Y",1))
      {
        // atoi needs balance_id str to end with \0 has it?
        if (atoi(balance_id[0])==1)
        {
          // length assumptions and memcpy when its only one byte
          memcpy(o_balance,str[2],16);
          memcpy(o_balance_change,str[3],16);
          memcpy(o_balance_sign,str[7],1);
          for(i=0;i<160;i++)
            free(str[i]);
          return 1;
        }
        else
        {
          for(i=0;i<160;i++)
            free(str[i]);
          return 0;
        }
      }
      // if ('N'==i_use_balance_ind[0]) 
      else if (!memcmp(i_use_balance_ind,"N",1))
      {
        // here I get a headache, this looks just at first glance risky. 
        for (j=1;balance_id[j]!=NULL,j<MAX_BUCKETS;j++)
        {
          b_ind=(j*8)+2;
          bc_ind=(j*8)+3;
          bs_ind=(j*8)+7;
          if (atoi(balance_id[j])!=1 && atoi( str[bc_ind] )!=0)
          {
            // length assumptions and memcpy when its only one byte
            // here u assume strlen(str[b_ind])>15 including \0
            memcpy(o_balance,str[b_ind],16);
            // here u assume strlen(str[bc_ind])>15 including \0
            memcpy(o_balance_change,str[bc_ind],16);
            // here, besides length assumption you could use a simple assignment
            // since its one byte
            memcpy(o_balance_sign,str[bs_ind],1);
            // a common practice is to set pointers that are freed to NULL.
            // maybe not necessary here since u return
            for(i=0;i<160;i++)
              free(str[i]);
            return 1;
          }
        }
        // suggestion do one function that frees your pointers to avoid dupl
        for(i=0;i<160;i++)
          free(str[i]);
        return 0;
      }
      for(i=0;i<160;i++)
        free(str[i]);
      return 0;
    }
    

    A helpful technique when you want to access offsets in an array is to create a struct that maps the memory layout. Then you cast your pointer to a pointer of the struct and use the struct members to extract information instead of your various memcpy’s

    I would also suggest you reconsider your parameters to the function in general, if you place every of them in a struct you have better control and makes the function more readable e.g.

    int foo( input* inbalance, output* outbalance )
    

    (or whatever it is you are trying to do)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code based on a specification from my boss. A requirement
I have written code that automatically creates CSS sprites based on the IMG tags
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this code where I want to increase / decrease number of
I have written some code which produces simple animations based on cycling through a
I have written some code in java that will guess a number based on
I now have this code based on some of the answers below. Is this
I have written this code for add cart to payment. while clicking buttons that
I have just written some code, which as i was writing i thought, this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.