简体   繁体   English

分配数组指针C ++等效代码

[英]allocating array pointer c++ equivalent code

I'm modifying some code I found to be inside a C++ object. 我正在修改一些发现在C ++对象内部的代码。 Message is a c++ class. 消息是c ++类。

The original line of code looks like this: 原始代码行如下所示:

unsigned char fifoQueue[256 * sizeof(Message)] = {0};

Since I'm putting it into an object, I'm doing this: 由于我将其放入对象中,因此我正在这样做:

///in header
unsigned char fifoQueue;

///in object initializer
fifoQueue = new unsigned char[256 * sizeof(Message)];

Somehow I don't think that is correct. 不知何故,我认为那是不正确的。 What's the correct implementation to get the same result? 什么是获得相同结果的正确实现? I'm just a bit cloudy about how this works - In the given example fifoQueue is a pointer to a memory location, correct? 我对它的工作方式有点困惑-在给定的示例中,fifoQueue是指向内存位置的指针,对吗? Should my object have the fifoQueue instance variable as a pointer to a "Message" array, instead? 我的对象应该使用fifoQueue实例变量作为指向“消息”数组的指针吗?

Thanks! 谢谢!

/////// Okay, I'm adding some information here that is relevant to the way this is being used. ///////好的,我在这里添加一些与使用方式有关的信息。 Sorry for not including this before. 抱歉,之前未包含此内容。

There is a method that accesses this value as a pointer and increments it based on a read/write location. 有一种方法可以将该值作为指针访问,并根据读/写位置将其递增。 So I need the new initializer such that this method works correctly. 因此,我需要新的初始化程序,以便此方法正常工作。

Message* Fifo::getMessageToWrite(){

    Message* base = (Message*)fifoQueue;
    return new(base + (fifoWritePtr & 255)) Message();

}

From the looks of it, you want to pass objects of type Message through a queue. 从外观上,您希望通过队列传递Message类型的对象。 For this, the proper C++ mechanism is to use std::queue<Message> : it will grow to an appropriate size when pushing new messages into the queue. 为此,正确的C ++机制是使用std::queue<Message> :当将新消息推送到队列中时,它将增长到适当的大小。 Unlike the std::vector<T> proposed in other answers, this actually uses a std::deque<Message> under the hood (you can use a std::deque<Message> directly but if you want a queue just use a queue). 与其他答案中提出的std::vector<T>不同,这实际上是在引擎盖下使用std::deque<Message> (您可以直接使用std::deque<Message> ,但是如果要排队,只需使用队列)。

The advantage of using a std::queue<Message> is that the object inside this queue stays put while they keep being shuffled around in a std::vector<Message> : while std::vector<T> only supports efficient (ie O(1)) addition/removal at the back as is eg used for a stack (LIFO), std::deque<T> supports efficient addition/removal at both ends as is needed for a queue (FIFO). 使用std::queue<Message>的优点在于,当队列中的对象在std::vector<Message>不断被拖曳时,该对象保持放置状态:而std::vector<T>仅支持高效(即O(1))位于后面,例如用于堆栈(LIFO)的添加/删除, std::deque<T>支持队列(FIFO)所需的两端的高效添加/删除。 I think the complexity of adding/removing to a std::deque<T> is only amortized constant but this is still better than linear complexity you'd get with a std::vector<T> when using it as a FIFO. 我认为添加/删除std::deque<T>的复杂度仅是摊销常量,但这仍比将std::vector<T>用作FIFO时得到的线性复杂度更好。

The second version declares an unsigned char and tries to assign a unsigned char* to it. 第二个版本声明一个unsigned char并尝试为其分配一个unsigned char* This is not going to work and even so it is not clear to me why you would allocate enough space for 256 intS and assign them to a unsigned char* unless you are doing something very low level. 这将无法正常工作,即使如此,我也不知道为什么您要为256个intS分配足够的空间,然后将它们分配给一个unsigned char*除非您执行的级别很低。 A std::vector is probably the better choice anyway. 无论如何, std::vector可能是更好的选择。

Piece of cake: std::vector is more preferable way instead of manual dynamic memory allocation (plain-old pointers). 小菜一碟: std :: vector比手动动态内存分配(普通指针)更可取。

#include <vector>
...
std::vector<char> fifoQueue(256);

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

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