Discussion Locked

C++ Forum

0 helpful
22 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Welcome to the C++ Forum. This is the place to post your C++ questions, share your best techniques, and express your opinion on topics that impact IT development.

We also encourage you to post your comments about the C++ TechMail.

If you haven't subscribed to the C++ TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:

http://builder.com.com/techmails.jhtml?repID=u001

22 replies

by Mark W. Kaelin

Understand the benefits of smart pointers:

The Feb. 12, C++ TechMail discusses the benefits of smart pointers. Have you used smart pointers in your application development? Share your experiences.

by dairoc

I have a question. When i use smart pointer and ends with an exception it's necessary in order to free the smart pointer with the RELEASE command.
My application consumes to much memory.
Thanks

by john torjo

Hi,

It depends on the type of smart pointer you use.
Usually that's the purpose of a smart pointer: so that you don't have to remember to release the pointed object, even in the presense of exceptions. Therefore, you should not have to manually release it (but do check the documentation of the smart pointer you use).

Best,
John

by Mark W. Kaelin

Learn about the placement new operator:

The Feb. 19, C++ TechMail discusses memory allocation with the placement new operator. Have you used placement new to preallocate memory in your applications? Tell how you used it and why.

by solrak29

Where does alignment come into play?
And why allocate the extra 100 bytes?

Pleas email me if you reply...solrak29@yahoo.com

by john torjo

Alignment comes into play with more complex structures, on certain platforms. For example, on certain platforms a 'long' variable can be found only at addresses that are multiple of 4 (0x00000004, 0x00000008,0x0000000C, etc.).
Alignment is a very delicate issue in itsown.

Click here for a pretty advanced article:
http://www.cuj.com/experts/2004/alexandr.htm?topic=experts
http://www.cuj.com/experts/2006/alexandr.htm?topic=experts
http://www.cuj.com/experts/2008/alexandr.htm?topic=experts

To my shame, the extra 100 bytes - it was mistake - it was no need for them.

Best,
John

by mohn3310

Some simple stuff but just wanted to check...

strBuff is a char pointer to a chunch of memory. So if you add to this pointer (base) you will point to the next character or byte.

When this pointer is cast to a CTest pointer, adding to it will move sizeof( CTest ) ahead.

by john torjo

> When this pointer is cast to a CTest pointer, adding to it will move sizeof( CTest ) ahead.

Yes, indeed.

For the sake of the example, I used a char array to symbolize an array of bytes - indeed just a chunk of memory. And on this chunk, wecan create objects using placement new.

Best,
John

by Mark W. Kaelin

Test code with a simple framework, part 1:

A recent C++ TechMail discusses testing methods. How do you test your C++ code? Do you write test routines as suggested in the tip? Tell us your best techniques for testing code.

by solrak29

I like the use of asserts in the testing code.
Also I like the fact of creating a testing directory and organizing your code, but litering
you code with ifdefs for testing can get harry. But I do understand that certain parts of the code can and/or should not executed when testing, but your missing the actual value of unit testing...right? I would rather take the extreme programming approach and design your code for testing. Thus, make sure your class, functions, or whatever are testable within the framework and you dont have to put ifdef in the production code.

It seesm straight forward, but the anonymous namespace only allows functions in the same file access...????

by Mark W. Kaelin

Parse strings more easily with the to_stream function:

The March 3, C++ TechMail discusses parsing strings with stringstream. Have you used the to_stream function to parse strings? Tell us how it worked for you.

by Mark W. Kaelin

Implement exceptions to validate streams:

The March 5, C++ TechMail discusses handling stream errors with exceptions. How do you manage stream errors? Do you use member function exceptions?

by tehilinski

Not mentioned is that std::ios_base::failbit is also set upon encountering an end-of-file condition. This will trigger an exception if exceptions are enabled for failbit. In that case, you need to check for eof in the catch block (probably using ios::eof() ).

by maryweilage

The Mar. 10, 2003, C++ e-newsletter discusses resetting the stream state when encountering invalid data. What are your thoughts on this subject? Did you find the code examples in the newsletter helpful? Please let us know.

by volker.goehrke

Hi,

your article about setting the stream state correctly when encountering invalid data was very interesting. I know now, how to handle an invalid state from the perspective of the operator implementation, but when i'm using the input operator, how can i display an error message, which points the user in the right direction? How should the caller of the operator deal with this situation?

I only played a little bit with input operators and it seemed more natural to me, to throw an exception with a detailed description of what was going on. My implementation of your example would be more like this:

template< class char_type, class char_traits>
std::basic_istream< char_type, char_traits> & operator >> (
std::basic_istream< char_type, char_traits> & streamIn, complex & value)
{
int nReal, nImag;

char_type ch;
streamIn >> ch;
if ( ch != '(')
throw ComplexBadFormatException("Missing opening bracket.");

streamIn >> nReal;
streamIn >> ch;
if ( ch != ',')
throw ComplexBadFormatException("Missing colon.");

streamIn >> nImag;
streamIn >> ch;
if ( ch != ')')
throw ComplexBadFormatException("Missing closing bracket.");

value.m_nReal = nReal;
value.m_nImag = nImag;

return streamIn;
}

greetings, Volker.

by Mark W. Kaelin

Return temporaries with proper coding techniques:

The March 12, C++ TechMail discusses the proper coding techniques for dealing with return temporaries. How do you handle return temporaries in your applications? Do you have a better technique? Tell us about it.

by solrak29

I found your atrictl very informative and good reminder in returning temporaries and example of temporary variable and the gotchas inbetween. I have to points though:

1. under djgpp there is no basic_stringstream...any ideas? I thought djgpp isthe same as gcc which uses the Silocone G STL...????

2. I would like to see a main for the "correct implementation"...??

3. Whats the difference between a stream and a stringstream??

by solrak29

After looking more at the code, I see you have the to_string implementation done....

nice article, but why doesn't the first implemenation work?

by Mark W. Kaelin

Allow easier usage of your classes with the setter class:

The March 17, C++ TechMail discusses providing access or modifier functions, also known as getter and setter functions, when creating new classes. Have you used this technique in your applications? What class were you developing?

If you haven't subscribed to the C++ TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:
http://builder.com.com/techmails.jhtml?repID=u001

by Mark W. Kaelin

Redirect STL streams with .rdbuf() function:

The March 19, C++ TechMail discusses how to redirect then input and output of C++ Standard Template Library streams. Have you used the .rdbuf() function to redirect STL streams before? Can you use thistechnique in your application development? Tell us about it.

If you haven't subscribed to the C++ TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:

http://builder.com.com/techmails.jhtml?repID=u001

by Mark W. Kaelin

Read and write stream buffers from/to streams:

The March 24, C++ TechMail discusses how to read and write stream buffers using the rdbuf() function. Do you need to read or write stream buffers in your applications? Do you have a better way? Shareyour technique.

If you haven't subscribed to the C++ TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:

http://builder.com.com/techmails.jhtml?repID=u001

This discussion is closed to new replies.