0
私はArduinoのを使用しますが、私は、これは関連する問題をアルドゥイーノされていないが、私は何かを理解していないと思います。 私は次のコードを使用すると、すべてが完璧に動作します:新しい演算子が私のプログラムをハングアップするのはなぜですか?
#include <Queue.h>
typedef struct strRec {
int entry1;
int entry2;
} Rec;
Rec tab[3] = {
{ 700, 7000 },
{ 300, 3000 },
{ 1000, 1000 },
};
Queue q(sizeof(Rec), 2, FIFO); // Instantiate queue
void setup() {
Serial.begin(19200);
unsigned int i;
for (i = 0 ; i < 3 ; i++)
{
Rec rec = tab[i];
q.push(&rec);
}
for (i = 0 ; i < 5 ; i++)
{
Rec rec;
if(q.pop(&rec)) {
Serial.print(rec.entry1);
Serial.print(" ");
Serial.println(rec.entry2);
}
else {
Serial.println("No records");
}
}
}
をしかし、私は新しいを使用してインスタンスを作成するときにプログラムがオペレータの後にハングアップ:なし操作は、それの後にperformerdません。
#include <Queue.h>
typedef struct strRec {
int entry1;
int entry2;
} Rec;
Rec tab[3] = {
{ 700, 7000 },
{ 300, 3000 },
{ 1000, 1000 },
};
Queue* q;
void setup() {
Serial.begin(19200);
q = new Queue(sizeof(Rec), 2, FIFO);
unsigned int i;
for (i = 0 ; i < 3 ; i++)
{
Rec rec = tab[i];
q->push(&rec);
}
for (i = 0 ; i < 5 ; i++)
{
Rec rec;
if(q->pop(&rec)) {
Serial.print(rec.entry1);
Serial.print(" ");
Serial.println(rec.entry2);
}
else {
Serial.println("No records");
}
}
}
しかし、私はキュークラスの任意のメソッドを呼び出すだけで、以下のコードを作成しない場合は
Serial.println("some text before"); //this text can be printed
q = new Queue(sizeof(Rec), 2, FIFO);
Serial.println("some text after"); //this text can be printed too
私はここにメソッド呼び出しを持っている場合、コールは右後にプログラムをハングアップを実行することができます新しい
Serial.println("some text before"); //this text can be printed q = new Queue(sizeof(Rec), 2, FIFO); Serial.println("some text after"); //this text can not be printed and the program hangs here Rec rec = tab[1]; q->push(&rec);
は私もを変更しようとしました
ではなく、メソッド呼び出しの後にコードハングが、何もしないように方法を押してください。 私は自由に文字列をコメントする場合は、私のコードは正常に動作しますので、問題はここにあるが、それはなぜ起こるか私がなぜ
Queue::Queue(uint16_t size_rec, uint16_t nb_recs, QueueType type, bool overwrite)
{
rec_nb = nb_recs;
rec_sz = size_rec;
impl = type;
ovw = overwrite;
if (queue) { free(queue); } // If I comment this string it works as I need
queue = (uint8_t *) malloc(nb_recs * size_rec);
clean();
}
Queue::~Queue()
{
free(queue);
}
void Queue::clean(void)
{
in = 0;
out = 0;
cnt = 0;
}
をunderstadませんか?私は何を理解していないのですか?コンストラクタ内のこの行があり
(通常は悪いだろう格納するローカルへのポインタを渡しますが、途中' pop'作品(とctorのが定義されている方法は)私はそれだけだと思わせる安全コピーされたものへのポインタを取るCのようなAPI)....何も問題はありません。 –
それは[mcve]ですか?私は 'FIFO'の定義を見ることができません。 – Yunnosch
私はキューライブラリへのリンクを追加しました。 –