|
I think both strncpy() and snprintf() sucks. Once I searched for hours for a bug that was caused by strncpy(). Here is the problem: strncpy() is suboptimal because even if you copy a 1 byte string, it fills the whole remaining part of the destination buffer with zeros unnecessarily, and on the other hand it doesn't zero terminate the string if its length equals to or bigger than the size of the dest buffer. The missing zero terminator character in similar cases is true for snprintf() too. I highly discourage everyone from using these functions.
|
|
|
|
|
bkelly13 wrote: In that case I wish to go on record as saying that we need a new set of these functions. It's no good making statements like this on CodeProject, you need to go and complain to the standards body.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
I did make a suggestion as to how you may alleviate this in my answer to your previous question.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Here is my opinion: strcpy is totally useless and totally unsafe. Use a string class in C++ and assign one string to another instead of strcpy. You can even implement refcounting if you write your own string class and then assignment will be super fast. You can of course use std::string for your needs.
If you write you own string class or if you extend your own string class from std::string then you can put in a format and vformat function (similarly to the MFC CString class) and you can do formatting so that the buffer size will be managed automatically for you. Here is an example to that: How to Format a String[^]. I would mention only one thing regarding this formatting function in the link: The use of the std::vector is unnecessary, you could put the string formatting directly to the string. even in that case the string resizing unnecessarily fills the whole string with a fill character before formatting the string into its buffer. With a custom string you can write more optimal code for this task.
The type safety of format parameters with the vararg nature of format functions is still endangering your program. Using the bad parameter types make your program crash at runtime when the formatting code gets executed. Example to a buggy formatting statement: format("Name: %s\n", 5) . Gcc has an __attribute__ format printf to defend against such mistakes at compile time. You can mark you format function with this attribute and then gcc will catch such errors, it would find out for exmple that "%s" and 5 are not compatible. Unfortunately visual C++ doesn't have such feature but our current project is crossplatform so we compile it with both VC++ and gcc and this gcc feature is priceless.
Unfortunately this kind of formatting is too widespread and comfortable without similarly comfortable alternatives so this wont be dropped easily from C++. However a feature like the one gcc has can make this stuff quite usable and safe.
|
|
|
|
|
You could try something like Boost's format[^] library.
Steve
|
|
|
|
|
Environment: Windows 7, Visual Studio 2008, MFC, CPP
Goal: start with an int, and put the character string equivilant into a text format such that a static control variable from a dialog will accept it. mp_C_Client is a pointer to a class that contains the TCP/IP code. The error code is returned as expected.l
void C_MFC_Winsock_01_Dlg::OnBnClickedBtnClientInit()
{
bool method_status = false;
int wsa_error_code = 0;
TCHAR wsa_error_text[ 16 ];
mp_C_Client = new C_Client;
if( mp_C_Client != NULL )
{
method_status = mp_C_Client->Class_Initialize();
wsa_error_code = mp_C_Client->Class_Get_WSA_Error_Code();
wprintf( wsa_error_text, "%d", wsa_error_code ); m_client_wsa_error_text.SetWindowText( wsa_error_text );
}
Intellisense shows the control variable argument as:
m_client_wsa_error_text.SetWindowText( LPCTSTR lpszstring )
This compiles but after the wprintf the text in wsa_error_text is not as expected.
How should this be coded?
Thanks for your time
Edit: This post looked pretty good in the preview, but not good after I posted it.
And again: But now its okay. ????
|
|
|
|
|
bkelly13 wrote: wprintf( wsa_error_text, "%d", wsa_error_code ); You should be using _stprintf() [^] to format the string into a buffer.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That got me where I needed to go. I used this:
int wsa_error_code;
TCHAR wsa_error_text[ 16 ];
...
_stprintf_s( wsa_error_text, 15, L"%d", wsa_error_code );
Note the _s on the end and the L prefixing the "%d"
This nonsense with all the different character types is way out of hand. After I get over this hurdle of writing some async TCP/IP code, I think I should re-create the project and get rid of all the multibyte and unicode nonsense.
Thanks for your time
Edit: The post was nicely formated in the preview, but looks bad after being submitted.
|
|
|
|
|
Unicode is the default compile type for Visual Studio projects.
If you would like to work in multi-byte only, you can change that property in your compiler settings on the General page.
The _stprintf_s function compiles using the character format based on that compiler setting.
If you don't want to mess with the compiler settings, you can use a char rather than a TCHAR, and the function sprintf_s,
or if you want to force the unicode type use the data type wchar_t, and the function wsprintf_s.
All of my software is powered by a single Watt.
|
|
|
|
|
bkelly13 wrote: Note the _s on the end and the L prefixing the "%d" The _s is a good thing as it helps to protect against buffer overflow, but less critical when you are sure that your buffer is big enough. The L prefix on your string is wrong, however, as you are using TCHAR so your buffer could be ASCII or Unicode. You should use the _T() macro around your strings, and the _countof() macro to specify the size thus:
TCHAR wsa_error_text[ 16 ];
...
_stprintf_s( wsa_error_text, _countof(wsa_error_text), _T("%d"), wsa_error_code );
Alternatively make your project all ASCII or all Unicode.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Cool, thanks for the aditional info.
I looked up _countof and discovered the return type is not declared anywhere. Is it an int, unsigned int, size_t, or something else?
The _countof function does not accomodate the null that belongs at the end of a character string. That is important because,...
The various versions of ...printf...( destination, count, source ) that I have tried appear to put a limiting number on the count of characters transferred. However, that limit takes the form of a program crash. Downright nasty! Just as bad as ASSERT.
IMNSHO, these functions should use the count argument to limit the number of characters being transfered rather than crashing the program. Do any of these "safe", or maybe "less dangerous" functions simply limit the number of character moves rather than crashing the app?
( BTW: In My Not So Humble Opinion )
Thank you for taking the time to reply.
|
|
|
|
|
bkelly13 wrote: I looked up _countof and discovered the return type is not declared anywhere. If you look closely you will see that it is a macro which invokes the sizeof() operator, so it returns the same type as that.
bkelly13 wrote: The various versions of ...printf. The functions with the _s suffix and the buffer length parameter will avoid the crash by giving you an error return or ASSERT if you attempt to overwrite the buffer, so these functions should always be used for safety. The old "unsafe" versions are part of the base C libraries and remain for backwards compatibility. As you so rightly say, they should not be used any more.
You can trap any ASSERT s that may be raised by adding your own handler as described here[^].
It always pays to read the documentation (including any linked pages) thoroughly. Not that I do so very often.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
dose any one know how to mirror system image list , iam developing RTL application on LTR windows xp , so i want to mirror image before setting listctrl so it will not look upside down ,
her what i did so far and i failed :
CMFCToolBarImages toolBarImage;
SHFILEINFO sfi;
UINT nFlags = SHGFI_SYSICONINDEX | SHGFI_ICON ;
HIMAGELIST hIL = (HIMAGELIST)SHGetFileInfo(szWindows, 0, &sfi, sizeof(sfi), nFlags);
CImageList* list = CImageList::FromHandle(hIL);
HBITMAP bitmap = list->GetImageWell();
toolBarImage.MirrorBitmap(bitmap,32);
any idea or small hint will be appreciated ?
asdasda
|
|
|
|
|
Environment: Windows 7, Visual Studio 2008, MFC, CPP
Two questions:
1. How does the asynchronous notification get back into the program?
2. What is the purpose of the class MyEchoSocket?
Details
I am working my way through the tutorial socket Programming with MFC (Part 1), written by Jobin Wilson and found on this website here:
Socket Programming with MFC (Part 1)
I am good through the point where the server calls the Listen() method. There is no client at this time and the code paused for a second or so then continues. My server class has the base class: CAsyncSocket so I presume the OS will eventually send a notification as to when a client connects. What part of the code says to the OS, when the client connects, call function X?
The next operation is to call the Accept( <argument uncertain=""> ) method. Under topic Accepting Connections Wilson suddenly uses m_sConnected as the argument to Accept without explanation. Looking at the downloaded code I think this is an instance of his class MyEchoSocket. But all he does with that class is call methods from the base class CAsyncSocket. Why not just call the CAsyncSocket methods directly?
Edit: The link did not work for me. Try again. I think it is Ok now.
Edit 14 Sep 2012
I think I am beginning to understand.
For question 1:
The programmer must override a set of methods from the base class. These include OnAccept(), OnConnect(), OnReceive(), etc. Windows calls these methods when the event has happened.
For question 2:
When the server code runs, it initiates a listen activity, Windows is listening for a client to connect. When one does, skipping some stuff, an instance of the class written to handle the traffic is created and put into action. That would be an instance of the MyEchoSocket. That class then performs all the sends and receives with the client. Meanwhile, the original server object is fee to go back to listening for another client connect.
No one has posted a reply and this is what I think is supposed to happen. I am still working my code. Please jump in if you have any corrections or comments.
Thanks for your time
modified 14-Sep-12 22:00pm.
|
|
|
|
|
The call to Listen is opening a socket for your server and it basically says "I'm interested in new requests..."
All that is happening at this point is some state has been setup.
The call to accept is actually where the connection is made. The variable m_sConnected is the variable that will receive the new connected socket. Notice how the rest of the calls made if Accept() succeeds are performed on m_sConnected .
In the meantime, m_sListener is still there ready to accept more connections. So if you start another client and then call Accept() with another socket variable, you would have two active client sessions in your application.
All of my software is powered by a single Watt.
|
|
|
|
|
I used AfxGetThread()->m_nThreadID to get the thread ID in my code I get a thread ID, and if I use GetCurrentThreadId() at the same line of code and I get different thread ID. Which one is correct?
Thanks,
Kishore
|
|
|
|
|
I think, AfxGetThread() gets threadId after start of main thread after getting duplicate of handle of thread, because own handle is pseudohandle.
I thing real threadId is from API version of GetCurrentThreadID.
But... same thread would has to have same threadId. It's weirdness
|
|
|
|
|
In VC6 when I use
std::abort(); I am geting an error "error C2039: 'abort' : is not a member of 'std'". But it compiles well in VS2010. I am seeing the definition of abort in both version of stdlib.h. What is the problem?
|
|
|
|
|
The problem is that stdlib.h does not define the std namespace, it is the standard C library header. You should include cstdlib for use of the std namespace features.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Even after including cstdlib, I am seeing the same error.
|
|
|
|
|
Difficult to guess without more information; take a look at the header file to see how it is declared.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Throw VC6 in the bin, it's long past its useby date.
Steve
|
|
|
|
|
Thanks. Yes, That's right! we are slowly moving to VS2010. But at this point of time we are in a situation to use
std::abort(); in both VC6 and VS2010. This compiles well in VS2010 but my concern is why its not in VC6. Any idea would be helpful.
|
|
|
|
|
Why not just use abort() ? Adding the namespace prefix buys you nothing.
One of these days I'm going to think of a really clever signature.
|
|
|
|