如何在Java中实现带有指针和内存分配的算法

发布于 2024-11-25 13:46:34 字数 498 浏览 1 评论 0原文

我有一个 C++ 算法,我需要在 Java 中实现类似的东西。我在内存分配方面遇到问题。例如,如何将以下代码片段从 C++ 迁移到 Java?

size_x = 3; size_y = 6; 
double **Data, *pDataData;
Data = (double **)malloc(size_x*sizeof(double *)+size_x*size_y*sizeof(double));
for (i = 0, pDataData = (double *)(Data+size_x); i < size_x; i++, pDataData += size_y)
Data[i]=pDataData;

我知道对于一个简单的 malloc 来说: char *x = (char *) malloc(256); 在Java中,我会说: ByteBuffer x = ByteBuffer.分配(250);

对于更复杂的事情,我会感到困惑。

先感谢您

I have an algorithm in C++ and I need to implement something similar in Java. I'm having trouble with memory allocation. How can I migrate the following snippet for example, from C++ to Java?

size_x = 3; size_y = 6; 
double **Data, *pDataData;
Data = (double **)malloc(size_x*sizeof(double *)+size_x*size_y*sizeof(double));
for (i = 0, pDataData = (double *)(Data+size_x); i < size_x; i++, pDataData += size_y)
Data[i]=pDataData;

I know that for a simple malloc like:
char *x = (char *) malloc(256);
In Java, I would say:
ByteBuffer x = ByteBuffer.allocate(250);

For anything more complicated, I get confused.

Thank you in advance

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

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

发布评论

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

评论(2

遮云壑 2024-12-02 13:46:34

那是一个二维数组。

double Data[][] = new double[size_x][size_y];

That's a 2 dimensional array.

double Data[][] = new double[size_x][size_y];
摇划花蜜的午后 2024-12-02 13:46:34

简短的回答:在 Java 或 C++ 中做这样的事情既没有必要也不可取。自从类的发明以来,当您需要复杂的结构时,您应该创建一个类,而不是分配无定形的内存块,然后将通过偏移量而不是名称检索的内容放入该内存中。

所以看起来您正在尝试分配一个 size_x 指针数组到双精度数,然后分配一个大小为 size_x x size_y 双精度数的二维数组,指针指向表中的连续行?抱歉,如果我不明白你想做什么。在我看来,这正是 Java 方式优越的原因:复杂的结构并不难理解。

在 Java 中,不需要指向行的指针。因此,在 Java 中,这将变成:

public class MyDataData
{
  double[][] table;

  public MyDataData(int size_x, int size_y)
  {
    table=new double[size_x][size_y];
  }
}

要调用它,您只需编写

MyDataData whatever=new MyDataData(3,6);

就这样。如果您需要对行的引用,您只需获取 table[x]。

不要尝试在 Java 中模拟 malloc。事情不是这样的。只需定义您需要的类并使用“new”来创建一个,Java 就会为您分配内存。

Short answer: It is neither necessary nor desirable to do such a thing in Java or in C++. Since the invention of classes, when you need a complex structure, you should create a class, not allocate an amorphous blob of memory and then put things in that memory that are retrieved by an offset rather than a name.

So it looks like you're trying to allocate an array of size_x of pointers to doubles, and then a two-dimensional array of size size_x by size_y of doubles, with the pointers pointing to successive rows in the table? Sorry if I'm not understanding what you're trying to do. This is exactly why the Java way is, in my humble opinion, superior: Complex structures are not that hard to figure out.

In Java, there would be no need to have the pointers to the rows. So in Java this would just turn into:

public class MyDataData
{
  double[][] table;

  public MyDataData(int size_x, int size_y)
  {
    table=new double[size_x][size_y];
  }
}

To invoke it you'd just write

MyDataData whatever=new MyDataData(3,6);

And that would be about it. If you need a reference to a row, you'd just get table[x].

Don't try to simulate malloc in Java. That's not how it's done. Just define the class you need and use "new" to make one, and Java will worry about allocating the memory for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文