P
Paul N
I have a program which handles lines of text. My question relates to
where I have two strings (ie arrays of char or wchar_t) and want to
malloc a new array to hold the concatenated string.
My first thought was that you would need to check whether adding the
sizes was going to overflow a size_t. Otherwise you might allocate a
new array which was too small.
My second thought was that this was unnecessary. If a size_t can span
the available memory, and you already have both strings in memory,
then their combined length must fit in a size_t. So no problem.
But on third thoughts - does a size_t have to span the available
memory? My understanding is that it has to be big enough to hold the
size of any object. But there's no reason why you should be able to
allocate a single object filling the entire available memory. I
wondered if it were possible to have a system in which, say, there ere
several Megs of memory but it could only be allocated in segments of
64K (I think the 286 used to work like this) and so a size_t need only
be 16 bits?
So do I need to worry about this or not? And is it the same in C and
in C++? Plus, any further comments (other than general disparagement
of malloc or of handling strings by hand) welcome!
Thanks.
Paul.
where I have two strings (ie arrays of char or wchar_t) and want to
malloc a new array to hold the concatenated string.
My first thought was that you would need to check whether adding the
sizes was going to overflow a size_t. Otherwise you might allocate a
new array which was too small.
My second thought was that this was unnecessary. If a size_t can span
the available memory, and you already have both strings in memory,
then their combined length must fit in a size_t. So no problem.
But on third thoughts - does a size_t have to span the available
memory? My understanding is that it has to be big enough to hold the
size of any object. But there's no reason why you should be able to
allocate a single object filling the entire available memory. I
wondered if it were possible to have a system in which, say, there ere
several Megs of memory but it could only be allocated in segments of
64K (I think the 286 used to work like this) and so a size_t need only
be 16 bits?
So do I need to worry about this or not? And is it the same in C and
in C++? Plus, any further comments (other than general disparagement
of malloc or of handling strings by hand) welcome!
Thanks.
Paul.