/ * END Absolute.c输出* / / * BEGIN Absolute.c * / #include< stdlib.h> #include< stdio.h> #include< string.h> char * setAbsoluteFilename(char * szDir,char * szFilename); int main(无效) { char * szFile; puts(" \ n / * BEGIN Absolute.c output * / \ n"); szFile = setAbsoluteFilename(" MyDirectory"," MyFile" ); if(szFile!= NULL){ printf(" Filename:%s\\\Length:%d \ n",szFile,strlen(szFile)); 免费(szFile); }其他{ puts(" szFile == NULL"); } puts(" \\\ / * END Absolute.c output * /"); 返回0; } char * setAbsoluteFilename(char * szDir,char * szFilename) { size_t iLenF; size_t iLenD; char * szFile; iLenF = strlen(szFilename); iLenD = strlen(szDir); szFile = malloc(iLenD + 1 + iLenF + 1); if(szFile!= NULL){ sprintf(szFile,"% s /%s",szDir,szFilename); } 返回szFile; } / * END Absolute.c * / - pete Hi,I would like to create a file index like updatedb on Linux does as apart of my program, but I dont know how long the filenames could be.Therefore I want to use malloc to keep the size of the filenamesflexible. I would expect an segmentation fault with the followingsourcecode, when invoking strcat or sprintf to assign the first value toszFile because of missing space.function with recursion...char *szFile = NULL,....while((ptrDirentry = readdir(ptrDir)) != NULL) { if (strcmp((*ptrDirentry).d_name, ".") != 0 &&strcmp((*ptrDirentry).d_name, "..") != 0) { setAbsoluteFilename(szFile, szDir, (*ptrDirentry).d_name);....void setAbsoluteFilename(char *szFile, char *szDir, char *szFilename) {int iLenF = strlen(szFilename),iLenD = strlen(szDir); szFile = (char *)malloc(1); //???//szFile = (char *)malloc((iLenD + iLenF + 1)); strcat(szFile, szDir);strcat(szFile, "/");strcat(szFile, szFilename); //sprintf(szFile, "%s/%s", szDir, szFilename); printf("Filename: %s, Size: %d\n", szFile, strlen(szFile)); } Why is it possible to assign strings to szFile bigger than space isallocated? Regards,T h o m a s B 解决方案 Because undefined behavior is "undefined." There isno guarantee that U.B. will cause a crash or other obviouserror. It may cause a subtle error that won''t be founduntil the most embarrassing moment possible. There''s evena chance that U.B. will turn out to be what you wanted --not something to bank on, obviously ... --Eric Sosman es*****@acm-dot-org.invalidBecause C assumes you know what you''re doing, and makes no assumption as tothe storage that szFile points to - you might own that memory through somedevious route ... e.g., perhaps you rewrote malloc(), and know what''s afterszFile[0]!--==============Not a pedant============== /* BEGIN Absolute.c output */ Filename: MyDirectory/MyFileLength: 18 /* END Absolute.c output *//* BEGIN Absolute.c */ #include <stdlib.h>#include <stdio.h>#include <string.h> char *setAbsoluteFilename(char *szDir, char *szFilename); int main(void){char *szFile; puts("\n/* BEGIN Absolute.c output */\n");szFile = setAbsoluteFilename("MyDirectory", "MyFile");if (szFile != NULL) {printf("Filename: %s\nLength: %d\n", szFile, strlen(szFile));free(szFile);} else {puts("szFile == NULL");}puts("\n/* END Absolute.c output */");return 0;} char *setAbsoluteFilename(char *szDir, char *szFilename){size_t iLenF;size_t iLenD;char *szFile; iLenF = strlen(szFilename);iLenD = strlen(szDir);szFile = malloc(iLenD + 1 + iLenF + 1);if (szFile != NULL) {sprintf(szFile, "%s/%s", szDir, szFilename);}return szFile;} /* END Absolute.c */--pete 这篇关于对malloc的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 02:53