Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Visual C++ and large 2d arrays

Reply
Thread Tools

Visual C++ and large 2d arrays

 
 
ico.bukvic@gmail.com
Guest
Posts: n/a
 
      05-03-2007
Hi all,

I've made a 2d dynamic array as follows (this is a snippet so not all
variables are accounted for):

//numvoices are dynamic (1-1000), entered by user
//MAXCHANNELS is currently defined as 24

float **gvoiceSpat;
float **notechannelGain;
float **notechannelGainSpread;

gvoiceSpat = new float *[numvoices];
notechannelGain = new float *[numvoices];
notechannelGainSpread = new float *[numvoices];

for (i = 0; i < numvoices; i++)
{
gvoiceSpat[i] = new float[MAXCHANNELS];
notechannelGain[i] = new float[MAXCHANNELS];
notechannelGainSpread[i] = new float[MAXCHANNELS];
}

The interesting thing is that this code works flawlessly in gcc but in
Visual C++ (2003 .NET) whenever numvoices exceeds ~120, the program
crashes, sometimes reporting unknown exception. The problem is that
this is a code for an external module for another application and uses
additional third-party libs so it is difficult to point fingers at the
culprit. Yet, the fact remains that this crash occurs only on Windows
using Visual C++, while it works flawlessly on OSX (gcc) and Linux
(gcc) using same libs.

Any ideas as to why would this be the case?

For what it's worth, I also tried substituting these with vectors with
no difference whatsoever.

Any help is most appreciated!

Sincerely,

Ico

 
Reply With Quote
 
 
 
 
Greg Herlihy
Guest
Posts: n/a
 
      05-03-2007



On 5/2/07 6:44 PM, in article
. com,
"" <> wrote:

> I've made a 2d dynamic array as follows (this is a snippet so not all
> variables are accounted for):
>
> //numvoices are dynamic (1-1000), entered by user
> //MAXCHANNELS is currently defined as 24
>
> float **gvoiceSpat;
> float **notechannelGain;
> float **notechannelGainSpread;
>
> gvoiceSpat = new float *[numvoices];
> notechannelGain = new float *[numvoices];
> notechannelGainSpread = new float *[numvoices];
>
> for (i = 0; i < numvoices; i++)
> {
> gvoiceSpat[i] = new float[MAXCHANNELS];
> notechannelGain[i] = new float[MAXCHANNELS];
> notechannelGainSpread[i] = new float[MAXCHANNELS];
> }
>
> The interesting thing is that this code works flawlessly in gcc but in
> Visual C++ (2003 .NET) whenever numvoices exceeds ~120, the program
> crashes, sometimes reporting unknown exception. The problem is that
> this is a code for an external module for another application and uses
> additional third-party libs so it is difficult to point fingers at the
> culprit. Yet, the fact remains that this crash occurs only on Windows
> using Visual C++, while it works flawlessly on OSX (gcc) and Linux
> (gcc) using same libs.
>
> Any ideas as to why would this be the case?


Could it be that on Windows numvoices is a char - and the program is
crashing when numvoices' value exceeds 128 and overflows?

Greg

 
Reply With Quote
 
 
 
 
ico.bukvic@gmail.com
Guest
Posts: n/a
 
      05-03-2007

> > Any ideas as to why would this be the case?

>
> Could it be that on Windows numvoices is a char - and the program is
> crashing when numvoices' value exceeds 128 and overflows?
>
> Greg


No idea. How can this be tested and/or alleviated?

Ico


 
Reply With Quote
 
ico.bukvic@gmail.com
Guest
Posts: n/a
 
      05-03-2007
Actually, upon closer inspection, the ceiling appears to shift a lot
and optimizing code does increase it (without code optimization it
hovers around 90, and with full optimization it can get up to 120 or
so).

Ico

 
Reply With Quote
 
ico.bukvic@gmail.com
Guest
Posts: n/a
 
      05-03-2007
Could it be something like the problem described at the following link
even though I am using "new" to allocate memory?

http://www.daniweb.com/techtalkforums/thread46297.html

