如何区分一个字符串一个malloced字符串? [英] How to distinguish a malloced string from a string literal?

查看:244
本文介绍了如何区分一个字符串一个malloced字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(纯C)从字符串区分的malloc 编辑字符串,不知道哪个是哪个?严格地说,我试图找到一种方法来检查变量是否是一个malloced字符串或不是,如果是,我要释放它;如果没有,我会让它去。

当然,我可以向后掏code,并确保如果变量的malloc ED或没有,但只是当一个简单的方式中存在的情况下。 ..

编辑:添加的行,使问题更具体

 的char * S1 =1234567890; // 字符串字面量
字符* S2 =的strdup(1234567890); // malloced串
字符* S3;
...
如果(someVar> someVal){
    S3 = S1;
}其他{
    S3 = S2;
}
// ...
//后$ C $的很多很多行C的算法分行...
//我现在记不清S3的:它是分配给S1或S2?
//如果它被分配到s2的,它需要被释放;
//如果没有,释放字符串文字会弹出一个错误


解决方案

  

有没有办法(纯C)来区分一个字符串malloced字符串,


不以任何方式携带,没有。无需担心,虽然;有更好的选择。

当你写code用C这样做,同时使关于谁拥有记忆有力保证。难道呼叫者自己呢?然后,它是他们的责任,以释放它。是否被调用者拥有它?类似的事情。

您写的code大约是保管和所有权链很清楚,你不遇到这样的问题是谁取消分配吗?你不应该觉得有必要说:

  //之后$ C $的很多很多行C的算法分行...
//我现在忘了S3的:它分配到S1或S2?

的解决方案是;别忘了!你在你的code的控制,只是看在页面一点。设计它来对内存泄漏出来的其他功能没有一个清醒的认识到:嘿,你可以阅读这件事情可以刀枪不入,但没有保证,这将十大之后有效或Y这不是你的记忆,把它看作。

或者,也许这是你的记忆。案例分析;您的来电的strdup 的strdup 让你知道(通过文档),这是你的责任,以释放它返回到您的字符串。你怎么做到的就是你的,但你最好的选择是限制其范围,以尽可能窄,只保留它周围尽可能短的时间是必要的。

这需要时间和实践这成为第二天性。您将创建一个处理内存的一些项目的不佳的你善于它。没关系;你在一开始的错误会教你什么的的做,你会在将来避免重复他们(希望!)

此外,作为@Lasse提到的评论,您不必担心 S3 ,这是一个指针的一个拷贝的 ,而不是记忆的整个块。如果您在拨打免费 S2 S3 你结束了未定义的行为。

Is there a way (in pure C) to distinguish a malloced string from a string literal, without knowing which is which? Strictly speaking, I'm trying to find a way to check a variable whether it's a malloced string or not and if it is, I'm gonna free it; if not, I'll let it go.

Of course, I can dig the code backwards and make sure if the variable is malloced or not, but just in case if an easy way exists...

edit: lines added to make the question more specific.

char *s1 = "1234567890"; // string literal
char *s2 = strdup("1234567890"); // malloced string
char *s3;
...
if (someVar > someVal) {
    s3 = s1;
} else {
    s3 = s2;
}
// ...
// after many, many lines of code an algorithmic branches...
// now I lost track of s3: is it assigned to s1 or s2?
// if it was assigned to s2, it needs to be freed;
// if not, freeing a string literal will pop an error

解决方案

Is there a way (in pure C) to distinguish a malloced string from a string literal,

Not in any portable way, no. No need to worry though; there are better alternatives.

When you write code in C you do so while making strong guarantees about "who" owns memory. Does the caller own it? Then it's their responsibility to deallocate it. Does the callee own it? Similar thing.

You write code which is very clear about the chain of custody and ownership and you don't run into problems like "who deallocates this?" You shouldn't feel the need to say:

// after many, many lines of code an algorithmic branches...
// now I forgot about s3: was it assigned to s1 or s2?

The solution is; don't forget! You're in control of your code, just look up the page a bit. Design it to be bulletproof against leaking memory out to other functions without a clear understanding that "hey, you can read this thing, but there are no guarantees that it will be valid after X or Y. It's not your memory, treat it as such."

Or maybe it is your memory. Case in point; your call to strdup. strdup let's you know (via documentation) that it is your responsibility to deallocate the string that it returns to you. How you do that is up to you, but your best bet is to limit its scope to be as narrow as possible and to only keep it around for as short a time as necessary.

It takes time and practice for this to become second nature. You will create some projects which handle memory poorly before you get good at it. That's ok; your mistakes in the beginning will teach you exactly what not to do, and you'll avoid repeating them in the future (hopefully!)

Also, as @Lasse alluded to in the comments, you don't have to worry about s3, which is a copy of a pointer, not the entire chunk of memory. If you call free on s2 and s3 you end up with undefined behavior.

这篇关于如何区分一个字符串一个malloced字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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