在访问器函数中是NULL检查冗余? [英] is NULL-checking redundant in accessor-functions?

查看:116
本文介绍了在访问器函数中是NULL检查冗余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


考虑以下简单的访问函数:


typedef struct {

int i;

char name [32];

} MY_TYPE;


const char *

getName( const MY_TYPE * p)

{

返回p->名称;

}


我总是假设,检查传递的指针'p''对NULL是多余的,因为,如果它在运行时碰巧是NULL(由于
$ b $) b编程错误),尝试取消引用它会导致SEGFAULT




我现在正在查看一个存在访问函数的情况,而不是
崩溃,返回字段name的偏移量从0(上面的

示例中的0x4)。好像在NULL指针处有合法值0x00000000 ...


有何评论?这是在Solaris-9下的Sparc机器上。谢谢!


-mi

-

comp.lang.c.moderated - 审核地址: cl ** @ plethora.net

解决方案

整鸡更有效率比购买件便宜。


1个小人类,切成碎片

2杯面粉

洋葱,大蒜



胡椒

大蒜粉

辣椒

辣酱等
油炸油


将牛奶,鸡蛋,辣酱拌入碗中,加入切碎的洋葱和大蒜。

大量调味肉类,腌几个小时。

将调味面粉放在纸质或塑料购物袋中,

几次掉落,彻底摇匀,

然后在热油(350°)中油炸约15分钟。

沥干并放在纸巾上。


与Mustard Greens一起流产


为什么浪费它?否则,一般来说,使用火腿或盐猪肉来调味蔬菜。

榨菜的技术可以和许多蔬菜一起使用;

绿豆特别好用。每天都不需要肉,不要害怕改变任何菜肴的素食口味。


1早产儿,天生死亡

大串芥末酱

2个白洋葱,1杯切碎的芹菜

植物油(或生猪脂肪)

盐,辣椒,大蒜等。


在大型重锅中加入浅棕色洋葱,芹菜,大蒜和肉。

加点水和绿色蔬菜(应该是彻底清洗和清洗)。

慢慢闷烧至少2小时,加入少量水

,当它开始粘住时。

经常搅拌。

准备好后 - 用米饭,烤烟服务


26:1,2

[。 .. ... ...


你们要吃你们儿子的肉,和你们女儿的肉,你们要吃。

利未记26:29

带玉米面包馅的烤小孩


土耳其可能会取代这个经典的节日节日st。

虽然耗费时间,这道菜似乎需要比实际更长的时间;

因为整个房子都充满了这种天堂般的香气,

等待变得几乎无法忍受。


1个整个孩子,清洁和负责人

1批玉米面包馅(见索引)

?杯子融化的黄油


从婴儿身上取下内脏并放在一边。

给孩子的生殖器和肛门所在的空腔填充

使用?每磅肉杯。

将手臂平放在身体上,然后将皮瓣向上拉以封闭腔体。

现在将大腿紧紧系在一起以便握住它所有在一起。

将乳房侧放在一个大型金属烤盘上。

在325°烤箱中烘烤2小时。

取下盖子,将一个烹饪温度计深深地插入其中一个婴儿的臀部,然后将其煮熟,直到温度为止


" Mikhail Teterin" <我们**** @ aldan.algebra.com>写了


我现在正在研究一种情况,其中一个访问器函数,而不是崩溃,返回字段name的偏移量从0(0x4)在上面的例子中)。好像在NULL指针处有合法值0x00000000 ...

有何评论?这是在Solaris-9下的Sparc机器上。谢谢!



取消引用空指针是未定义的,所以如果你完全依赖

任何特定的行为,那么你必须确保你不要这样做。

C的一个问题是访问一个非法的,但不是空的指针是

也是未定义的,但没有办法保护你的函数不被

传递了这样一个指针。

在很多环境中,只是接受该函数可能会崩溃

但是如果传递NULL则可能只返回垃圾就可以了。但是,如果它是一个

