c++ - 作为参数传递给 C++ 中的类时的内存丢失

标签 c++ arrays class visual-c++ arguments

我用一个模板定义了一个类,该模板定义了一个泛型数组类型 T,元素个数为 N。我有另一个类,它有一个该数组的实例作为成员。当我尝试使用 setString 函数时,我传递的数组从 15 个元素任意变为 4 个元素。

// testClassArraySize.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

template<class T, int N>
class CArray {
public:
    T arr[N];
    CArray(void) {/*arr=(T *)malloc(sizeof(T)*N);*/
        if (arr == NULL) {
            cout << "allocation error\n";
        }
    }
    ;
    //CArray (int n) {arr=new T [n]; if(arr==NULL){exit(0); cout<<"allocation error\n";}};
    CArray operator=(const T *);
    T operator[](const int i) {
        return arr[i];
    }
    ;
};

template<class T, int N>
CArray<T, N> CArray<T, N>::operator=(const T *srce) {
    size_t x = sizeof(arr);
    size_t y = sizeof(srce);
    for (int j = 0; j < sizeof(arr); j++) {
        if (j > sizeof(srce)) {
            arr[j] = 0;
            break;
        }
        arr[j] = srce[j];
    }
    return *this;
}

class myTestClass {
private:
    CArray<char, 15> myString;
public:
    myTestClass setString(char set[15]) {
        myString = set;
        size_t x = sizeof(set);
        return *this;
    }
    ;
};

int main() {
    myTestClass myObject;
    myObject.setString("helloWorld");
    return 0;
}

有人知道为什么吗?

最佳答案

这有几个问题,但您可能会看到的是一行

CArray<T, N> CArray<T, N>::operator= (const T *srce)

注意:const T* source是一个指针,因此sizeof(srce)是指针的大小。我猜您使用的是 32 位系统?

sizeof 为您提供对象的大小(即“存储区域”)。对于数组,这是整个数组的大小(以字节为单位)。 sizeof(int[10]) == sizeof(int) * 10。指针本身就是一个对象,其大小取决于 C++ 实现(操作系统、编译器等)。在 32 位系统上,它通常是 4 个字节。 sizeof( char* ) 因此是 4 个字节,而不是您传递给函数的数组的长度,即 sizeof( (char*)(char[10]) ) 仍然是 4 字节,而不是 10。

您可能看到的另一个问题(但仅通过调试/跟踪)是 setString(char set[15]) 被解析为 setString(char* set)。因此,x = sizeof(set) 解析为 x = sizeof(char*),通常为 4。

您将“helloWorld”传递给setString,它需要一个15-item char arraychar*;我会说这不是一个好主意,因为“helloWorld”的类型为 char const[10](注意 const)。 获取 15 个字符的数组的正确语法是 char (&set)[15]

如果你添加一个模板成员函数,你可以更优雅地做到这一点:

// in class CArray
template < std::size_t length >
CArray& operator= (const T (&srce)[length]);    // note I return a reference, so no copying

这样,您将获得数组大小作为模板参数。注意:由于我在赋值操作中使用了 const T,因此您还需要在 setString 中强制执行 const。

template < class T, int N >
template < std::size_t srce_length >
CArray < T, N >& CArray < T, N > :: operator= (const T (&srce)[srce_length])
{
    for (int j = 0; j < N; j++) {    // N is the own length
        if (j >= srce_length) {    // note the >= instead of >, and srce_length instead of sizeof
            arr[j] = 0;
            break;
        }
        arr[j] = srce[j];
    }
    return *this;
}

关于c++ - 作为参数传递给 C++ 中的类时的内存丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12770648/

相关文章:

c++ - opengl - 如何在 3D 不规则物体上放置纹理

c++ - 将任何字符串转换为固定大小的字符串

c++ - 相邻打印 2 个数组

python - Pandas 无法从 Numpy 时间戳数组创建 DataFrame

python - 什么是 DynamicClassAttribute 以及如何使用它?

c# - 将 C# 中的 "from ... in "迭代转换为 C++ (Qt)

C++:setsockopt() 可以被信号忽略吗?

javascript - 如何在 Spring 中正确创建 JavaScript 日期数组?

r - getMethod ("summary"中的错误,签名 = "FitDiff")

php - 访问同一类的另一个对象的 protected 属性的方法