本文整理匯總了C++中std::bad_alloc方法的典型用法代碼示例。如果您正苦於以下問題:C++ std::bad_alloc方法的具體用法?C++ std::bad_alloc怎麽用?C++ std::bad_alloc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類std
的用法示例。
在下文中一共展示了std::bad_alloc方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: bad_alloc
ArrayRCP<zscalar_t> makeWeights(
const RCP<const Teuchos::Comm<int> > & comm,
zlno_t len, weightTypes how, zscalar_t scale, int rank)
{
zscalar_t *wgts = new zscalar_t [len];
if (!wgts)
throw bad_alloc();
ArrayRCP<zscalar_t> weights(wgts, 0, len, true);
if (how == upDown){
zscalar_t val = scale + rank%2;
for (zlno_t i=0; i < len; i++)
wgts[i] = val;
}
else if (how == roundRobin){
for (int i=0; i < 10; i++){
zscalar_t val = (i + 10)*scale;
for (zlno_t j=i; j < len; j += 10)
weights[j] = val;
}
}
else if (how == increasing){
zscalar_t val = scale + rank;
for (zlno_t i=0; i < len; i++)
wgts[i] = val;
}
return weights;
}
示例2: bad_alloc
ParserInterfaceScannerMembers::ParserInterfaceScannerMembers(
const char* const query
) :
scanner_(),
bufferState_(nullptr)
{
if (0 != sql_lex_init(&scanner_))
{
throw bad_alloc();
}
bufferState_ = sql__scan_string(query, scanner_);
if (nullptr == bufferState_)
{
sql_lex_destroy(scanner_);
throw bad_alloc();
}
}
示例3: ResizeBuffer
/**
* Resizes the FIFO's buffer so that it is at least newSize bytes long.
*
* @param newSize The minimum new size of the FIFO buffer.
*/
void FIFO::ResizeBuffer(size_t newSize)
{
if (m_AllocSize >= newSize)
return;
newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize;
char *newBuffer = static_cast<char *>(realloc(m_Buffer, newSize));
if (newBuffer == NULL)
throw_exception(bad_alloc());
m_Buffer = newBuffer;
m_AllocSize = newSize;
}
示例4: add
/**
* If you want to upload more than one file, you can pass the form name and a
* vector of filenames.
*/
void curl_form::add(const curl_pair<CURLformoption,string> &form_name, const vector<string> &files) {
const size_t size = files.size();
struct curl_forms *new_files;
this->is_null(new_files = (struct curl_forms *)calloc(size,sizeof(struct curl_forms)));
if (new_files == nullptr) {
throw bad_alloc();
}
for (size_t i = 0; i < size; ++i) {
new_files[i].option = CURLFORM_FILE;
new_files[i].value = files[i].c_str();
}
if (curl_formadd(&this->form_post,&this->last_ptr,
form_name.first(),form_name.second(),
CURLFORM_ARRAY,new_files,
CURLFORM_END) != 0) {
delete []new_files;
throw curl_exception("Error while adding the form",__FUNCTION__);
}
delete []new_files;
}
示例5: new
_GLIBCXX_WEAK_DEFINITION void *
operator new (std::size_t sz) throw (std::bad_alloc)
{
void *p;
/* malloc (0) is unpredictable; avoid it. */
if (sz == 0)
sz = 1;
p = (void *) malloc (sz);
while (p == 0)
{
new_handler handler = __new_handler;
if (! handler)
#ifdef __EXCEPTIONS
throw bad_alloc();
#else
std::abort();
#endif
handler ();
p = (void *) malloc (sz);
}
return p;
}
示例6: makeMeshCoordinates
/* Create a mesh of approximately the desired size.
*
* We want 3 dimensions close to equal in length.
*/
tMVector_t* makeMeshCoordinates(
const RCP<const Teuchos::Comm<int> > & comm,
gno_t numGlobalCoords)
{
int rank = comm->getRank();
int nprocs = comm->getSize();
double k = log(numGlobalCoords) / 3;
double xdimf = exp(k) + 0.5;
gno_t xdim = static_cast<gno_t>(floor(xdimf));
gno_t ydim = xdim;
gno_t zdim = numGlobalCoords / (xdim*ydim);
gno_t num=xdim*ydim*zdim;
gno_t diff = numGlobalCoords - num;
gno_t newdiff = 0;
while (diff > 0){
if (zdim > xdim && zdim > ydim){
zdim++;
newdiff = diff - (xdim*ydim);
if (newdiff < 0)
if (diff < -newdiff)
zdim--;
}
else if (ydim > xdim && ydim > zdim){
ydim++;
newdiff = diff - (xdim*zdim);
if (newdiff < 0)
if (diff < -newdiff)
ydim--;
}
else{
xdim++;
newdiff = diff - (ydim*zdim);
if (newdiff < 0)
if (diff < -newdiff)
xdim--;
}
diff = newdiff;
}
num=xdim*ydim*zdim;
diff = numGlobalCoords - num;
if (diff < 0)
diff /= -numGlobalCoords;
else
diff /= numGlobalCoords;
if (rank == 0){
if (diff > .01)
cout << "Warning: Difference " << diff*100 << " percent" << endl;
cout << "Mesh size: " << xdim << "x" << ydim << "x" <<
zdim << ", " << num << " vertices." << endl;
}
// Divide coordinates.
gno_t numLocalCoords = num / nprocs;
gno_t leftOver = num % nprocs;
gno_t gid0 = 0;
if (rank <= leftOver)
gid0 = gno_t(rank) * (numLocalCoords+1);
else
gid0 = (leftOver * (numLocalCoords+1)) +
((gno_t(rank) - leftOver) * numLocalCoords);
if (rank < leftOver)
numLocalCoords++;
gno_t gid1 = gid0 + numLocalCoords;
gno_t *ids = new gno_t[numLocalCoords];
if (!ids)
throw bad_alloc();
ArrayView<gno_t> idArray(ids, numLocalCoords);
for (gno_t i=gid0, *idptr=ids; i < gid1; i++)
*idptr++ = i;
RCP<const tMap_t> idMap = rcp(new tMap_t(num, idArray, 0, comm));
delete [] ids;
// Create a Tpetra::MultiVector of coordinates.
scalar_t *x = new scalar_t [numLocalCoords*3];
if (!x) throw bad_alloc();
scalar_t *y = x + numLocalCoords;
scalar_t *z = y + numLocalCoords;
gno_t xStart = 0;
gno_t yStart = 0;
gno_t xyPlane = xdim*ydim;
//.........這裏部分代碼省略.........
示例7: if
/*! \brief Create a mesh of approximately the desired size.
*
* We want 3 dimensions close to equal in length.
*/
const RCP<tMVector_t> getMeshCoordinates(
const RCP<const Teuchos::Comm<int> > & comm,
zgno_t numGlobalCoords)
{
int rank = comm->getRank();
int nprocs = comm->getSize();
double k = log(numGlobalCoords) / 3;
double xdimf = exp(k) + 0.5;
ssize_t xdim = static_cast<ssize_t>(floor(xdimf));
ssize_t ydim = xdim;
ssize_t zdim = numGlobalCoords / (xdim*ydim);
ssize_t num=xdim*ydim*zdim;
ssize_t diff = numGlobalCoords - num;
ssize_t newdiff = 0;
while (diff > 0){
if (zdim > xdim && zdim > ydim){
zdim++;
newdiff = diff - (xdim*ydim);
if (newdiff < 0)
if (diff < -newdiff)
zdim--;
}
else if (ydim > xdim && ydim > zdim){
ydim++;
newdiff = diff - (xdim*zdim);
if (newdiff < 0)
if (diff < -newdiff)
ydim--;
}
else{
xdim++;
newdiff = diff - (ydim*zdim);
if (newdiff < 0)
if (diff < -newdiff)
xdim--;
}
diff = newdiff;
}
num=xdim*ydim*zdim;
diff = numGlobalCoords - num;
if (diff < 0)
diff /= -numGlobalCoords;
else
diff /= numGlobalCoords;
if (rank == 0){
if (diff > .01)
cout << "Warning: Difference " << diff*100 << " percent" << endl;
cout << "Mesh size: " << xdim << "x" << ydim << "x" <<
zdim << ", " << num << " vertices." << endl;
}
// Divide coordinates.
ssize_t numLocalCoords = num / nprocs;
ssize_t leftOver = num % nprocs;
ssize_t gid0 = 0;
if (rank <= leftOver)
gid0 = zgno_t(rank) * (numLocalCoords+1);
else
gid0 = (leftOver * (numLocalCoords+1)) +
((zgno_t(rank) - leftOver) * numLocalCoords);
if (rank < leftOver)
numLocalCoords++;
ssize_t gid1 = gid0 + numLocalCoords;
zgno_t *ids = new zgno_t [numLocalCoords];
if (!ids)
throw bad_alloc();
ArrayRCP<zgno_t> idArray(ids, 0, numLocalCoords, true);
for (ssize_t i=gid0; i < gid1; i++)
*ids++ = zgno_t(i);
RCP<const tMap_t> idMap = rcp(
new tMap_t(num, idArray.view(0, numLocalCoords), 0, comm));
// Create a Tpetra::MultiVector of coordinates.
zscalar_t *x = new zscalar_t [numLocalCoords*3];
if (!x)
throw bad_alloc();
ArrayRCP<zscalar_t> coordArray(x, 0, numLocalCoords*3, true);
zscalar_t *y = x + numLocalCoords;
zscalar_t *z = y + numLocalCoords;
zgno_t xStart = 0;
zgno_t yStart = 0;
//.........這裏部分代碼省略.........