简体   繁体   English

用new在c ++中分配结构矩阵

[英]allocate a matrix of struct in c++ with new

for a kind of pattern recognition I have defined a very simple struct对于一种模式识别,我定义了一个非常简单的结构

struct object
{
  int color;
  int shape;
}

now I want to allocate a matrix of this struct for all pixel of a picture in c++ (so not with malloc), like I do it for normal fields (matrix) like:现在我想在 C++ 中为图片的所有像素分配这个结构的矩阵(所以不是使用 malloc),就像我为普通字段(矩阵)做的那样:

struct object_A = allocate(objects, dim_x, dim_y);

my first try was:我的第一次尝试是:

struct object_A MyProgramm::allocate(object object_A, int x, int y)
{

         object var[x][y];   

          return var
}

better surely would be something like:更好的肯定是:

struct object_A MyProgramm::allocate(object object_A, int x, int y)
{

    var = new struct object* [x];

    for(int i = 0; i < x; i++)
      var[i] = new struct object[y];

    return var
}

Has anybody an idea?有人有想法吗?

thks.谢谢。 for the creative comment SM, Iam sure, you never startet some years ago!对于创意评论SM,我敢肯定,您几年前从未开始!

Ok, I solved the problems alone and here is the final programm好的,我一个人解决了问题,这是最终的程序

int myprogram::allocate(object** new_object, int x, int y)
{

 if(new_object == NULL)
{
  object** new_object = new object*[x];

 for(int i = 0; i < x; i++)
  new_object[i] = new object[y];

    return 1;
    }

  return 0;
}

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

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