C++打印内存数据

本文介绍了一种使用C++编程的方式,参照GDB调试器的风格,逐字节打印内存数据,同时展示10进制无符号和16进制表示。代码中引用了两个来自Linux内核的函数,并给出了完整的实现代码,运行结果显示每行打印8字节的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参照gdb的风格, 逐字节打印,同时打印10进制无符号和16进制,一行打印8字节

其中

int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
int scnprintf(char *buf, size_t size, const char *fmt, ...)

两个函数是从Linux内核源码复制的

直接上完整代码:

#include <cstdio>
#include <cstdarg>

#define unlikely(expr) __builtin_expect(!!(expr), 0)
#define likely(expr) __builtin_expect(!!(expr), 1)

/**
 * vscnprintf - Format a string and place it in a buffer
 * @buf: The buffer to place the result into
 * @size: The size of the buffer, including the trailing null space
 * @fmt: The format string to use
 * @args: Arguments for the format string
 *
 * The return value is the number of characters which have been written into
 * the @buf not including the trailing '\0'. If @size is == 0 the function
 * returns 0.
 *
 * If you're not already dealing with a va_list consider using scnprintf().
 *
 * See the vsnprintf() documentation for format string extensions over C99.
 */
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
	int i;

	i = vsnprintf(buf, size, fmt, args);

	if (likely(i < size))
	{
		return i;
	}
	if (size != 0)
	{
		return size - 1;
	}
	return 0;
}

/**
 * scnprintf 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值