Easy C++ Questions

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

camsr wrote:The same way as any other C/C++ constant. Make sure the compilation settings don't remove it either.
I think that's what he's asking; How is it done in C/C++. If your code accesses the memory I'd be surprised if the compiler just deallocates it. I've never seen that and would be rather pissed off if my compiler decided to break my code by 'optimising' away memory that I'd need to access. What settings would allow that?

Post

It depends if they are "used" or not. In GCC, IIRC it's Dead Code Removal. If it's referenced by an ambiguous pointer, that might make it seem unused.

If he is writing the values to memory using new, they should be there.

Post

I'm aware of dead code removal, but I've never ran into a situation of pointer ambiguity causing this problem. Are you talking about user error, or the compiler not being smart enough to resolve the pointer at compile time? Either way this is going a little OT :)

EXC_BAD_ACCESS could also result from accessing outside of array bounds (0 to n-1).

Post

I would definitely advise to start by the basics, and obviously you are not there yet. Not try to get too far ahead without mastering the fundamentals of the language.

Post

Here's what I've got:

In header:

Code: Select all

private:
float **wave;
	float **coeffs;
In Constructor:

Code: Select all

coeffs = new float *[4];
	for (int i=0; i<4; i++)
		coeffs[i] = new float [128];
	makeCoeffs();
	
	wave = new float *[NO_OF_WAVES];
	for (int i=0; i<NO_OF_WAVES; i++)
		wave[i] = new float [1024];
	makeWaves();
In process(...):

Code: Select all

s = (wave[NO_OF_NODES][n0]) * (coeffs[0][index]) + (wave[NO_OF_NODES][n1]) * (coeffs[1][index]) +
					(wave[NO_OF_NODES][n2]) * (coeffs[2][index]) + (wave[NO_OF_NODES][n3]) * (coeffs[3][index]);
In Crash Report:

Code: Select all

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xffffffff10e3fffc
n0 through n4 are all ints calculated cyclically from 0-1023 and are defined with int index and float s just before the loop, so are valid(ish). :wink:

That was part of an inline call to an interpolation method, but I broke it out for troubleshooting. If I bypass it and just calculate s = (float)sin((float)j*PI*2.0f/1023.0f); I get wave sign--er, a sine wave, but it does make a noise.
Last edited by syntonica on Wed Nov 23, 2016 11:55 am, edited 1 time in total.
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

I've checked everything I can think of, including compiler settings. Grah!

:lol:
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

use std::vector
Olivier Tristan
Developer - UVI Team
http://www.uvi.net

Post

otristan wrote:use std::vector
Thanks, but the arrays I am using are fixed in size, so I'm not sure what I could gain by this.
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

What is no_of_nodes? What is index set to? How are/are they incremented?

Use a debugger and make sure nothing goes out of bounds. Should be simple enough to verify.

Post

matt42 wrote:
camsr wrote:The same way as any other C/C++ constant. Make sure the compilation settings don't remove it either.
I think that's what he's asking; How is it done in C/C++. If your code accesses the memory I'd be surprised if the compiler just deallocates it. I've never seen that and would be rather pissed off if my compiler decided to break my code by 'optimising' away memory that I'd need to access. What settings would allow that?
I'm using Xcode, so it's probably doing quite a number of "nice" things for me that I'm unaware of, making debugging a much harder task than it should be. Divide by zero? Don't worry... we'll make that go away for you... :dog:
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

matt42 wrote:What is no_of_nodes? What is index set to? How are/are they incremented?

Use a debugger and make sure nothing goes out of bounds. Should be simple enough to verify.
NO_OF_NODES is a #define.
Oh crap... it's my indices. I just replaced them all with hard values and no crash...
...sigh...

Back to the drawing board... Can I go back to Java now? :ud:

Sorry for my frustration spilling out onto this board. But having to learn a new IDE (Xcode) AND a new language extension (c++) at the same time is driving me spare. It makes me want to kick a puppy at times.
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

Aside of the error get use of typedefs, e.g.:

Code: Select all

wave = new float *[NO_OF_WAVES];
   for (int i=0; i<NO_OF_WAVES; i++)
      wave[i] = new float [1024];
->

Code: Select all

typedef float Wave[1024]; 
Wave* wave = new Wave[NO_OF_WAVES];
etc.

Also counting that NO_OF_NODES is a constant I suspect the NO_OF_WAVES is a (compile time) constant too, if so you could use the array w/o any new/delete at all (same goes for your coeffs array).

Post

Max M. wrote:

Code: Select all

typedef float Wave[1024]; 
Wave* wave = new Wave[NO_OF_WAVES];
OMG! Thank you so much! I went through every permutation I could find of how to define a new 2D array. That one actually makes sense and works! I had completely forgotten about "typedef".
I started on Logic 5 with a PowerBook G4 550Mhz. I now have a MacBook Air M1 and it's ~165x faster! So, why is my music not proportionally better? :(

Post

You could as well interleave your data in the same array
way easier and with a better locality.
Olivier Tristan
Developer - UVI Team
http://www.uvi.net

Post

syntonica wrote:I went through every permutation I could find of how to define a new 2D array.
It can be written w/o a typedef too (if I recall correctly though):

Code: Select all

float (*wave)[1024] = new float[NO_OF_WAVES][1024];
But it's pretty much head-scratching to read even for C++ geeks.

Post Reply

Return to “DSP and Plugin Development”