std::bad_array_new_length

Write your question here.
what() is wrong whith this code ?
from a famous book I'm reading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  Put the code you need help with here.
class vect
  {
   int nelem; // nombre d'éléments
   int *adr;  // pointeur sur ces éléments
   public:
     vect( int n ) // constructeur
       {
        adr = new int [ nelem - n ];
        for( int i = 0; i < nelem; i++ )
           adr[ i ] = 0;
        std::cout << "++obj taille " << nelem << " en " << this
                   << " - v. dyn en " << adr << "\n";
       } // fin du constructeur
  }; // fin de la classe vect
int main()
  {
   vect a( 1 );
std::cout << "*** fin main *** " << std::endl;
   return 0;
}

error message folows :
terminate called after throwing an instance of 'std::bad_array_new_length'
what(): std::bad_array_new_length
abandon (core dumped)
Actually, original code declares vect a );
and the real fun begins : this noob is stumped and the machine freezes !
Michel Chassey
Any book that contains code like this should be immediately thrown in the trash. The author clearly didn't even run it.

vect::nelem is uninitialized at line 9, so it probably contains a fairly large garbage value that can't be allocated.
nelem has no value. line 9 won't work.

try something like...
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
#include <iostream>

class vect
  {
   int nelem; // nombre d'éléments
   int *adr;  // pointeur sur ces éléments
   public:
     vect( int n, int m ) // constructeur
       {
           nelem = m;
        adr = new int [ nelem - n ];
        for( int i = 0; i < nelem; i++ )
           adr[ i ] = 0;
        std::cout << "++obj taille " << nelem << " en " << this
                   << " - v. dyn en " << adr << "\n";
       } // fin du constructeur
       ~vect() {
           delete[] adr;
       }
  }; // fin de la classe vect
int main()
  {
   vect a( 1 , 3 );
std::cout << "*** fin main *** " << std::endl;
   return 0;
}
Hello mycuser,

As "helios" and "Manga" have said "nelem" is never given a value. On my computer an "int' variable that is defined and not given a value usually has the value of "-858993460" known as a garbage value. Which is whatever is at that memory location when the variable is given space at compile time.

Your error message is most likely based on the fact that you are trying to use the "new" to create an array with a negative size. This must be a positive number.

"Manga" has the simplest way of fixing your problem and I would just use it. There is another option, but it is more involved.

Another point you might find a problem with is adr = new int [ nelem - n ];. Subtracting "n" could make your array smaller than you need or even give it a negative value which would cause your error. Yo may find it better to use [nelem + 1] to make it work.

Suggestion/hint a few blank lines would make your code easier to read as in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class vect
{
	int nelem; // nombre d'éléments
	int *adr;  // pointeur sur ces éléments
public:
	vect(int n) // constructeur
	{
		adr = new int[nelem - n];

		for (int i = 0; i < nelem; i++)
			adr[i] = 0;

		std::cout << "++obj taille " << nelem << " en " << this
			<< " - v. dyn en " << adr << "\n";
	} // fin du constructeur
}; // fin de la classe vect
int main()
{
	vect a(1);

	std::cout << "*** fin main *** " << std::endl;

	return 0;
}

Also notice the {}s of the blocks. This is how my IDE deals with them, but as you can see it is cleaner. It also make finding the opening and closing braces easier when they line up.

Hope that helps,

Andy
[Hello all,
I have reworked Andy's line # 12

from : for (int i = 0; i < nelem; i++ )

to : for ( int i = 0; i < nelem - n; i++ )

and I have initialized nelem right up to 100,000,000 but my machine is slow,
while declaring : vect a( 5 ), b( 3 ), c( 4 );

nelem at 1,000,000,000 brings on the big freeze :)

Now I'm going to the lounge where there was mention of some good 'must have' books

As for the { } , simple emacs will let you scribble all kinds of weird stuff.
Topic archived. No new replies allowed.