?
Solved

Problem using char *, value being overridden.

Posted on 2002-07-29
4
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 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