Hello All,
I am creating the array of size: 26627664 bytes then convert the byte to char array.
While deleting the array i got an code gaurd log error that Bad Parameter.

I am doing like this
Char* pBuffer = (Char*)new BYTE[26627664];

and deleting like this,
if(pBuffer != NULL)
delete[] pBuffer;
facing the problem
Please Help.

Dani AI

Generated

This sounds like a classic heap-corruption / mismatched-allocation problem rather than a mysterious delete[] bug. allocated a large raw buffer and then saw a CodeGuard/CRT "Bad parameter" when freeing it. 's suggestion to write at the index equal to the allocation size will write one past the end (as pointed out) and is exactly the kind of overwrite that corrupts the debug heap header and causes that error at delete time.

Most likely causes to rule out:

  • out‑of‑bounds writes (off‑by‑one or overruns) — the single most common cause;
  • deleting a pointer that was modified (not the original pointer returned by new[]);
  • double delete or deleting memory not allocated with new[];
  • crossing CRT boundaries (allocating in one CRT/DLL and freeing in another) or otherwise mismatched allocators;
  • undefined behavior from casting between unrelated pointer types.

Practical steps to isolate and fix:

  • Rebuild with the debug runtime and run under the debugger. Use heap-checking tools (AddressSanitizer/Valgrind on Unix, Visual Studio debug heap / _Crt* APIs on Windows) to catch overruns.
  • Replace the raw new/delete with RAII to see if the problem disappears. For example, use a vector or a smart pointer so you avoid manual delete:
std::vector<unsigned char> buffer(size);
auto buffer = std::make_unique<char[]>(size);
  • Add sentinel (canary) bytes before/after the buffer to detect writes past the ends. If the sentinel changes, you have an overrun.
  • Confirm what BYTE and Char actually typedef to; prefer standard types (char, unsigned char, std::uint8_t) and avoid casting between pointer types.
  • If allocation and deallocation happen across DLLs, ensure both use the same CRT build.

If these steps still don’t reveal the culprit, produce a minimal reproducible test case that allocates, writes only within bounds, and frees. That will either reproduce the error (showing a compiler/runtime/toolchain issue) or point back to hidden corrupting code elsewhere.

Recommended Answers

All 2 Replies

Add this line of code in and see if the array will delete:

pBuffer[26627664] = '\0';

>pBuffer[26627664] = '\0';
You've got an off-by-one error. Do try to remember that array indexing in C is based on an offset, not an item count. pBuffer[0] is the first item, so pBuffer[N-1] is the last item, and pBuffer[N] is an overflow error.

>Char* pBuffer = (Char*)new BYTE[26627664];
How are BYTE and Char defined?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.