Solved

Problem using char *, value being overridden.

Posted on 2002-07-29
4
197 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 93
How to set environment variables in C 2 86
c++ getting the first 10 characters of a char* string 11 98
nested if statement in excel help 4 37
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

792 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