验证有效指针 [英] verifying valid Pointer
问题描述
Hello Newsgroup,
我遇到了一个函数的问题,它应该返回一个指向
字符串开头或NULL的指针。像这样:
char * getString(void);
问题是,该函数似乎很烦人。它有时返回一个
指针,如果我尝试的话,一定不能去掉指针,我得到一个分段错误
或类似的东西。这个指针不是NULL。
我如何测试,如果我可以在程序崩溃时访问String?试一试
catch块不起作用。
只是为了说明:
char * getString(void) {//这只是为了创造一个糟糕的回报
char * null = NULL; //真正的bugy函数在库中
return& null [1]; //我必须使用
}
int main(void){
char * badPtr = getString();
if(badPtr == NULL){
printf(" NULL Pointer\\\
);; //这不会发生
} else {
printf(" Not NULL Pointer\\\
);
printf("值:%i \ n",* badPtr); //程序在这里崩溃
}
}
我在Windows下遇到了这个问题,在cygwin / gcc和borland bcc。
我想我能想象为什么,但我怎么能解决呢?
谢谢
return& null [1];不行不动
return& null [0];将来。
GanetMarV写道:
return& null [1];不会工作
return& null [0];将会。
那不是点。
我在getString中无法解决它,因为这不是我的代码。
我必须在主要测试指针
谢谢
" andreas" <一个********* @ gmx.de>在消息中写道
news:41 *********************** @ news.freenet.de ...Hello新闻组,
我遇到了一个函数问题,该函数应该返回指向字符串开头或NULL的指针。像这样:
char * getString(void);
问题是,该函数似乎是bugy。它有时会返回一个
指针,如果我尝试的话,一定不能去掉它,我会得到一个分段错误
或类似的东西。这个指针不是NULL。
如果我可以在没有程序崩溃的情况下访问String,我怎么测试?
try catch块不起作用。
只是为了说明:
char * getString(void){//这只是为了创建一个错误的返回
char * null = NULL; //真正的bugy函数在库中
return& null [1]; //我必须使用
更改为:
返回null;
}
int main(void){
char * badPtr = getString();
if(badPtr == NULL){
printf(" NULL Pointer \ N'QUOT); //这不会发生
} else {
printf(" Not NULL Pointer\\\
);;
printf(" Value:%i \ n",* badPtr) ; //程序在这里崩溃
}
我在Windows下遇到了这个问题,在cygwin / gcc和borland bcc下面。
我想我可以想象为什么,但是如何我可以解决吗?
见上文,并阅读有关数组和指针的内容。
-Mike
Hello Newsgroup,
I''ve got a problem with a function that should return a pointer to the
beginning of a string or NULL. like this:
char* getString(void);
The problem is, that the function seems to be bugy. It sometimes returns a
pointer, that must not be derefered, if I try, I get a segmentation fault
or something like this. This pointer is not NULL.
How can I test, if I can access the String whithout a programm crash? A try
catch block does not work.
Just to illustrate:
char* getString(void){ //this is just to create a bad return
char* null = NULL; //the real bugy function is in a library
return &null[1]; //i have to use
}
int main(void) {
char* badPtr = getString();
if(badPtr == NULL) {
printf("NULL Pointer\n"); // this does not happen
} else {
printf("Not NULL Pointer\n");
printf("Value: %i\n",*badPtr); // program crashes here
}
}
I had this Problems under Windows whis cygwin/gcc and borland bcc.
I think i can imagine why, but how can i solve it?
thanks
return &null[1]; won''t work
return &null[0]; will.
GanetMarV wrote:
return &null[1]; won''t work
return &null[0]; will.
that''s not the Point.
I can''t solve it in getString, cause this is not my code.
I have to test the pointer in main
thanks anyway
"andreas" <an*********@gmx.de> wrote in message
news:41***********************@news.freenet.de...Hello Newsgroup,
I''ve got a problem with a function that should return a pointer to the
beginning of a string or NULL. like this:
char* getString(void);
The problem is, that the function seems to be bugy. It sometimes returns a
pointer, that must not be derefered, if I try, I get a segmentation fault
or something like this. This pointer is not NULL.
How can I test, if I can access the String whithout a programm crash? A try catch block does not work.
Just to illustrate:
char* getString(void){ //this is just to create a bad return
char* null = NULL; //the real bugy function is in a library
return &null[1]; //i have to use
Change this to:
return null;
}
int main(void) {
char* badPtr = getString();
if(badPtr == NULL) {
printf("NULL Pointer\n"); // this does not happen
} else {
printf("Not NULL Pointer\n");
printf("Value: %i\n",*badPtr); // program crashes here
}
}
I had this Problems under Windows whis cygwin/gcc and borland bcc.
I think i can imagine why, but how can i solve it?
See above, and read up about arrays and pointers.
-Mike
这篇关于验证有效指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!