设置数组指针并存储为类成员 [英] pasing an array pointer and storing as a class member

查看:96
本文介绍了设置数组指针并存储为类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(对来自受审核小组的交叉发帖道歉......我确定你理解了




您好,我正在通过数组到一个构造函数中并希望使用它作为

a指针并将其存储为类成员以供将来使用。到目前为止,我是

只是导致崩溃,伪造的代码如下:

double block [8192];

foo = MyClass(block );


// //


MyClass {


public:


MyClass(double * blk){//构造函数

inBlk = blk;


}

流程{

//将定期调用此方法,并且

//迭代inBlk指向的数组

/ / ..导致崩溃

}


私人:

double * inBlk;


}


我想我正在做一些愚蠢的事情,我无法在

时刻看到。怎么可以解决这个问题?也许理想情况下我应该在MyClass中创建

数组,但我现在不想。

感谢

任何帮助,

Stephen。

(apologies for cross posting from the moderated group..i''m sure you
understand)

Hello, I''m passing an array into a Constructor and hoping to use it
as
a pointer and store it as a class member for future use. So far, I''m
just causing crashes, psuedo code below:
double block[8192];
foo = MyClass(block);

////

MyClass {

public:

MyClass (double *blk) {// constructor
inBlk = blk;

}

Process {
// this method will be called periodically and
// iterate over array pointed to by inBlk
// ..which causes a crash
}

private:
double *inBlk;

}

I imagine I''m doing something stupid that I just can''t see at the
moment. How might one solve this? Perhaps ideally I should create
the array within MyClass but I don''t really want to right now.
Thanks
for any help,
Stephen.

推荐答案

stephen b< ke **** @ audiospillage.comwrote in news :1192294175.091803.112560

@ z24g2000prh.googlegroups.com:
stephen b <ke****@audiospillage.comwrote in news:1192294175.091803.112560
@z24g2000prh.googlegroups.com:

我想我正在做一些愚蠢的事情,我只能''请看

时刻。怎么可以解决这个问题?也许理想情况下我应该在MyClass中创建数组,但我现在并不想要。
I imagine I''m doing something stupid that I just can''t see at the
moment. How might one solve this? Perhaps ideally I should create
the array within MyClass but I don''t really want to right now.



代码看起来不错,尽管你没有显示Process的功能。数组是

在堆栈上还是全局? (即,当你搞砸它时它仍然在范围内吗?)

在课堂上宣布它有什么问题?还有什么需要原始的

访问它吗?为什么不使用std :: vector< double>?

The code looks ok, although you don''t show what Process does. Is the array
on the stack or global? (Ie. is it still in scope when you mess with it?)
What''s wrong with declaring it in the class? Does something else need raw
access to it? Why not use a std::vector<double>?





您知道阵列在从函数返回

时不应该访问堆栈(例如通过做'返回

foo;''之类的东西)?


显示真实代码所以我们不必猜测

问候,Ron AF Greve

http://www.InformationSuperHighway.eu


" stephen b" < ke **** @ audiospillage.comwrote in message

news:11 ********************** @ z24g2000prh.googlegr oups.com ...
Hi,

You are aware that the array is on the stack and shouldn''t be accessed when
returning from the function (for instance by doing something like ''return
foo;'' )?

Show the real code so we don''t have to guess
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"stephen b" <ke****@audiospillage.comwrote in message
news:11**********************@z24g2000prh.googlegr oups.com...

(对来自受审核组的交叉发布道歉......我确定你

明白了)


您好,我正在将一个数组传递给一个构造函数并希望使用它

as

a指针并存储为未来使用的集体成员。到目前为止,我是

只是导致崩溃,伪造的代码如下:

double block [8192];

foo = MyClass(block );


// //


MyClass {


public:


MyClass(double * blk){//构造函数

inBlk = blk;


}

流程{

//将定期调用此方法,并且

//迭代inBlk指向的数组

/ / ..导致崩溃

}


私人:

double * inBlk;


}


我想我正在做一些愚蠢的事情,我无法在

时刻看到。怎么可以解决这个问题?也许理想情况下我应该在MyClass中创建

数组,但我现在不想。

感谢

任何帮助,

Stephen。
(apologies for cross posting from the moderated group..i''m sure you
understand)

Hello, I''m passing an array into a Constructor and hoping to use it
as
a pointer and store it as a class member for future use. So far, I''m
just causing crashes, psuedo code below:
double block[8192];
foo = MyClass(block);

////

MyClass {

public:

MyClass (double *blk) {// constructor
inBlk = blk;

}

Process {
// this method will be called periodically and
// iterate over array pointed to by inBlk
// ..which causes a crash
}

private:
double *inBlk;

}

I imagine I''m doing something stupid that I just can''t see at the
moment. How might one solve this? Perhaps ideally I should create
the array within MyClass but I don''t really want to right now.
Thanks
for any help,
Stephen.



显示真实代码所以我们不必猜测


好​​的,这里是完整的代码..好吧我剥离了很多东西,

无关紧要,为了简洁而删除了析构函数。希望我

并没有错过任何其他的东西。 FilterSynth(或Synth)对象是在运行时创建的
(音符),最后持续5-6秒。


我使用的是数组而不是vector< ; double因为我发现

初始化了许多向量< double> s导致短暂的CPU峰值每个

创建一个对象的时间。阵列似乎不那么麻烦,这对于实时DSP的东西很重要。我声明数组在

FilterSynth而不是Filter中,因为其他对象需要访问

它们虽然我总是可以提供一个指针来访问我猜...

感谢您抽出宝贵时间。


class FIlterSynth:public Synth

{

public:


FilterSynth(){

filt =过滤器(filtIn,filtOut);

}


虚拟UInt32 NextBlock(UInt32 inNumFrames){

//此函数由另一个类调用。

Filter.Process(inNumFrames);

}


私人:

double filtIn [8192];

double filtOut [8192];

过滤过滤;


};

类过滤器:public UGen

{

public:


过滤(double * inBlk,double * outBlk){

in = inBlk;

out = outBlk;

}


void流程(UInt32 inNumFrames){


for(UInt32 i = 0; i< inNumFrames; ++ i){

//一些伪过滤器代码..

y0 = *(in)++ + b1 * y1 + b2 * y2;

*(out)++ = a0 * y0 + a1 * y1 + a2 * y2;

}


私人:

如果我这么做的话,我会好奇地加倍* *,* out;

};




Filter.Process(filtIn,FiltOut, inNumFrames);

来电:

void流程(double * in,double * out,UInt32 inNumFrames){// etc};

和omitt

私人:double * in,* out;

然后一切都很好。


Stephen

Show the real code so we don''t have to guess

Okay here is the full code.. well I stripped out a lot of stuff that
doesn''t matter and removed destructors for brevity. Hopefully I
didn''t miss anything else out. FilterSynth (or Synth) objects are
created at runtime (musical notes) and last for upto 5-6 seconds.

I am using arrays instead of vector<doublebecause I found the
initialisation of many vector<double>s caused brief CPU spikes each
time an object is created. Arrays seemed less troublesome which is
important for real-time DSP stuff. I declare the arrays in
FilterSynth instead of Filter as other objects will need access to
them although I could always provide a pointer for access I guess..
Thanks for taking time.

class FIlterSynth : public Synth
{
public:

FilterSynth() {
filt = Filter(filtIn, filtOut);
}

virtual UInt32 NextBlock(UInt32 inNumFrames) {
// this function is called by another class.
Filter.Process(inNumFrames);
}

private:
double filtIn[8192];
double filtOut[8192];
Filter filt;

};
class Filter : public UGen
{
public:

Filter(double *inBlk, double *outBlk) {
in = inBlk;
out = outBlk;
}

void Process(UInt32 inNumFrames) {

for (UInt32 i=0; i<inNumFrames; ++i) {
// some pseudo filter code..
y0 = *(in)++ + b1 * y1 + b2 * y2;
*(out)++ = a0 * y0 + a1 * y1 + a2 * y2;
}

private:
double *in, *out;
};

curiously if I do:
Filter.Process(filtIn, FiltOut, inNumFrames);
to call:
void Process(double *in, double *out, UInt32 inNumFrames) {//etc };
and omitt
private: double *in, *out;
then all is fine.

Stephen


这篇关于设置数组指针并存储为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