c - 如何打印内存和缓冲区的长度?
问题描述
我需要打印内存和缓冲区的大小?
他们是另一种方式吗?
size_t len;
len = strlen(input);
printf("******************len is : %d\n", len)
我刚刚编辑了“输入”实际上是“缓冲区”的问题
解决方案
可以使用自定义的malloc方法,来自redis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* zmalloc(size_t size) {
void* ptr = malloc(size + sizeof(size_t));
*((size_t*)ptr) = size;
return ptr + sizeof(size_t);
}
size_t zmalloc_size(void* ptr) {
void* real_ptr = ptr - sizeof(size_t);
return *((size_t*)real_ptr);
}
int main() {
void* ptr = zmalloc(100);
printf("%zu", zmalloc_size(ptr));
}
推荐阅读
- json-deserialization - 使用 jackson 反序列化 json 文件:UnresolvedForwardReference
- elasticsearch - 日志发送的Logstash延迟
- list - Flutter 'map' 动态调用 null。接收方:'_Future 的实例
' 参数:['(dynamic) => Builder'的实例] - excel - 如何将循环的值添加到 Excel 中表格的新行?
- sql - Postgres | 如何从包含多个 JSON 对象的 JSON 数组中提取值
- vb.net - VB.NET中无法识别的数据库格式错误
- elasticsearch - .kibana 索引在 Kibana 中提供哪些信息?
- python - 在 Blender 中列出到记事本
- python - Flask 使用 asyncio 上传到 s3
- excel - 如何显示“并非所有项目都显示”VBA自动过滤器