简体   繁体   English

正确使用memset和memcpy在C ++中初始化字符数组

[英]Using memset and memcpy correctly to initialize a character array in C++

I wanted to initialize a character array with the data from a character pointer. 我想使用来自字符指针的数据来初始化字符数组。 I wrote the following code for this: 我为此编写了以下代码:

(kindly excuse what I am doing with the structure and all .. actually this code is supposed to fit into something bigger and hence the strangeness of that structure and its use) (请原谅我对结构和所有结构所做的事情。实际上,此代码应该适合更大的结构,因此该结构及其用途很奇怪)

#include <iostream>
#include <string>

struct ABC 
{
    char a;
    char b;
    char c[16];
};

int main(int argc, char const *argv[])
{
    struct ABC** abc;
    std::string _r = "Ritwik";
    const char* r = _r.c_str();

    if (_r.length() <= sizeof((*abc)->c))
    {
        int padding = sizeof((*abc)->c) - _r.length();

        std::cout<<"Size of `c` variable is : "<<sizeof((*abc)->c)<<std::endl;
        std::cout<<"Value of padding is calculated to be : "<<padding<<std::endl;

        char segment_listing[ sizeof((*abc)->c)]; 

        std::cout<<"sizeof segment_listing is "<<sizeof(segment_listing)<<std::endl;

        memcpy(segment_listing, r, _r.length());
        memset( (segment_listing + _r.length()), ' ', padding);

        std::cout<<segment_listing<<std::endl;

    }
    return 0;
}

However, when I run my code I get these wierd characters at the end of my string: 但是,当我运行代码时,我在字符串的末尾得到了这些奇怪的字符:

(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik          °×
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik           Ñ
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik          g
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik          pô
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik          àå
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik           »
(rh4dev01:~/rough) rghosh> ./crptr
Size of `c` variable is : 16
Value of padding is calculated to be : 10
sizeof segment_listing is 16
Ritwik          pZ

Can you please explain why that is happening ? 你能解释为什么会这样吗? Since I am printing only a character array taht is only 16 characters in length, shouldn't only 16 characters get printed ? 因为我只打印一个字符数组,所以长度仅为16个字符,难道不应该只打印16个字符吗? Where are those two (sometimes zero and sometimes one) characters coming from ? 那两个(有时是零,有时是一个)字符从哪里来?

More importantly, am I corrupting any memory (that does not belong to my character array c ) by my padding ? 更重要的是,我是否通过填充破坏了任何内存(不属于我的字符数组c )?

Your string needs to be NUL terminated. 您的字符串需要NUL终止。

    memcpy(segment_listing, r, _r.length());
    memset( (segment_listing + _r.length()), ' ', padding-1);
    segment_listing[_r.length() + padding - 1] = '\0';

Perhaps you would be better served by using snprintf() , which will add the terminator for you: 使用snprintf()可能会更好地为您服务,它将为您添加终止符:

    snprintf(segment_listing, sizeof(segment_listing), "%-*s",
             (int)sizeof(segment_listing)-1, r);

AC string is terminated by a 0 byte, which you havn't accounted for anywhere. AC字符串以0字节结尾,您在任何地方都没有考虑。 You need to terminate your string with the value 0, and you'll have to take that extra byte in account in all your calculations. 您需要以0终止字符串,并且在所有计算中都必须考虑到该额外字节。

segment_listing没有空字符

const int SIZE = 16;  //or 17 if you want 16 + null char
//pre-initialise array - then will automatically be null terminated
char segment_listing[SIZE] = {0};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM