Topic: Macro and Memory questions
Author: Ben Jamali <bana@idcomm.com>
Date: 1999/09/04 Raw View
Hi All,
I have a couple of questions that I hope some one here can help:
Question 1. The MSDN library has the following definition (to the best
of my recollection) for the macro HIWORD(x):
#define HIWORD(x) (((WORD) >> 16) & 0xFFFF);
I know what the "(WORD) >> 16" part does, but what is the & 0xFFFF for?
Is it necessary?
Question 2: After a dynamically allocated string is created in a function,
does it still need to be destroyed? Or does the dynamically allocated
variable expire as soon as we are done with the function. For example:
char *StringFunction (char *String)
{
unsigned int STRING;
char Hello[] = "Hello";
int StrLength1, StrLength2, TotalStringLength;
StrLength1 = strlen (String);
StrLength2 = strlen (Hello);
TotalStringLength = StrLength1 + StrLength2;
String = (char *) malloc (TotalStringLength + 1);
return (String);
}
In this case, the new string "String)"should be returned to the calling
function. However, is it still necessary to free up the newly
constructed string (e.g. free (String)) ?
Any help is welcome and appreciated.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: brahms@mindspring.com (Stan Brown)
Date: 1999/09/04 Raw View
bana@idcomm.com (Ben Jamali) wrote in comp.std.c++:
>Question 1. The MSDN library has the following definition (to the best
>of my recollection) for the macro HIWORD(x):
>
>#define HIWORD(x) (((WORD) >> 16) & 0xFFFF);
>
>I know what the "(WORD) >> 16" part does, but what is the & 0xFFFF for?
>Is it necessary?
Ask yourself what is the result of HIWORD(-1) without the bit masking.
Some implementations propagate the sign of a signed quantity when right
shifting; others do not. (The standard allows either, calling the result
"implementation defined". I'm actually looking at the C standard here,
but I can't imagine that the C++ standard would differ.) Masking gives
you a 16-bit result, with all 0 bits in higher positions.
>Question 2: After a dynamically allocated string is created in a function,
>does it still need to be destroyed? Or does the dynamically allocated
>variable expire as soon as we are done with the function. For example:
>
>char *StringFunction (char *String)
>{
// irrelevant stuff removed
> String = (char *) malloc (TotalStringLength + 1);
> return (String);
>}
>
>In this case, the new string "String)"should be returned to the calling
>function. However, is it still necessary to free up the newly
>constructed string (e.g. free (String)) ?
Think about what you are doing. You allocate some memory on the heap, and
then return a pointer to that memory. If you had freed the memory, you'd
be passing back a pointer to unallocated memory. This would be a bad
thing.
The parentheses in the return statement are legal but not needed.
The function argument should probably be const char*, not char*.
malloc and free are legal in C++ as legacy from C. But why not use C++
memory allocation:
String = new char[TotalStringLength+1];
and then in the calling function, when it's done with the memory, delete
it with
delete[]
name_of_variable_that_received_the_return_value_from_the_function;
And finally the biggie: separating creation and deletion responsibility
this way is a fruitful source of errors. (Read in the FAQ at
http://www.cerfnet.com/~mpcline/c++-faq-lite/
about the dangers of arrays and pointers.) I won't say "always", but very
often you should think about using real strings #include <string> instead
of C-style "strings", which are just zero-terminated character arrays.
If you return a real string, instead of a pointer to an array, then the
calling program unambiguously owns the returned object, and there is no
possible confusion about memory.
--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
http://www.mindspring.com/~brahms/
I don't need e-mail copies of posted follow-ups, but if you send
them PLEASE identify them as such.
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]