C ++ sizeof C风格的字符串/char数组-优化 [英] C++ sizeof C-style string / char array - optimization

查看:100
本文介绍了C ++ sizeof C风格的字符串/char数组-优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名大学学生.我主要使用Java,C ++对我来说还是很新的,所以我可能犯了许多愚蠢的错误,并且我即将进行考试.别对我太苛刻.

I'm a student at university. I work mostly with Java, C++ is very new to me, so I probably make many silly mistakes and I have upcoming exams to cope with. Don't be too harsh with me.

注意:我不能使用C ++ std :: string,因为由于大学的工作,我需要使用C字符串!

Note: I can NOT use C++ std::string because I need to work with C-strings due to university tasks!

请参阅我的研究以及我问到的有关指针和const自变量的问题(您会在其中找到这里)我尝试弄乱内存管理,但似乎没有效果,或者我只是误解了sizeof的某些方面或某些元素的实际大小.

Referring to my studies and the question I asked about pointers and const arguments (which you find here) I tried messing around with memory management but it seems it has no effect, or I just misunderstood some aspects about sizeof or actual sizes of certain elements.

这是我班的人:

Person.cpp

Person.cpp

using namespace std;

Person::Person()
{
    Person::name = new (char[64]);
    Person::adress = new (char[64]);
    Person::phone = new (char[64]);

    cout << "standard constructor called; object created, allocated " << sizeof(name) << "+" << sizeof(adress) << "+" << sizeof(phone) << "bytes" << endl;

}

Person::Person(const char *name, const char *adress , const char *phone)
{

    Person::name = new (char[strlen(name)]);
    Person::adress = new (char[strlen(adress)]);
    Person::phone = new (char[strlen(phone)]);


    setName(name);
    setAdress(adress);
    setPhone(phone);

    cout << "general constructor called; object created, allocated " << sizeof(this->name) << "+" << sizeof(this->adress) << "+" << sizeof(this->phone) << "bytes" << endl;
};

Person::Person(Person const &other)
{

    Person::name = new (char[strlen(other.getName())]);
    Person::adress = new (char[strlen(other.getAdress())]);
    Person::phone = new (char[strlen(other.getPhone())]);


    setName(other.getName());
    setAdress(other.getAdress());
    setPhone(other.getPhone());

    cout << "copy constructor called; object created, allocated " << sizeof(name) << "+" << sizeof(adress) << "+" << sizeof(phone) << "bytes" << endl;
};

Person::~Person()
{
    delete [] name;
    delete [] adress;
    delete [] phone;

    cout << "destructor called; object removed" << endl;
};

我试图通过创建带有给定参数字符串长度的C字符串来节省内存. 认为C字符串是一个char数组,保留char将导致保留内存,例如C字符串"John"占用的内存少于C字符串"Jonathan".

I tried to spare memory with creating a C-string with a string length of the given parameters. Thinking that a C-string is a char array, sparing chars would result in sparing memory, e.g. a C-string of "John" takes up less memory than a C-string of "Jonathan".

所以现在我不确定我是否只是对C字符串或char数组有错误的概念,或者我的实现是错误的.

So now I'm not sure if I just got the wrong concept of C-strings or char arrays, or my implementation is just faulty.

我主要创建以下对象:

int main()
{
    Person t;
    t.printPerson();

    cout << "size of t: " << sizeof(t) << endl;

    Person p("John", "some street", "0736182");
    p.printPerson();

    cout << "size of p: " << sizeof(p) << endl;

    Person x(p);
    x.printPerson();

    cout << "size of x: " << sizeof(x) << endl;

    Person y("Jonathan", "Lancaster Ave 53", "3584695364");

    y.printPerson();

    cout << "size of y: " << sizeof(y) << endl;

    cin.get();
};

但是我每个对象的大小都是24,所以每个成员变量的大小是8.为什么会这样?

But I alwas get a size of 24 per object, so 8 for each member variable. Why is that?

谢谢.

推荐答案

我认为您期望sizeof运算符的行为与实际不同.让我们以下面的代码为例:

I think you are expecting the sizeof operator to behave differently than it actually does. Let's take this code, for example:

const char* str = new char[137];

在这里,如果您编写sizeof(str),则可能会得到4或8,这取决于您的系统,因为sizeof(str)会测量指针str本身的字节数,而不是指针中的字节数. str指向的数组.因此,在32位系统上,您可能会得到4,而在64位系统上,您可能会得到8,而与分配的字符数无关.

Here, if you write sizeof(str) you'll probably either get 4 or 8, depending on your system, because sizeof(str) measures the number of bytes of the pointer str itself rather than the number of bytes in the array pointed at by str. So, on a 32-bit system, you'd probably get 4, and on a 64-bit system you'd probably get 8, independently of how many characters you allocated.

不幸的是,C ++无法让您获取动态分配的数组所占用的字符数或内存.您只需要自己跟踪即可.

Unfortunately, C++ doesn't have a way for you to get the number of characters or the memory used up by a dynamically allocated array. You just have to track that yourself.

类似地,在主函数中,编写sizeof(p)时,您要测量的是对象p使用的字节数,而不是p使用的字节总数以及它指向的数组.无论sizeof(p)指向什么字符串,您都将始终获得相同的值.

Similarly, in your main function, when you write sizeof(p), you're measuring the number of bytes used by the object p, not the total number of bytes used by p and the arrays it points at. You'll always get back the same value for sizeof(p) regardless of what strings it points at.

如果您打算在C ++中使用字符串,我强烈建议您对原始C样式字符串使用std::string.它们更易于使用,并且记住它们的长度(因此很难混合strlensizeof),并且如果您的类中包含一堆std::string,则不需要复制构造函数或赋值运算符来处理逻辑以将它们改组.这样可以大大清除您的代码并消除其中的大多数内存错误.

If you're planning on working with strings in C++, I strongly recommend using std::string over raw C-style strings. They're much easier to use, they remember their length (so it's harder to mix up strlen and sizeof), and if you have a class holding s bunch of std::strings you don't need a copy constructor or assignment operator to handle the logic to shuffle them around. That would significantly clean up your code and eliminate most of the memory errors in it.

这篇关于C ++ sizeof C风格的字符串/char数组-优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