问题,那么你应该捕获空指针并考虑做什么

如果通过它。

- -

comp.lang.c.moderated - 审核地址: cl**@plethora.net


Hello!

Consider the following simple accessor function:

typedef struct {
int i;
char name[32];
} MY_TYPE;

const char *
getName(const MY_TYPE *p)
{
return p->name;
}

I always assumed, that checking the passed pointer `p'' against NULL is
redundant, because, if it happens to be NULL at run-time (as a result of
programming error), trying to dereference it would result in SEGFAULT
anyway.

I''m now looking at a situation, where an accessor function, instead of
crashing, returns the offset of the field `name'' from 0 (0x4 in the above
example). As if there is a legal value 0x00000000 at the NULL pointer...

Any comments? This is on a Sparc machine under Solaris-9. Thanks!

-mi
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

解决方案

Whole chicken is much more efficient and inexpensive than buying pieces.

1 tiny human, cut into pieces
2 cups flour
Onion, garlic
Salt
pepper
garlic powder
cayenne pepper
hot sauce, etc.
Oil for frying

Mix milk, eggs, hot sauce in a bowl, add chopped onion and garlic.
Season the meat liberally, and marinate for several hours.
Place seasoned flour in a paper or plastic shopping bag,
drop pieces in a few a time, shake to coat thoroughly,
then deep fry in hot oil (350°) for about 15 minutes.
Drain and place on paper towels.

Miscarriage with Mustard Greens

Why waste it? Otherwise, and in general, use ham or salt pork to season greens.
The technique of smothering greens can be used with many vegetables;
green beans work especially well. Meat is not necessary every day, don?t
be afraid to alter any dish to vegetarian tastes.

1 premature baby, born dead
Large bunch of mustard greens
2 white onions, 1 cup chopped celery
Vegetable oil (or hog fat)
Salt, pepper, garlic, etc.

Lightly brown onions, celery, garlic and meat in large heavy pot.
Add a little water and the greens (which should be thoroughly cleaned and washed).
Smother slowly for at least 2 hours, adding small amounts of water
when it starts to stick.
Stir frequently.
When ready - serve with rice, grilled smoke


26: 1,2
[...else...]

Ye shall eat the flesh of your sons, and the flesh of your daughters ye shall eat.
Leviticus 26:29
Roast Child with Cornbread Stuffing

Turkey may be substituted for this classic holiday feast.
Although time consuming, this dish seems to take longer than it actually does;
as the entire house is filled with such a heavenly aroma,
the waiting becomes almost unbearable.

1 whole child, cleaned and de-headed
1 batch cornbread stuffing (see index)
? cup melted butter

Remove the giblets from the infant and set aside.
Stuff the cavity where the child?s genitals and anus were located
using ? cup per pound of meat.
Tie the arms flat to the body, then pull the skin flaps up to close the cavity.
Now tie the thighs up tight to hold it all together.
Place breast side up in a large metal roasting pan.
Bake in 325° oven covered for 2 hours.
Remove cover, stick a cooking thermometer deep into one of the
baby?s buttocks and cook uncovered till thermo


"Mikhail Teterin" <us****@aldan.algebra.com> wrote


I''m now looking at a situation, where an accessor function, instead of
crashing, returns the offset of the field `name'' from 0 (0x4 in the above
example). As if there is a legal value 0x00000000 at the NULL pointer...

Any comments? This is on a Sparc machine under Solaris-9. Thanks!


Dereferencing a null pointer is undefined, so if you are totally relying on
any particular behaviour, then you must make sure you don''t do it.
One problem with C is that accessing an illegal, though not null, pointer is
also undefined, but there is no way to protect your function from being
passed such a pointer.
In many environments, simply accepting that the function will probably crash
but might just return garbage if passed NULL is OK. However if it is an
issue for you, then you should trap the null pointer and consider what to do
if passed it.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


这篇关于在访问器函数中是NULL检查冗余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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