Ico

 
Reply With Quote
 
GeekBoy
Guest
Posts: n/a
 
      05-03-2007
Maybe because you have here a single dimensional array and not a 2D one as
you claim?

Define 2D array:

"A 2D array is an array that has both rows and columns. You must use 2 sets
of square brackets when declaring a 2D array and when using it."

e.g.:
int arr[3][3];
arr[0][0] = 5;

<> wrote in message
news: oups.com...
> Hi all,
>
> I've made a 2d dynamic array as follows (this is a snippet so not all
> variables are accounted for):
>
> //numvoices are dynamic (1-1000), entered by user
> //MAXCHANNELS is currently defined as 24
>
> float **gvoiceSpat;
> float **notechannelGain;
> float **notechannelGainSpread;
>
> gvoiceSpat = new float *[numvoices];
> notechannelGain = new float *[numvoices];
> notechannelGainSpread = new float *[numvoices];
>
> for (i = 0; i < numvoices; i++)
> {
> gvoiceSpat[i] = new float[MAXCHANNELS];
> notechannelGain[i] = new float[MAXCHANNELS];
> notechannelGainSpread[i] = new float[MAXCHANNELS];
> }
>
> The interesting thing is that this code works flawlessly in gcc but in
> Visual C++ (2003 .NET) whenever numvoices exceeds ~120, the program
> crashes, sometimes reporting unknown exception. The problem is that
> this is a code for an external module for another application and uses
> additional third-party libs so it is difficult to point fingers at the
> culprit. Yet, the fact remains that this crash occurs only on Windows
> using Visual C++, while it works flawlessly on OSX (gcc) and Linux
> (gcc) using same libs.
>
> Any ideas as to why would this be the case?
>
> For what it's worth, I also tried substituting these with vectors with
> no difference whatsoever.
>
> Any help is most appreciated!
>
> Sincerely,
>
> Ico
>



 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-03-2007
wrote:
> Could it be something like the problem described at the following link
> even though I am using "new" to allocate memory?
>
> http://www.daniweb.com/techtalkforums/thread46297.html


Your issue is somthing else because as you already noted, you're using
dynamic memory allocation.
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-03-2007
wrote:
> Hi all,
>
> I've made a 2d dynamic array as follows (this is a snippet so not all
> variables are accounted for):
>
> //numvoices are dynamic (1-1000), entered by user
> //MAXCHANNELS is currently defined as 24
>
> float **gvoiceSpat;
> float **notechannelGain;
> float **notechannelGainSpread;
>
> gvoiceSpat = new float *[numvoices];
> notechannelGain = new float *[numvoices];
> notechannelGainSpread = new float *[numvoices];
>
> for (i = 0; i < numvoices; i++)
> {
> gvoiceSpat[i] = new float[MAXCHANNELS];
> notechannelGain[i] = new float[MAXCHANNELS];
> notechannelGainSpread[i] = new float[MAXCHANNELS];
> }
>
> The interesting thing is that this code works flawlessly in gcc but in
> Visual C++ (2003 .NET) whenever numvoices exceeds ~120, the program
> crashes, sometimes reporting unknown exception. The problem is that
> this is a code for an external module for another application and uses
> additional third-party libs so it is difficult to point fingers at the
> culprit. Yet, the fact remains that this crash occurs only on Windows
> using Visual C++, while it works flawlessly on OSX (gcc) and Linux
> (gcc) using same libs.
>
> Any ideas as to why would this be the case?
>
> For what it's worth, I also tried substituting these with vectors with
> no difference whatsoever.
>
> Any help is most appreciated!


