|
Hello everyone,
I am wondering except when there is no memory on heap, are there any other situations when we will get bad_alloc exceptions? For example, invalid input of the size (e.g. very huge number or zero or negative number) will cause exception?
thanks in advance,
George
|
|
|
|
|
MSDN http://msdn2.microsoft.com/en-us/library/6512dwes(VS.71).aspx[^] is a bit laconic about, but seems to go on the way you depicted. BTW, Why don't you make a little test and tell us your findings?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
It seems bad_alloc only occurs when there is no memory or not enough memory (as showed in the sample). And we can not input invalid value, like -1, compiler will fail. Do you agree with me?
BTW: what situations do you find you met with bad_alloc?
regards,
George
|
|
|
|
|
George_George wrote: It seems bad_alloc only occurs when there is no memory or not enough memory (as showed in the sample).
Maybe, see the following lines.
George_George wrote: And we can not input invalid value, like -1, compiler will fail. Do you agree with me?
Well, a negative integer is casted to a size_t one, that is an unsigned one, since -1 turns out to a very big unsigned value (4294967295), the compiler is able to detect the error. But a silly trick like the following
int *p;
int a=1;
int b=rand()> RAND_MAX / 2 ? 1 : 2;
try {
p= new int[-a*b];
}
catch(...)
{
printf("Exception, George!\n");
}
makes the compiler happy but an the exception is thrown at run-time.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks CPallini,
Cool!
regards,
George
|
|
|
|
|
Did you check when you want to allocate memory?
|
|
|
|
|
Thanks Hamid,
What do you mean check? Check what?
regards,
George
|
|
|
|
|
When you want to allocate dynamic memory did you check for example use of try/catch?
|
|
|
|
|
Hi Hamid,
Which example do you mean? URL please?
regards,
George
|
|
|
|
|
No I said for example use of try/catch not Example.
|
|
|
|
|
Hi Hamid,
I am confused, which sample do you mean? If you mean anything of this forum, could you provide a link please?
regards,
George
|
|
|
|
|
No I said (for example) not a example.dont worry
|
|
|
|
|
dear all
how to set path in setting of option? anyone has experience or some detail to introduce this? thanks a lot.
Li Zhiyuan
5/1/2008
|
|
|
|
|
What are you talking about ?
|
|
|
|
|
Hey Tox, was you on vacation, wasn't you?
Happy New Year and best wishes.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
yup, and to be fully exact, I'm still on vacation.
I was off from Codeproject because I went to Lithuania for the new year
|
|
|
|
|
Do you mean additional search paths for include files?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
yes, you are right, when we go to tool-> option, we can see executable file..but we also can choose include file, how to set the path there? i encountered a problem, after compilation,output showed me:
fatal error C1083: Cannot open include file: 'Centroids.h': No such file or directory
so i dun know how to set this path.
thanks
Li Zhiyuan
5/10/2006
|
|
|
|
|
Well, IMHO, the best behaviour will be to put your Centroids.h header file inside the project source folder (where is, for instance Centroids.cpp ) and then use:
#include "Centroids.h"
into Centroids.cpp .
However, if you really need to put Centroids.h in a special folder, then, adding such a folder to the VC++ include directories isn't difficult:
(1) Choose Tools->Options menu item
(2) Select Project and Solutions->VC++ Directories node.
(3) Select Include Files item in the Show Directories for list.
(4) Choose the New line (folder icon) button or press CTRL+INS key combination.
(5) Add the path of the folder wherein Centroids.h is (you can browse for).
(6) Enjoy compilation (remember the #include "Centroids.h" in the source code)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
yes, actually i did like this already, centroids just is a class in my header file... i installed vc++ 2005 in D driver, but my project in C driver..so i added a direcotry included my project in C driver...but the problem is still like this
Li Zhiyuan
5/10/2006
|
|
|
|
|
li zhiyuan wrote: i installed vc++ 2005 in D driver, but my project in C driver.
That shouldn't be a problem.
Check well all that you've made.
You can also try to start from zero agin.
Good luck.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
this is my all source code, i combined together, it can run smoothly, but i seperated, the problem is like above.
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <cstdlib>
using std::rand;
using std::srand;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
#include <ctime>
using std::time;
class Centroids
{
public:
void printArray(double a[3][4] )
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
cout << setw(3) << setprecision(6) << setiosflags(ios::fixed|ios::showpoint) << a[i][j] << " ";
cout << endl;
}
}
private:
double a[3][4];
};
int main()
{
Centroids matrix;
int number = 1;
for (int number = 1; number <=10; number++)
{
double a[3][4] = {{(double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX,( double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX}};
cout << setw(4) << number << endl;
matrix.printArray(a);
cout << endl;
}
number = number + 1;
return 0;
}
the following is what i seperated code above, can you help me to check? what is problem?
header file:
include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
class Centroids
{
public:
void printArray(double a[3][4] )
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
cout << setw(3) << setprecision(6) << setiosflags(ios::fixed|ios::showpoint) << a[i][j] << " ";
cout << endl;
}
}
private:
double a[3][4];
};
cpp code:
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <cstdlib>
using std::rand;
using std::srand;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
#include <ctime>
using std::time;
#include "Centroids.h"
int main()
{
Centroids matrix;
int number = 1;
for (int number = 1; number <=10; number++)
{
double a[3][4] = {{(double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX,( double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX}};
cout << setw(4) << number << endl;
matrix.printArray(a);
cout << endl;
}
number = number + 1;
return 0;
}
thanks a lot
Li Zhiyuan
5/10/2006
|
|
|
|
|
yes, actually i did like this already, centroids just is a class in my header file... i installed vc++ 2005 in D driver, but my project in C driver..so i added a direcotry included my project in C driver...but the problem is still like this.
the following is combined code:
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <cstdlib>
using std::rand;
using std::srand;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
#include <ctime>
using std::time;
class Centroids
{
public:
void printArray(double a[3][4] )
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
cout << setw(3) << setprecision(6) << setiosflags(ios::fixed|ios::showpoint) << a[i][j] << " ";
cout << endl;
}
}
private:
double a[3][4];
};
int main()
{
Centroids matrix;
int number = 1;
for (int number = 1; number <=10; number++)
{
double a[3][4] = {{(double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX,( double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX}};
cout << setw(4) << number << endl;
matrix.printArray(a);
cout << endl;
}
number = number + 1;
return 0;
}
the following is seperate code:
header file:
include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
class Centroids
{
public:
void printArray(double a[3][4] )
{
for (int i = 0; i <3; 3; i++)
{
for (int j = 0; j < 4; j++)
cout << setw(3)<< setprecision(6) << setiosflags(ios::fixed|ios::showpoint)<< a[i][j] <<; " ";
cout <<; endl;
}
}
private:
double a[3][4];
};
cpp code:
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <cstdlib>
using std::rand;
using std::srand;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::ios;
using std::fixed;
using std::showpoint;
using std::setiosflags;
#include <ctime>
using std::time;
#include "Centroids.h"
int main()
{
Centroids matrix;
int number = 1;
for (int number = 1; number <=10; number++)
{
double a[3][4] = {{(double)rand()/RAND_MAX, (double)rand()/RAND_MAX, (double)rand()/RAND_MAX,( double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX},{(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX,(double)rand()/RAND_MAX}};
cout << setw(4)<< number << endl;
matrix.printArray(a);
cout << endl;
}
number = number + 1;
return 0;
}
Li Zhiyuan
5/10/2006
|
|
|
|
|
What you have here is a big ol' mess. Why have you duplicated the code in both the .h and .cpp files? Put the class declaration in the .h file like:
class Centroids
{
public:
void printArray( double a[3][4] );
}; Then put its definition in the .cpp file like:
#include "centroids.h"
void Centroids::printArray( double a[3][4] )
{
using namespace std;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
cout << setw(3) << setprecision(6) << setiosflags(ios::fixed | ios::showpoint) << a[i][j] << " ";
cout << endl;
}
} li zhiyuan wrote: int number = 1;
for (int number = 1;
Why do you have number declared twice?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: Why do you have number defined twice?
He has the Visual Studio Dumb Edition C++ Compiler.
Thank you for pointing out code mess. I overlooked his code, focusing my attempts to help him to properly set the environment.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|