C++静态维度数组的动态分配数组

发布于 2024-08-04 09:00:39 字数 310 浏览 13 评论 0原文

我需要创建一个包含可变数量的“char[2]”的结构,即 2 个字符的静态数组。

我的问题是,如何为 x 个 char[2] 分配内存。

我尝试了这个(假设定义了 int x ):(

char** m = NULL;
m = new char[x][2];
...
delete [] m;

它不起作用)

我意识到我可以使用 std::vector作为一个容器,但我很好奇如何使用原始指针来完成它。

我对 C++ 很陌生,正在努力学习。

I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars.

My question is, how do I allocate memory for x number of char[2].

I tried this (assuming int x is defined):

char** m = NULL;
m = new char[x][2];
...
delete [] m;

(it didn't work)

I realise I could use std::vector<char[2]> as a container, but I'm curious as to how it would be done with raw pointers.

I am very new to C++ and trying to learn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃不饱 2024-08-11 09:00:39

在您的代码中,“m”的类型与您的“new”调用不匹配。你想要的是:

char (*m)[2] = NULL;
m = new char[x][2];
...
delete [] m;

m 是一个指向 2 个字符数组的指针,你调用 new 来获取 x 个 2 个字符数组的数组,并将 m 指向第一个字符。

In your code, the type of 'm' doesn't match your 'new' call. What you want is:

char (*m)[2] = NULL;
m = new char[x][2];
...
delete [] m;

m is a pointer to arrays of 2 chars, and you call new to get an array of x arrays of 2 chars and point m at the first one.

桜花祭 2024-08-11 09:00:39

我相信下面的代码比 char[n][2] 更具可读性:

typedef char wchar[2];   // array of two chars
const size_t n = 100;    // some const
wchar* x = new wchar[n]; // array of wchars, where wchar is array of two chars

// here is still a problem that you could write the following
x[5][5] = 0;             // not what you expected?

delete[] x;              // clean up

如果我们了解 wchar 的内部结构,那么如果我们将其声明如下,代码将更具可读性:

// using struct is just gives names to chars in wchar, without performance drop
struct wchar {
  char h;
  char l;
};

...

const size_t n = 100;    // some const
wchar* x = new wchar[n]; // array of wchars

x[0].h = 0;
x[0].l = 0;

delete[] x;              // clean up

最后,因为我们使用 C++,不需要使用 C 数组:

const size_t n = 100;    // some const   
typedef std::tr1::array<wchar, n> my_arr;
my_arr* x = new my_arr;

(*x)[0].h = 0;
(*x)[0].l = 0;

delete x;

还有一个非常安全的选项,带有编译时间范围检查:

template<int n_max>
struct array_n {
    char v[2*n_max];

    template<size_t n, size_t s> 
    char& get() {
        BOOST_STATIC_ASSERT( s < 2 );
        BOOST_STATIC_ASSERT( n < n_max );
        return v[n*2+s];
    };  
};

int main( int argc, char**argv)
{
    const size_t n = 100;    // some const   
    typedef array_n<100> my_arr;
    my_arr* x = new my_arr;

    x->get<10, 1>() = 0;   // ok
    x->get<50, 0>() = 0;   // ok
    x->get<10, 2>() = 0;   // compile time error
    x->get<500, 0>() = 0;  // compile time error

    delete x;
}

I believe that the following code is more readable than char[n][2]:

typedef char wchar[2];   // array of two chars
const size_t n = 100;    // some const
wchar* x = new wchar[n]; // array of wchars, where wchar is array of two chars

// here is still a problem that you could write the following
x[5][5] = 0;             // not what you expected?

delete[] x;              // clean up

If we aware of the internal structure of wchar, the code will be more readable if we declare it as follows:

// using struct is just gives names to chars in wchar, without performance drop
struct wchar {
  char h;
  char l;
};

...

const size_t n = 100;    // some const
wchar* x = new wchar[n]; // array of wchars

x[0].h = 0;
x[0].l = 0;

delete[] x;              // clean up

And finally, because we use C++, no need to use C arrays:

const size_t n = 100;    // some const   
typedef std::tr1::array<wchar, n> my_arr;
my_arr* x = new my_arr;

(*x)[0].h = 0;
(*x)[0].l = 0;

delete x;

One more pretty safe option with compile time range checking:

template<int n_max>
struct array_n {
    char v[2*n_max];

    template<size_t n, size_t s> 
    char& get() {
        BOOST_STATIC_ASSERT( s < 2 );
        BOOST_STATIC_ASSERT( n < n_max );
        return v[n*2+s];
    };  
};

int main( int argc, char**argv)
{
    const size_t n = 100;    // some const   
    typedef array_n<100> my_arr;
    my_arr* x = new my_arr;

    x->get<10, 1>() = 0;   // ok
    x->get<50, 0>() = 0;   // ok
    x->get<10, 2>() = 0;   // compile time error
    x->get<500, 0>() = 0;  // compile time error

    delete x;
}
烂人 2024-08-11 09:00:39

您最终将确定数组的大小,然后使用 new,并将其视为二维数组。

但是,为了对此进行良好的讨论,您可能需要查看:
http://www.velocityreviews.com/forums/t283481-dynamic-多维数组.html

You would end up determining the size of the array, and then use new, and treat it as a two-dimensional array.

But, for a good discussion on this you may want to look at:
http://www.velocityreviews.com/forums/t283481-dynamic-multidimensional-arrays.html

送你一个梦 2024-08-11 09:00:39
unsigned x=10;
typedef char A2[2];
A2 *m=new A2[x];
m[0][1]='a';
m[9][0]='b';
delete[] m;

C 多维数组(其中除第一个维度外的所有维度都是常数)是连续排列的。

如果你想要一个(可能是锯齿状的)多维数组,它是一维数组的一维数组,那么你必须循环:

  char **m=new char *[x];
  for (unsigned i=0;i<x;++i) m[i]=new char[2];
  ...
  for (unsigned i=0;i<x;++i) delete[] m[i];
  delete[] m;
unsigned x=10;
typedef char A2[2];
A2 *m=new A2[x];
m[0][1]='a';
m[9][0]='b';
delete[] m;

C multi-dimensional arrays (where all but the first dimensions are constant) are laid out contiguously.

If you want a (potentially jagged) multi-dimensional array which is a 1d array of 1d arrays, then you have to loop:

  char **m=new char *[x];
  for (unsigned i=0;i<x;++i) m[i]=new char[2];
  ...
  for (unsigned i=0;i<x;++i) delete[] m[i];
  delete[] m;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文