How to distinguish between strings in heap or literals?
Question:
I have a use case where I can get pointers of strings allocated either in memory or literals. Now the latter can't be freed so that's a problem if I pass the wrong one. Is there a way to know which one is allocated and which not?
char *b = "dont free me!";
if(!IS_LITERAL(b)) {
free(b);
}
I imagine something like that.
My example:
Scenario 1: literal
char *b = "dont free me!";
scruct elem* my_element = mylib_create_element(b);
// do smth
int result = mylib_destroy_element(my_element); // free literal, very bad
Scenario 2: in heap
char *b = malloc(sizeof(char)*17); // example
strncpy(b, "you can free me!",17);
scruct elem* my_element = mylib_create_element(b);
// do smth
int result = mylib_destroy_element(my_element); // free heap, nice
How the user calls mylib_create_element(b); is not under my control. If he frees before
mylib_destroy_element it can crash. So it has got to be mylib_destroy_element that cleans up.
Answer:
I've had a similar case recently. Here's what I did:
If you're making an API that accepts a string pointer and then uses it to create an object (mylib_create_element), a good idea would be to
copy the string to a separate heap buffer and then free it at your discretion. This way,
the user is repsonsible for freeing the string he used in the call to your API, which makes sense. It's his string, after all.
Note that this won't work if your API depends on the user changing the string after creating the object
How to distinguish between strings in heap or literals?的更多相关文章
- IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings
Dictionary Strings 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictio ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- 《ruby编程语言》笔记 1
赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...
- Javascript.ReactNative-2-javascript-syntax-in-react-native
JavaScript Syntax in React Native Contents: Arrow Function Let+Const Default + Rest + Spread Destruc ...
- 读书笔记, Python - python-tricks-buffet-awesome-features
To be a Pythonista 1. assert syntax: assert expression1 [",", expression2] 大致相当于 if __debu ...
- The Dangers of the Large Object Heap(转载,LOH内存碎片情景重现)
原文地址:https://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/ You'd h ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- [Java Basics] Stack, Heap, Constructor, I/O, Immutable, ClassLoader
Good about Java: friendly syntax, memory management[GC can collect unreferenced memory resources], o ...
- Core Java Volume I — 3.6. Strings
3.6. StringsConceptually, Java strings are sequences of Unicode characters(Java的字符串是一个Unicode序列). Fo ...
随机推荐
- Codeforces791 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- ReactNative学习笔记(一)环境搭建
前言 本文开发环境为Windows,目标平台为Android,react-native版本为0.35.0. 环境搭建 注意,本文不是按照官网的教程来的,官网说必须安装什么Chocolatey,我懒得鸟 ...
- first one
我说一句话你就知道我是干什么的 hello world
- JQuery Mobile - 动态修改select选择框的选中项
<label for="day">选择天</label> <select name="day" id="day" ...
- WebRTC学习之 Intel® Collaboration Suite for WebRTC源码流程解读
年后回来,因为新项目的需求,开始了解WebRTC相关的知识.目前接触的是Intel® Collaboration Suite for WebRTC.刚开始看SDK发现很多概念是我目前不知道的,于是恶补 ...
- 使用 PLSQL 提示动态执行表不可访问,本会话的自动统计被禁止
使用PLSQL,第一次执行表的select操作的时候,提示"动态执行表不可访问,本会话的自动统计被禁止",如上图: 这种问题,一看就是当前连接用户没有对sys用户下的表v$sess ...
- Spring,为内部方法新起一个事务,此处应有坑。
事务的作用,使我们操作能够连贯起来.而spring则是提供了一个更简单的方法,只要使用 @Transactional 一个注解,就可以保证操作的连贯性了. 普通用法,稍后再说,这里要说的是: 在最外面 ...
- apache环境之困扰,Rewrite导致无法加载多个不同的.html文件
又是一个项目,为访问多个纯静态html页面h5游戏页,能够做一些简单分享和跳转即可.原本是一个简单得不能的项目,但是却多生了事端. 我按照apache的惯例,将文件上传到服务器的DocumentRoo ...
- opus代码解析
opus的初始化函数如下所示,在初始化的过程中,从代码结构上来看,这里主要完成是内存的申请,基本参数的定义 cOpusEncoder *opus_encoder_create(opus_int32 F ...
- python函数练习——个人信息修改
修改个人信息程序 在一个文件里存多个人的个人信息,如以下 1.输入用户名密码,正确后登录系统 ,打印 1. 修改个人信息 2. 打印个人信息 3. 修改密码 2.每个选项写一个方法 3.登录时输错3次 ...