Storing null value in a char array

Hi,

i need to store a value starting with null character in character array but i am getting buffer overflow exception. any method to store null character without terminating the array.

Need urgent help.

Thanks
Hi,

This is my idea, if you can store the values as a byte array then the problem will solve know?
actually i am working in qt on linux,, i tried using qbytearray but it behaves same as char array. Also char array is accepting null value in case of debug build but in case of release build it gives buffer overflow error.
You could use a std::string, or a QString since you are using Qt. Since it stores the size of the string, it can store the data including the null terminators.
There's nothing wrong with having an array of chars - or bytes - where one or more of the non-terminating elements is 0.

The problem comes when you want to treat that array as a string, because all of the standard library functions and classes will treat those zeroes as terminators.

If you want to use an array like that as a string, then you'll have to re-implement any string handling functionality yourself, rather than relying on the standard library.

In a previous job, I had to work on software that used char arrays with null-characters in the middle as strings. It was hugely error-prone. I'd recommend you don't do it, unless there's an overriding reason why you have to.

If you do decide you need to do this, I recommend that you encapsulate it in your own string class, so that it's clear throughout your program that your using an unusual construction. If you just pass them around as normal char arrays (or std::strings, or QStrings) then you run the risk of getting confused about which arrays you can use as normal strings, and which you can't.
Last edited on
its done, problem was somewhere else. thanx
Topic archived. No new replies allowed.