只是不要将其调整为超过大小限制:
char* realloc_lim(char* data, int new_count, bool &ok)
{
if(sizeof(char) * new_count > SIZE_LIMIT)
{
ok = false;
return null;
} else {
ok = true;
return (char*)realloc((void*)data, sizeof(char) * new_count);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样使用它:
bool allocation_ok = false;
int newsize = readint(); // read the size as an int from somewhere
buffer = realloc_lim(buffer, newsize, &allocation_ok);
if(!allocation_ok)
{
printf("Input size was too large!\n");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
426 次 |
| 最近记录: |