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 ...
随机推荐
- 卷积在图像处理中的应用(转自https://medium.com/@irhumshafkat/intuitively-understanding-convolutions-for-deep-learning-1f6f42faee1)
直观理解深度学习的卷积 探索使他们工作的强大视觉层次 近年来强大且多功能的深度学习框架的出现使得可以将卷积层应用到深度学习模型中,这是一项非常简单的任务,通常可以在一行代码中实现. 然而,理解卷积 ...
- git 命令(基础篇)的本质理解
主要命令 1. 提交,git commit 本质:创建一个节点(node),标志了当前位置(node)与以前的node存在不同之处,如下图中的 c0 <-- c1 <-- c2 等等 图中 ...
- nginx 502错误 upstream sent too big header while reading response header from upstream
原本的设置是 proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; 在这种配置下,使用fiddler进行抓包分 ...
- NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成
本篇内容属于非实用性(拿来即用)介绍,如对框架设计没兴趣的朋友,请略过. 快一个月没有写博文了,最近忙着两件事; 一:阅读刘墉先生的<说话的魅力>,以一种微妙的,你我大家都会经常遇见 ...
- Android Bitmap操作问题之Canvas: trying to use a recycled bitmap
一.Bitmap.recycle 方法被弃用 在Android中,Bitmap的存储分为两部分,一部分是Bitmap的数据,一部分是Bitmap的引用.在Android2.3时代,Bitmap的引用是 ...
- Android开发工程师文集-1 小时学会Widget小组件开发
前言 大家好,给大家带来Android开发工程师文集-1 小时学会Widget小组件开发的概述,希望你们喜欢 学会用Widget (小组件) Widget小组件很方便,很快捷,可以个性化,自己定制,相 ...
- 用redux-thunk异步获取数据
概述 最近学习redux,打算用redux-thunk给todo添加异步获取数据组件.记录下来,供以后开发时参考,相信对其他人也有用. 注意: 在todo下方,我异步获取我的react博客的标题,点击 ...
- React.js 三周 -- 入门到搭建团队基础项目
吐槽 公司自己的产品,由于历史遗留问题,前端一直是和java放到一个项目里写的. 导致了,前端就被死死的绑在了IDEA战车上.想要看页面效果,先起几个java服务.想要调试一个改动,重启个java服务 ...
- jQuery应用实例4:下拉列表
应用场景:左侧是已有商品,右侧是未有商品,选择其中的内容点击箭头即可互换: 点击大箭头则全部内容去另一边,或者双击已有商品的选项也会加入右边: 代码实现: <!DOCTYPE html> ...
- css 如何“画”一个抽奖转盘
主要描述的是如何运用 css 绘制一个抽奖转盘,并运用原生 js 实现转盘抽奖效果. 先来张效果图: 布局 一般来说,转盘一般有四个部分组成:外层闪烁的灯.内层旋转的圆盘.圆盘上的中奖结果.指针. 所 ...