在传递NULL时strdup转储核心 [英] strdup dumping core on passing NULL

查看:119
本文介绍了在传递NULL时strdup转储核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

strdup(null)转储核心.

strdup(null) dumps core.

在ubuntu和freeBSD上都尝试过.

Tried in on ubuntu and freeBSD both.

为什么?它不应该返回null吗?

why? Shouldn't it return null?

char *b = NULL;
a = strdup(b);

这将在strdup调用中转储核心.

This will dump core on strdup call.

推荐答案

没关系.

文档暗示它的参数必须是字符串,如果还有其他含义,例如空指针,任何人都将猜测会发生什么.从本质上讲,将NULL指针传递给strdup时会得到未定义的行为.

The documentation implies that it's argument must be string, if it's something else, such as a null pointer, it's anyones guess what'll happen. In essence, you get undefined behavior when passing a NULL pointer to strdup.

如果将不希望的行为传递给函数,则函数会产生未定义的行为是很正常的.许多标准的C函数,例如strcpy,strlen也不接受空指针.

It's quite normal for functions to yield undefined behavor if you pass them something you're not supposed to. Many standard C function such as strcpy, strlen does not accept null pointers either.

这篇关于在传递NULL时strdup转储核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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