c - memset 没有设置 num 个字节?

标签 c gdb memset

在下面的简单程序中,命令指向堆上的 400 字节。然后我将“./search '”复制到命令中,*buffer 指向“'”(单引号)之后的下一个字节。启动缓冲区指向的内存,我使用 memset 将 300 个字节设置为值 0x41(ASCII“A”),然后附加结束单引号。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

int main(int argc, char *argv[]) {
    char *command = (char *)malloc(400);
    bzero(command, 400);

    strcpy(command, "./search \'");
    char *buffer = command + strlen(command);

    memset(buffer, 0x41, 300);
    strcat(command, "\'");

    system(command);
    free(command);
}

但是当我在 gdb 中查看 *command 和 *buffer 时,这就是我所看到的。

char * command 0x601010 "./search '", 'A' <repeats 186 times>...
char * buffer  0x60101e 'A' <repeats 200 times>...

首先我希望它说重复 299 次,其次我希望命令和缓冲区重复具有相似的值。有人可以告诉我我错过了什么吗?

最佳答案

来自GDB manual, section 10.8 Print Settings :

set print elements number-of-elements

Set a limit on how many elements of an array gdb will print. If gdb is printing a large array, it stops printing after it has printed the number of elements set by the set print elements command. This limit also applies to the display of strings. When gdb starts, this limit is set to 200. Setting number-of-elements to zero means that the printing is unlimited.

关于c - memset 没有设置 num 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675293/

相关文章:

c - 是否可以从嵌套函数调用返回的对象中获取指针?

c - 关于 nftw() 和 ftw() 线程安全的问题

c - OpenCL 在 code::blocks 中包括 cl.h on Windows

debugging - 如何更改编译器包含在二进制文件的 DWARF 信息中的调试路径

debugging - 如何使用 gdb debug golang 代码查看 channel 内的内容?

c++ - 为什么 memset sockaddr_in 为 0

c++ - 如何检测无符号整数溢出?

linux - 我如何调试 exec* = -1 ENOENT?

memset 之后的 C++ 放置新

与 realpath() 缓冲区混淆