473,804 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pasing an array pointer and storing as a class member

(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.

Oct 13 '07 #1
13 2150
stephen b <ke****@audiosp illage.comwrote in news:1192294175 .091803.112560
@z24g2000prh.go oglegroups.com:
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.
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<dou ble>?
Oct 13 '07 #2
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****@audiosp illage.comwrote in message
news:11******** **************@ z24g2000prh.goo glegroups.com.. .
(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.

Oct 13 '07 #3
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<doublebe cause 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(UInt3 2 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

Oct 14 '07 #4
On Oct 14, 8:23 am, stephen b <ker...@audiosp illage.comwrote :
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<doublebe cause 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(UInt3 2 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

there are 2 thing that i feel are wrong in ur code:

First:
filt = Filter(filtIn, filtOut);

if u want to have a class member variable (not pointer) pls use member
wise initialization
Second i dont know how are u using

Filter.Process( inNumFrames);

class-name.function-name();

could u pls change this to

filt.Process(in NumFrames);

Oct 14 '07 #5
there are 2 thing that i feel are wrong in ur code:

First:
filt = Filter(filtIn, filtOut);

if u want to have a class member variable (not pointer) pls use member
wise initialization
i'm not sure what you mean by member wise initialization in this
context?
Second i dont know how are u using

Filter.Process( inNumFrames);

class-name.function-name();

could u pls change this to

filt.Process(in NumFrames);
okay well spotted that was a typo.. really I am doing
filt.Process(in NumFrames);

Stephen.
Oct 14 '07 #6
On 2007-10-14 20:34, stephen b wrote:
>there are 2 thing that i feel are wrong in ur code:

First:
filt = Filter(filtIn, filtOut);

if u want to have a class member variable (not pointer) pls use member
wise initialization

i'm not sure what you mean by member wise initialization in this
context?
Instead of having a constructor like this:

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

you should use an initialisation-list to initialise filt, like this:

FilterSynth() : filt(filtIn, filtOut)
}
}

--
Erik Wikström
Oct 14 '07 #7

stephen b wrote in message...
>
I am using arrays instead of vector<doublebe cause I found the
initialisation of many vector<double>s caused brief CPU spikes each
time an object is created.
I'm curious. How did you initialize the vectors?
Arrays seemed less troublesome which is
important for real-time DSP stuff.
I think it's the opposite. :-}
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);
}
If this is your class constructor, the names should match: 'FIlterSynth' vs.
'FilterSynth'.
>
virtual UInt32 NextBlock(UInt3 2 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;
}
This is why I'm asking about your vector inits, you should use class
initialization lists:

Filter( double *inBlk, double *outBlk )
: in( inBlk ), out( outBlk ){} // note the colon
>
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
Could it be because you are dereferenceing un-initialized data (the arrays)?
Let's test something. In your FilterSynth() constructor:

FilterSynth(){
for( int i(0); i < 10; ++i ){
std::cout<< filtIn[ i ] <<'\n';
} // for(i)
} // Ctor

What's your output from that? (I got garbage (UB)).
Also, please indent your code for posting (using spaces, not tabs).

--
Bob R
POVrookie
Oct 14 '07 #8
On 14 Oct, 19:56, "BobR" <removeBadB...@ worldnet.att.ne twrote:
stephen b wrote in message...
I am using arrays instead of vector<doublebe cause I found the
initialisation of many vector<double>s caused brief CPU spikes each
time an object is created.

I'm curious. How did you initialize the vectors?
Arrays seemed less troublesome which is
important for real-time DSP stuff.

I think it's the opposite. :-}
really? right, I think I initialised my vectors like this:
vector<doubleve c (8192, 0.); so I guess maybe filling the array
with zeros was causing some overhead. I also found arrays to be
slightly more efficient in use although it's probably negligible even
though I'll be using 20 or so per synth.

Could it be because you are dereferenceing un-initialized data (the arrays)?
Let's test something. In your FilterSynth() constructor:

FilterSynth(){
for( int i(0); i < 10; ++i ){
std::cout<< filtIn[ i ] <<'\n';
} // for(i)
} // Ctor

What's your output from that? (I got garbage (UB)).
well with the filter example I fill the array with zeros first but
otherwise just garbage.

Stephen.

Oct 16 '07 #9
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;
forgot to clarify: the reason my code is crashing is because I'm
incrementing the pointers into the arrays and calling Process
potentially hundreds of times a second. so I need to reset the
pointers to the start of the array.. if such a thing is possible? I'm
quite surprised I didn't notice that. or I could just continue
passing the pointers via the Process function.

Stephen

Oct 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1990
by: Chris Bruyere | last post by:
Hi All, I just finished reading the FAQ page on fucntion points, for this ng, and I just wanted to clarify something. class MessageCenter { .... void addMessage(int i, Message* m); //implemented in .cpp file }
3
5850
by: Kaz Kylheku | last post by:
Given some class C with array T x, is it possible to get a pointer-to-data-member to one of the elements? &C::x gives us a pointer-to-member-array: T (C::*). But I just want to get a T C::* pointing to a selected array element, so that I can later use an instance c of that class to pick out that array element: c->*ptr. This syntax, for instance, doesn't work: &C::x. My compiler thinks
22
2465
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
8
2403
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct Employee { char employeeid; /* id of employee*/
14
3139
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
10
2704
by: Roman Mashak | last post by:
Hello, All! I've met the code containing this kind of structure: typedef struct cmd { unsigned int Cmd; unsigned int Code; unsigned int Data; } CMD;
20
4657
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
5
3656
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
2
11945
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.