当前位置: 动力学知识库 > 问答 > 编程问答 >

string - Strcmp Contradicts Printf in C

问题描述:

I am passing a pointer to a pointer to a void function, whose purpose is to dynamically allocate and create a string (containing a full file path).

At the end of the function, I check with a printf(%s) and the function seems to work perfectly (for the ~10 test files in my sample run). However, I check the pointer again in main, just after the void is called, and the result doesn't seem to be maintained.

At the end of the void function:

printf("%s\n", *ptr) returns usr/path/file

In main:

printf("%s\n", *ptr) returns usr/path/fil50(??

Stuff like this happens for several of the longer file paths (the shorter strings seem to be immune). I create the string inside the void function with strcpy and strcat on two char buffers (for directory path and file name).

The really weird thing is - I added strcmp and strlen checks on the *ptr strings in the two places (at the end of the void function and in main), and:

  1. strlen is the same for the two, even though the externally printed string is clearly longer! See the above example. I can clearly tell because each string is printed on a new line.
  2. strcmp returns 0, even though the strings printed are obviously different.

I cannot account for this behavior...The change in *ptr within and outside the function is one thing, but how can printf and strcmp contradict each other??

I have not been able to find anyone else who has experienced this, and would be grateful for any help.

EDIT- I should have mentioned the void function is trying to replace an existing intra-main operation that wastes a lot of memory (calling sprintf on a massive buffer). Frankly I was just asking a general question about contradiction between strcmp and printf, but am certainly willing to provide more evidence.

int main(int argc, char *argv[]) {

char buff[MASSIVE_SIZE];

sprintf(buff, "%s/%s", dir, path->d_name);

char **ptr = (char **)malloc(sizeof(char*));

make_full_path(dir, path->d_name, ptr);

printf("external, strcmp:%d\n%s\n", strlen(*ptr), *ptr);

printf("strcmp=%d\n", strcmp(buff, *ptr));

}

void make_full_path (const char *dir_name, char *other_name, char **pptr){

int len_one = strlen(dir_name);

int len_two = strlen(other_name);

char dest[len_one+2];

strcpy(dest, dir_name);

dest[len_one] = '/';

dest[len_one+1] = '\0';

char src[len_two+1];

strcpy(src, other_name);

src[len_two] = '\0';

*pptr = strcat(dest, src);

printf("internal, strlen=%d:\n%s\n", strlen(*pptr), *pptr);

}

网友答案:

However, I check the pointer again in main, just after the void is called, and the result doesn't seem to be maintained.

This could mean only one thing: you are returning a pointer to local. This is undefined behavior:

char dest[len_one+2];
...
*pptr = strcat(dest, src); // Passing a local back to main

Replace it with a call of malloc will fix the problem:

char dest[len_one+strlen(other_name)+2];
...
strcat(dest, src);
*pptr = malloc(strlen(dest)+1);
strcpy(*pptr, dest);
分享给朋友:
您可能感兴趣的文章:
随机阅读: