如何查看“char * str =”yoo“;”的内存

时间:2017-07-14 03:21:33

标签: c

据说......

char *str = "yoo";

在内存中看起来像这样:

--------------------------------------------
| 'y' | 'o' | 'o' | '\0' | 'z' | 'b' | 't' | ...
--------------------------------------------

如何在内存中查看“yoo”,如上例所示?

用什么类型的工具打印上述作品?

  

我想在写一个字符串后检查内存。至   看看它在记忆中的样子。

2 个答案:

答案 0 :(得分:0)

我不完全确定你的意思,但也许一些精美的印刷可以提供帮助:

for(i=0; i < MEM_SIZE; i++)
{
    printf("--------------");
    printf("| 0x%X |%x|\n ",&(str+i),*(str+i));
}

当然,您可能希望确保数字位数保持不变,因此所有内容都排列整齐,并且也考虑到不同的大小(也许您希望一次只能通过内存或双字)。

答案 1 :(得分:0)

由于需要管理此类输出的输出字符数,因此创建动态字符串(如java中的StringBuffer)可以使工作更轻松。

以下是一个简单的实现示例:

var zoneId = "America/Antigua";
var zone = DateTimeZoneProviders.Tzdb[zoneId];
var systemClock = SystemClock.Instance;
var zonedClock = systemClock.InZone(zone);
var zoned = zonedClock.GetCurrentZonedDateTime();
Console.WriteLine(zoned);

执行示例:

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

typedef struct StringBuffer {
    size_t capacity;
    size_t length;
    char *str;
} SB;
void *mallocWithCheck(size_t size, const char *mes){
    void *p = malloc(size);
    if(!p){
        fprintf(stderr, "malloc failed, %s.\n", mes);
        exit(EXIT_FAILURE);
    }
    return p;
}
void *reallocWithCheck(void *p, size_t size, const char *mes){
    void *ret = realloc(p, size);
    if(!ret){
        free(p);
        fprintf(stderr, "realloc failed, %s.\n", mes);
        exit(EXIT_FAILURE);
    }
    return ret;
}
SB *SB_makeWithInitSize(size_t size){
    SB *sb = mallocWithCheck(sizeof(*sb), __func__);
    sb->str = mallocWithCheck(sb->capacity = size, __func__);
    sb->length = 0;
    return sb;
}
void SB_drop(SB *sb){
    free(sb->str);
    free(sb);
}
void SB_appendChar(SB *sb, char ch){
    if(sb->length == sb->capacity){
        sb->str = reallocWithCheck(sb->str, sb->capacity *= 2, __func__);
    }
    sb->str[sb->length++] = ch;
}
void SB_appendCString(SB *sb, char *s){
    while(*s)
        SB_appendChar(sb, *s++);
}
void SB_init(SB *sb){
    sb->length = 0;
}
char *SB_toString(SB *sb){
    SB_appendChar(sb, 0);
    sb->length--;
    return sb->str;
}

void memdmp(const void *p, size_t size, size_t width){
    const unsigned char *c = p;
    SB *sb = SB_makeWithInitSize(size * 6);
    char buff[16];

    while(size){
        SB_init(sb);
        for(; size; --size, ++c){
            int field_size;
            if(*c == '\'' || *c == '\\')
                field_size = sprintf(buff, "| '\\%c' ", *c);
            else if(isprint(*c))
                field_size = sprintf(buff, "| '%c' ", *c);
            else
                field_size = sprintf(buff, "| '\\%#o' ", *c);
            if(field_size + sb->length > width-2)
                break;
            SB_appendCString(sb, buff);
        }
        SB_appendChar(sb, '|');
        char *middle = SB_toString(sb);
        size_t len = sb->length;
        //print upper part
        for(size_t i = 0; i < len; ++i)
            putchar('-');
        //print middle part
        printf("\n%s\n", middle);
        //print bottom part
        for(size_t i = 0; i < len; ++i)
            putchar('-');
        puts("");
    }
    SB_drop(sb);
}

int main(void){
    const char str[] = "yoo";
    const int width = 80;
    char buff[16];

    for(int i = 0; i < sizeof(buff); ++i)
        buff[i] = rand() % 256;
    memcpy(buff, str, sizeof str);
    memdmp(buff, sizeof buff, width);
}