struct stat * stbuf seg fault [英] struct stat *stbuf seg fault

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

问题描述

我是c的新手,还不确定c和它之间的界限是什么?b
实现。因此,如果应该在其他地方询问,请重定向我。


我正在开发一个函数来确定目录是否存在。想法

来自我在samba源文件中找到的片段。


bool dirExist(char * dname)

{

struct stat * stbuf;

// stbuf =(struct stat *)malloc(sizeof(struct stat));

bool ret;


if(stat(dname,stbuf)!= 0)

return(False);


ret = S_ISDIR(stbuf-> st_mode);

if(!ret)

errno = ENOTDIR;

printf("%s \\ \\ n             
返回ret;

}


似乎同意K& R2的第180页。


当传递一个无法存在的目录名称时,它会正确地报告失败。

然而,当通过一个存在的目录时,它会发生段错误。
< br $>
stat(dname,stbuf)


Mallocing stbuf只是我一直在尝试的东西 - 没有修复任何东西。

谢谢

Dave Farning



I am new to c and not yet sure of the boundary between c and it''s
implementations. So please redirect me if this should be asked elsewhere.

I am working on a function to determine if a directory exists. The idea
comes from a snippet I found in a samba source file.

bool dirExist(char *dname)
{
struct stat *stbuf;
//stbuf = (struct stat*)malloc(sizeof(struct stat));
bool ret;

if (stat(dname,stbuf) != 0)
return(False);

ret = S_ISDIR(stbuf->st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s\n",ret);
return ret;
}

It seems to agree with page 180 of K&R2.

When passed a none existant directory name, it correctly reports a failure.
Yet, when passed a existant dir it segfaults at

stat(dname,stbuf)

Mallocing stbuf was just something I have been trying--didn''t fix anything.

Thanks
Dave Farning




推荐答案



我获得了文学执照,并对代码进行了一些批评(sp?)。


标记了你问题的直接答案。

I took a literary license and did some critiquing(sp?) of the code.

The direct answer to your question is marked.
bool dirExist(char * dname)
{
struct stat * stbuf;


以这种方式声明结构将*不*在堆栈上腾出空间

变量。另一种方法是这样做(如果你不喜欢做/ b
动态内存管理结构你知道你需要但不想要
交易随着&'s到处都是:


struct stat _stbuf;

struct stat * stbuf =& _stbuf;


要么程序溢出堆栈(你应该有一个例程

注册来处理这个事件)或者它有效。


我也觉得声明变量没有初始化它们是个坏主意因为

a以后与NULL的比较可能会或者可能不会将它显示为无用的

指针。通常情况下,NULL为0,并且*随机地* *
运气的神奇*为零,但这是实现定义和运气的组合。


----------------> ANSWER< --------------


你的方式它按原样完成,永远不会为缓冲区分配空间。

因此,当操作系统试图找到文件并失败时,事情就像

计划一样,但是当它成功找到文件,它试图写入垃圾

指针,从而写入段错误。

// stbuf =(struct stat *)malloc(sizeof(struct stat) ));
我很确定//在ansi 89中是非法的,但在99中确定

施放malloc的返回值是* bad *。这样做会导致

编译器不要抱怨您忘记包含stdlib。

bool ret;
if(stat(dname,stbuf)!= 0)
return(False);
ret = S_ISDIR(stbuf-> st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s \ n",ret);
返回ret;
}


我不确定你使用的编辑器是什么,但我很确定这个行业
标准是两个空格,小写和_'的命名约定相反

的混合大小写。

Mallocing stbuf只是我有的东西一直在尝试 - 没有解决任何问题。
bool dirExist(char *dname)
{
struct stat *stbuf;
declaring the struct in this manner will *not* make space on the stack
for the variable. Another way to do this (if you don''t like doing
dynamic memory management for structs you know you''ll need but don''t want to
deal with the &''s all over the place) is:

struct stat _stbuf;
struct stat *stbuf = &_stbuf;

Either the program overflows the stack (and you should have a routine
registered to handle this event) or it works.

I also feel it''s a bad idea to declare variables w/o initializing them because
a comparison to NULL later on might or might not reveal it as a useless
pointer. Typically, NULL is 0, and things are *magically* zero out of random
luck, but that''s a combination of implementation definitions and luck.

---------------->ANSWER<--------------

The way you have it done as is, space is never being allocated for the buffer.
Consequently when the OS tries to find the file and fails, things go as
planned, but when it succeeds in finding the file, it tries to write to a junk
pointer, and thus segfaults.
//stbuf = (struct stat*)malloc(sizeof(struct stat)); i''m pretty sure // is illegal in ansi 89, but ok in 99

casting the return value of malloc is *bad*. Doing that will cause the
compiler not to complain that you forgot to include stdlib.
bool ret; if (stat(dname,stbuf) != 0)
return(False); ret = S_ISDIR(stbuf->st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s\n",ret);
return ret;
}
I''m not sure what editor you''re using, but i''m pretty sure that industry
standard is two spaces, and a naming convention of lower case and _''s instead
of mixed case.
Mallocing stbuf was just something I have been trying--didn''t fix anything.




不太确定。我尝试了以下测试代码:


我的方式:工作

malloc:工作

显示:segfaulted


hth

-

Harrison Caudill | 。^ www.hypersphere.org

计算机科学&物理双专业| |我*我= 1

佐治亚理工学院| v''我只是一个普通人



Not really sure. I tried test code with the following results:

my way: worked
malloc: worked
shown: segfaulted

hth

--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | v'' I''m just a normal guy


Charles Harrison Caudill写道:
Charles Harrison Caudill wrote:

我拿了一个文学许可证并对代码做了一些批评(sp?)。

标记了你的问题的直接答案。

I took a literary license and did some critiquing(sp?) of the code.

The direct answer to your question is marked.
bool dirExist(char * dname)
结构声明* stbuf;
bool dirExist(char *dname)
{
struct stat *stbuf;



以这种方式声明结构将*不*为变量创建堆栈空间
。另一种方法(如果你不喜欢对结构进行动态内存管理,你知道你需要但不想要处理&所有在这个地方)是:

struct stat _stbuf;
struct stat * stbuf =& _stbuf;

程序溢出堆栈(你应该有注册处理此事件的例程)或者它可以工作。

我也觉得声明变量没有初始化它们是个坏主意
因为与NULL的比较以后可能会或可能不会将其显示为无用的指针。通常情况下,NULL为0,并且随机运气中的东西是* * *,但这是实现定义和运气的组合。

---- ------------> ANSWER< --------------

你按原样完成的方式,空间永远不会被分配给
缓冲区。因此,当操作系统试图找到文件并失败时,事情按计划进行,但是当它成功找到文件时,它会尝试写入垃圾指针,从而进行段错误。



declaring the struct in this manner will *not* make space on the stack
for the variable. Another way to do this (if you don''t like doing
dynamic memory management for structs you know you''ll need but don''t want
to deal with the &''s all over the place) is:

struct stat _stbuf;
struct stat *stbuf = &_stbuf;

Either the program overflows the stack (and you should have a routine
registered to handle this event) or it works.

I also feel it''s a bad idea to declare variables w/o initializing them
because a comparison to NULL later on might or might not reveal it as a
useless
pointer. Typically, NULL is 0, and things are *magically* zero out of
random luck, but that''s a combination of implementation definitions and
luck.

---------------->ANSWER<--------------

The way you have it done as is, space is never being allocated for the
buffer. Consequently when the OS tries to find the file and fails, things
go as planned, but when it succeeds in finding the file, it tries to write
to a junk pointer, and thus segfaults.

// stbuf =(struct stat *)malloc(sizeof(struct stat));
//stbuf = (struct stat*)malloc(sizeof(struct stat));


我非常肯定//在ansi 89中是非法的,但是好吧在99
施放malloc的返回值是*坏*。这样做会导致
编译器不要抱怨您忘记包含stdlib。


i''m pretty sure // is illegal in ansi 89, but ok in 99

casting the return value of malloc is *bad*. Doing that will cause the
compiler not to complain that you forgot to include stdlib.

bool ret;
bool ret;


if(stat(dname,stbuf)!= 0)
return(False);
if (stat(dname,stbuf) != 0)
return(False);


ret = S_ISDIR(stbuf-> st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s\\\
",ret);
return ret;
}
ret = S_ISDIR(stbuf->st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s\n",ret);
return ret;
}



我不确定你正在使用什么编辑器,但我很确定行业
标准是两个空格,以及小写和_'的命名约定没有混合大小写。



I''m not sure what editor you''re using, but i''m pretty sure that industry
standard is two spaces, and a naming convention of lower case and _''s
instead of mixed case.




谢谢,我来自一个主要是java,python和bash的世界。骆驼

案例和标签(4)几乎是我养成的习惯。这真是太棒了

有多少方法可以用C咬你自己 - 不抱怨

只是脾气暴躁。


删除(struct stat *)后,它就像魅力一样。我认为

重要的是把指针指向它们指向的对象类型。我是一个错误的思路。

其次,你提到的是处理堆栈溢出的例行程序。你能不能给b $ b指出更多的信息。


谢谢

Dave Farning



Thanks, I''m coming from a world mostly of java, python, and bash. Camel
case and tab(4) were pretty much the habit I got into. It is trully amazing
how many way there are to bite yourself in the butt with C--Not complaining
just grumping.

After removing the (struct stat*) cast it works like a charm. I thought
that it was important to cast pointers to the type of object that they
point to. Am I heading down a errorous path of thought.
Secondly, you mentioned are routine to handle stack overflows. Could you
point to more imformation on this.

Thanks
Dave Farning


>我是c的新手,还不确定c和它的
>I am new to c and not yet sure of the boundary between c and it''s
实现之间的界限。所以如果应该在其他地方询问,请重定向我。

我正在开发一个函数来确定目录是否存在。这个想法来自我在一个samba源文件中找到的片段。

bool dirExist(char * dname)
{
struct stat * stbuf;


这里你已经声明了一个指针变量,但它并没有被初始化为
指向任何地方,所以它可能指向Mars Rover

或它可能指向你的屁股。

// stbuf =(struct stat *)malloc(sizeof(struct stat));
bool ret;

if(stat(dname,stbuf)!= 0)


这里你使用的是一个未初始化的指针并将其传递给

a函数写指针所指向的数据。

哎哟!如果那个指针没有指向你的屁股,你可能是造成火星漫游者问题的那个人。


尝试声明一个struct stat和stbuf指向它。

返回(错误);

ret = S_ISDIR(stbuf-> st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s \ n",ret);
返回ret;
}

似乎同意第180页K& R2。

当传递一个无法存在的目录名称时,它会正确地报告失败。
然而,当通过一个存在的目录时,它会出现段错误。

stat (dname,stbuf)
implementations. So please redirect me if this should be asked elsewhere.

I am working on a function to determine if a directory exists. The idea
comes from a snippet I found in a samba source file.

bool dirExist(char *dname)
{
struct stat *stbuf;
Here you have declared a pointer variable, but it isn''t
initialized to point anywhere, so it might point at the Mars Rover
or it might point at your butt.
//stbuf = (struct stat*)malloc(sizeof(struct stat));
bool ret;

if (stat(dname,stbuf) != 0)
Here you are using an uninitialized pointer and passing it to
a function that expects to write data where the pointer is pointing.
Ouch! If that pointer is not pointing at your butt, YOU might be
the one who caused the problems with the Mars Rover.

Try declaring a struct stat and making stbuf point at it.
return(False);

ret = S_ISDIR(stbuf->st_mode);
if(!ret)
errno = ENOTDIR;
printf("%s\n",ret);
return ret;
}

It seems to agree with page 180 of K&R2.

When passed a none existant directory name, it correctly reports a failure.
Yet, when passed a existant dir it segfaults at

stat(dname,stbuf)




你认为stbuf指向哪里?

为什么SHOULDN'你得到了一个包皮过多的错误?

Gordon L. Burditt



Where, exactly, do you think stbuf is pointing?
Why SHOULDN''T you get a smegmentation fault?

Gordon L. Burditt


这篇关于struct stat * stbuf seg fault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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