测试指针是否指向已分配的内存 [英] Test if pointer points to allocated memory

查看:54
本文介绍了测试指针是否指向已分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何测试指针是否指向已分配的内存或

不是吗?

例如,如果我有一个指针,如char * p,那么是否有标准

方法测试以下作业是否已经应用了


p =(char *)malloc(sizeof(char)* n);

Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(char) * n);

推荐答案

Andrew< an *********** @ ca.com>潦草地写了下面的内容:
Andrew <an***********@ca.com> scribbled the following:
无论如何都要测试一个指针是指向分配的内存还是
不是?
例如,如果我有一个指针,如char * p,那么标准
测试是否已应用如下任务的方法?
p =(char *)malloc(sizeof(char)* n);
Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(char) * n);



不便携。


-

/ - Joona Palaste(pa*****@cc.helsinki .fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------- -----规则! -------- /

在狗的外面,一本书是男人最好的朋友。无论如何,在狗的内心,它太黑了



- Groucho Marx



Not portably.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man''s best friend. Inside a dog, it''s too dark
to read anyway."
- Groucho Marx


安德鲁写道:

无论如何测试指针是否指向已分配的内存或
不是吗?
例如,如果我有一个指针,如char * p是否有一种标准的方法来测试是否已经应用了以下任务?
p =(char *)malloc(sizeof(char)* n);

Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(char) * n);




如果指针未初始化或已被释放

而未被重新分配,则无法评估指针。

如果您知道指针值为NULL或

指向内存,则可以检查是否为NULL。


我认为你可以只需添加另一个对象来保持

跟踪该行是否已执行。


int applied = 0;


p =(char *)malloc(sizeof(char)* n);

applied = 1;


我也是su你对malloc的

返回值有疑问,但我不确定。


完成malloc任务后,

你总是需要检查NULL。


p = malloc(n * sizeof * p);

if(p == NULL){

/ *聪明地处理它* /

}


-

pete



If a pointer is uninitialized or has been freed
without being reassigned, then you can''t evaluate the pointer.

If you know that a pointer value is either NULL or
points to memory, you can check for NULL.

I think you could just add another object to keep
track of if that line was executed.

int applied = 0;

p = (char *) malloc(sizeof(char) * n);
applied = 1;

I also suspect that you have a question about the
return value of malloc, but I''m not sure.

After a malloc assignment has been made,
you always need to check for NULL.

p = malloc(n * sizeof *p);
if (p == NULL) {
/* deal with it intelligently */
}

--
pete


Andrew写道:

无论如何测试指针是指向已分配的内存还是
不是?
例如,如果我有一个像char * p这样的指针,是否有一种标准的方法可以测试是否已经应用了以下任务?
p =(char *)malloc(sizeof(char)* n);

Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(char) * n);




不要施放malloc。这是不必要的,并且隐藏了错误。


出于所有实际目的,你的答案是否定的。但是,如果你

执行free(p);并且程序会变为BOOM你可以相当

确保p没有被malloced,或者已经被释放了。我很有信心

这对你来说是一个很大的帮助和安慰。


-

Chuck F(cb * *******@yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



Don''t cast malloc. It is unnecessary and hides errors.

To all practical purposes, your answer is no. However if you
execute "free(p);" and the program goes BOOM you can be fairly
sure that p was not malloced, or has already been freed. I''m sure
this is a great help and comfort to you.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


这篇关于测试指针是否指向已分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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