• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Problem using char *, value being overridden.

I am trying to combine bunch of strings into two char * variables called StanAdd1 and StanAdd2. First I copy..say five strings into StanAdd1 and then I copy couple more into StanAdd2. I do this by first using strcpy and then strcat for each additional char *.
But, as soon as I use strcpy on StanAdd2, my strlen on StanAdd1 changes, and it should not.

Here is a snippet of my code:

void main(int argc, char **argv) {
  . . .
  char *StanAdd1, *StanAdd2;
  . . .
  StanAdd1 = calloc(1,  95);
  StanAdd2 = calloc(1, 166);
  . . .
  strcpy(StanAdd1, strtrim(HNumber));      /* 10 chars */
  strcat(StanAdd1, strtrim(HNumberSuff));  /* 10 chars */
  strcat(StanAdd1, strtrim(StrPrefDir));   /*  3 chars */
  strcat(StanAdd1, strtrim(StrPrefType));  /* 20 chars */
  strcat(StanAdd1, strtrim(StreetName));   /* 25 chars */

  while (strlen(StanAdd1) < 127) strcat(StanAdd1, " ");

printf("1. strlen(StanAdd1): %d.\n", strlen(StanAdd1));

  strcpy(StanAdd2, BoxType);
printf("2. strlen(StanAdd1): %d.\n", strlen(StanAdd1));

  . . .
} /* end of main */

Program Output:

1. strlen(StanAdd1): 127.
2. strlen(StanAdd1): 119.

The strlen should be 127 on the second line. What am I doing wrong here?

Thanks in advance.
Theta.


0
theta
Asked:
theta
  • 3
1 Solution
 
thetaAuthor Commented:
Following is the strtrim function that I am calling from main:

/*** Trim string, accept for one space at the end of the string ***/
char strtrim(char *inStr) {
  char *result;
  char lastchar;
  int count=0;

  free(result);
  result  = calloc(1, strlen(inStr));

  while (strncmp(inStr+count, " ", 1) == 0) {count++;}
  strncat(result, inStr+count, 1);
  count++;

  while (count <= strlen(inStr)) {
    if (strncmp(inStr+count, " ", 1) != 0) {
       lastchar = (char)inStr[count];
       strncat(result, inStr+count, 1);
    }
    else {
       if (lastchar != ' ') {
          lastchar = (char)inStr[count];
          strncat(result, inStr+count, 1);
       }
    }
    count++;
  }
  return *result;
}
0
 
thetaAuthor Commented:
Sorry, following is the correct version of strtrim function:

/*** Trim string, accept for one space at the end of the string ***/
char *strtrim(char *inStr) {
  char *result;
  char lastchar;
  int count=0;

  free(result);
  result  = calloc(1, strlen(inStr));

  while (strncmp(inStr+count, " ", 1) == 0) {count++;}
  strncat(result, inStr+count, 1);
  count++;

  while (count <= strlen(inStr)) {
    if (strncmp(inStr+count, " ", 1) != 0) {
       lastchar = (char)inStr[count];
       strncat(result, inStr+count, 1);
    }
    else {
       if (lastchar != ' ') {
          lastchar = (char)inStr[count];
          strncat(result, inStr+count, 1);
       }
    }
    count++;
  }
  return result;
}
0
 
imladrisCommented:
You appear to be expecting StanAdd1 to be to 127 bytes. However, you have only allocated 95.

So you concatenate some strings into StanAdd1, then add spaces until it is 127 long.

But StanAdd1 is only has 95 bytes allocated, then StanAdd2 has 166 bytes allocated. So characters added to StanAdd1 past the 95th will overrun its buffer, and, since StanAdd2 is probably allocated right behind it, and run into StanAdd2.

Then when you copy characters into StanAdd2, they will wind up in the middle of, what appears to be, StanAdd1.

I think you can resolve it all by simply allocating enough space:

StanAdd1 = calloc(1, 150);
StanAdd2 = calloc(1, 166);
 
0
 
thetaAuthor Commented:
Yes..that was the bug.....which I couldn't see.
Thank you.

Theta.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now