Solved

Problem using char *, value being overridden.

Posted on 2002-07-29
4
192 Views
Last Modified: 2010-04-15
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
Comment
Question by:theta
  • 3
4 Comments
 

Author Comment

by:theta
ID: 7185934
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
 

Author Comment

by:theta
ID: 7185948
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
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 7186403
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
 

Author Comment

by:theta
ID: 7187946
Yes..that was the bug.....which I couldn't see.
Thank you.

Theta.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now