You're probably corrupting memory somewhere. If you are running on a
linux box, try to use valgrind (remember to set the appropriate
environment variable.

The environment variable is GLIBCXX_FORCE_NEW (or GLIBCPP_FORCE_NEW
depending on version of GCC).

For gcc 3.3.2 the variable is GLIBCPP_FORCE_NEW
For gcc 3.4.0 and above it is GLIBCXX_FORCE_NEW

If you're not sure
$ strings /usr/lib/libstdc++.so.6 | grep FORCE
GLIBCXX_FORCE_NEW


If you're running only on windows, you have to buy a third party tool.

Just a note, if you litter your code with news and deletes, you're bound
to have a problem like this show up. Use vectors or a 2D matrix class
and avoid problems like this.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-03-2007
On May 3, 7:10 am, ico.buk...@gmail.com wrote:
> Could it be something like the problem described at the following link
> even though I am using "new" to allocate memory?


> http://www.daniweb.com/techtalkforums/thread46297.html


No, or at least not directly.

I'm not sure what your version of VC++ does if new fails because
of insufficient memory. The standard says you get an exception,
and the latest versions of VC++ are conform, but VC++ 6.0 still
returned a null pointer (when it detected the situation). For
starters, you might try 1) wrapping your code in a try block,
and catching std::bad_alloc, and 2) testing the result of each
new for NULL. Realistically, however: at around 100, you're
allocating under 50 KB; I can't imagine that failing on any
modern machine.

Greg Hilary's suggestion about char overflowing is interesting
as well. Logically, it should mean a threashold of exactly 128,
however. Still, if you compile using unsigned char (option /J,
I think), you should be able to eliminate it.

Other than that, I don't see anything wrong with it. I just ran
it on my PC (a very small machine, with only 256 MB), and had no
problems with numvoices at 100000; at 1000000, the program
became very slow, as the machine started paging, and I finally
got a bad_alloc exception. (This is with VC++ 2005.)

All I can suggest is that you try to isolate a 15-20 line bit of
code which can be compiled on its own, and manifests the error,
and post that.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      05-03-2007
On Thu, 03 May 2007 03:02:12 -0500, GeekBoy wrote:
> <> wrote in message
> news: oups.com...
>> Hi all,
>>
>> I've made a 2d dynamic array as follows (this is a snippet so not all
>> variables are accounted for):
>>
>> //numvoices are dynamic (1-1000), entered by user //MAXCHANNELS is
>> currently defined as 24
>>
>> float **gvoiceSpat;
>> float **notechannelGain;
>> float **notechannelGainSpread;
>>
>> gvoiceSpat = new float *[numvoices];
>> notechannelGain = new float *[numvoices]; notechannelGainSpread =
>> new float *[numvoices];
>>
>> for (i = 0; i < numvoices; i++)
>> {
>> gvoiceSpat[i] = new float[MAXCHANNELS];
>> notechannelGain[i] = new float[MAXCHANNELS];
>> notechannelGainSpread[i] = new float[MAXCHANNELS];
>> }

>
> Maybe because you have here a single dimensional array and not a 2D one
> as you claim?
>
> Define 2D array:
>
> "A 2D array is an array that has both rows and columns. You must use 2
> sets of square brackets when declaring a 2D array and when using it."
>
> e.g.:
> int arr[3][3];
> arr[0][0] = 5;


*Please* don't top-post (rearranged)

The OP described it as a *dynamic* array and presents a pretty standard
implementation. Note the two levels of "new" in the code. "Array"
elements can then be referenced as:

gvoiceSpat[i][j]

etc. just like a "proper" 2D array.

>> The interesting thing is that this code works flawlessly in gcc but in
>> Visual C++ (2003 .NET) whenever numvoices exceeds ~120, the program
>> crashes, sometimes reporting unknown exception. The problem is that
>> this is a code for an external module for another application and uses
>> additional third-party libs so it is difficult to point fingers at the
>> culprit. Yet, the fact remains that this crash occurs only on Windows
>> using Visual C++, while it works flawlessly on OSX (gcc) and Linux
>> (gcc) using same libs.
>>
>> Any ideas as to why would this be the case?


Try the usual; a *minimal* program which demonstrates the problem (my
suspicion would be that the problem exists elsewhere in the code).

--
Lionel B
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Difference between accessing arrays and associative arrays using a int index shashi Perl Misc 17 04-13-2006 03:48 PM
Problem with va_ macros and arrays of arrays rir3760 C Programming 1 04-04-2005 06:29 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57