对malloc的问题 [英] Question to malloc

查看:55
本文介绍了对malloc的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在Linux上创建一个像updatedb这样的文件索引作为我程序的一部分,但是我不知道文件名能用了多长时间是。

因此我想使用malloc来保持文件名的大小

灵活。我会期望使用以下

源代码进行分段错误,当调用strcat或sprintf将第一个值分配给

szFile时,因为缺少空间。

函数有递归...

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));


}


为什么可以将字符串分配给大于空格的szFile

分配了吗?


问候,

T homas B

解决方案

Thomas Barth写道:


我想在Linux上创建一个像updatedb这样的文件索引作为我程序的一部分,但我不知道文件名能用了多长时间因此我想使用malloc来保持文件名的大小灵活。当调用strcat或sprintf将第一个值分配给
szFile时,由于缺少空间,我期望使用以下源代码进行分段错误。

char * szFile = NULL,
...
szFile =(char *)malloc(1); // ???
// szFile =(char *)malloc((iLenD + iLenF + 1));

strcat(szFile,szDir);
strcat(szFile) ,/;;
strcat(szFile,szFilename);

为什么可以将字符串分配给大于空间的szFile分配?




因为未定义的行为是未定义的。还有

无法保证U.B.会导致崩溃或其他明显的

错误。它可能会导致一个微妙的错误,直到最可能的尴尬时刻才会找到
。 U.B.甚至还有机会获得
的机会。结果是你想要的 -

不是什么东西可以存入,显然......


-

Eric Sosman
es*****@acm-dot-org.inva lid


Thomas Barth写道:


我想在Linux上创建像updatedb这样的文件索引作为
我的程序的一部分,但我不知道文件名可以有多长。
因此我想使用malloc来保持文件名的大小灵活。我会期望使用以下源代码进行分段错误,当调用strcat或sprintf为szFile分配第一个值因为缺少空间。

函数递归...
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);
...

空隙setAbsoluteFilename(字符* szFile,字符* szDir,字符* 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));

}


为什么可以将字符串分配给比分配空间更大的szFile?



因为C假设你知道你在做什么,并且不做任何假设来自szFile所指向的存储空间 - 你可能通过一些

狡猾的路线拥有那个记忆......例如,也许你重写了malloc(),并知道'''之后

szFile [0]!

-

==============
不是学生

==============


Thomas Barth写道:< blockquote class =post_quotes>

我想创建一个像updatedb一样的文件索引Linux确实是我程序的一部分,但我不知道文件名可以有多长。
因此我想使用malloc来保持文件名的大小灵活。我会期望使用以下
源代码进行分段错误,当调用strcat或sprintf将第一个值分配给
szFile时因为缺少空间。

带递归的函数...
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);
...

空隙setAbsoluteFilename(字符* szFile,字符* szDir,字符* 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));

}




/ * BEGIN Absolute.c输出* /


文件名:MyDirectory / MyFile

长度:18

/ * 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 a
part 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 filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value to
szFile 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 is
allocated?

Regards,
T h o m a s B

解决方案

Thomas Barth wrote:

Hi,
I would like to create a file index like updatedb on Linux does as a
part 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 filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value to
szFile because of missing space.

char *szFile = NULL,
...
szFile = (char *)malloc(1); //???
//szFile = (char *)malloc((iLenD + iLenF + 1));

strcat(szFile, szDir);
strcat(szFile, "/");
strcat(szFile, szFilename);

Why is it possible to assign strings to szFile bigger than space is
allocated?



Because undefined behavior is "undefined." There is
no guarantee that U.B. will cause a crash or other obvious
error. It may cause a subtle error that won''t be found
until the most embarrassing moment possible. There''s even
a chance that U.B. will turn out to be what you wanted --
not something to bank on, obviously ...

--
Eric Sosman
es*****@acm-dot-org.invalid


Thomas Barth wrote:

Hi,
I would like to create a file index like updatedb on Linux does as a
part 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 filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value
to szFile 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 is
allocated?


Because C assumes you know what you''re doing, and makes no assumption as to
the storage that szFile points to - you might own that memory through some
devious route ... e.g., perhaps you rewrote malloc(), and know what''s after
szFile[0]!
--
==============
Not a pedant
==============


Thomas Barth wrote:


Hi,
I would like to create a file index like updatedb on Linux does as a
part 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 filenames
flexible. I would expect an segmentation fault with the following
sourcecode, when invoking strcat or sprintf to assign the first value to
szFile 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));

}



/* BEGIN Absolute.c output */

Filename: MyDirectory/MyFile
Length: 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的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