|
Many memory problems come from inherent issue with pointers that lets you copy the reference to an allocated object without restriction, but then doesn't let these references track when someone deletes it. Neither indirection nor garbage collection can solve that. Instead, use smart pointers!
Smart pointers behave for the most part like normal pointers, but they also make sure that the allocated objects get released when they are no longer referenced. And not earlier!
You can check your preferred search engine on this topic or just take the first hit:
c++ - What is a smart pointer and when should I use one? - Stack Overflow[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
HS_C_Student wrote: My problem is that as I write larger and more complicated (C) programs, errors in manual memory management become much harder to find and more catastrophic, particularly if caused by memory corruption....I couldn't find a standard practice
Err...because you are going at it from the wrong end. You are trying to fix it after it happens rather than stopping it before it occurs.
You do the latter by strict care in your design and implementation.
Create design that strictly specify where allocations occur and where they are cleaned up.
Implement code that follows that and if you allocate something then BEFORE you do anything else implement, strictly, how it is cleaned up.
That also applies to how you use each pointer also. If you are code a piece of code and your are not sure how to use it, whether it is defined or what you can put in it, then you have already done something wrong.
|
|
|
|
|
Hi, I'm new to Socket Programming, I don't know how to create a NamedPipe Server and Client Application, Here My requirement is below
1.) Create multithreaded pipe server and pipe Client inter-process sample application VC++/MFC.
2.) Transfer unsigned Char arr[50] dummy bytes to and fro pipe client.
3.) Result display in printf both side.
By using VC++ or MFC Classes,
Can any one help me please?
Thanks,
|
|
|
|
|
You may want to download .zip with CNamedPipe class implementation from Naughter Software
|
|
|
|
|
Thank you I got some idea
|
|
|
|
|
Man! That website is straight out of 1995!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
And what? Does't the code work in VS2017?
|
|
|
|
|
Is it your website? I didn't mean any offense. I'm just exclaiming.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No!
But I know this guy and I trust him.
Besides, I used some code example from his site.
PS: just read something about him! There is quite enough info about him in the web.
|
|
|
|
|
|
|
Can anyone suggest me how to start learning programming by myself? Since I am a newbie and hardly have any knowledge of programming.
Your kind reply will be very helpful.
Thanks
|
|
|
|
|
Tutorials, books, programming, programming, programming (get a decent, updated compiler, e.g. g++ ).
You might easily find many many resources freely available on the web, Google is your friend.
|
|
|
|
|
I have CListCtrl and updating this control with love data using OnTimer(). But i want to select some rows and do furthur actions. But when i select the row, the selected color appears,but moment it updating using OnTimer(), the selected row color become normal. How can i keep the selection row visible always?
Anu
|
|
|
|
|
Store the selected item index (or, maybe, item data) before the updating the list.
After update select the stored item.
|
|
|
|
|
Hi All,
I'm trying to create a C++ DLL using the WinInet API, but for some odd reason, I get no errors during compiling but get external link errors regarding variables within my class. Any idea how I can resolve this? Visual Studio 2015.
----------------------------------------header test.h -------------------------------------------
// #include "stdafx.h"
using namespace std;
// #include <Winineti.h>
#include <windows.h>
#include <strsafe.h>
#include <wininet.h>
#include <math.h>
#include <cstring>
#include <WinInet.h>
//#include <string>
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "user32.lib")
#define FTP_FUNCTIONS_BUFFER_SIZE MAX_PATH+8
namespace Win32_FTP
{
class FTP_Win32_ClientV1
{
private:
public:
static LPCWSTR sFileSearch;
static char * sIpAddress;
static LPCWSTR sUsername;
static LPCWSTR sPassword;
static string sRemoteDir;
static string sLocalDir;
static string sFilename;
static DWORD dwError;
static HINTERNET hFTPConnect;
static HINTERNET hFind;
static HINTERNET hConnect;
static HANDLE hFile;
static WIN32_FIND_DATA FindFileData;
static string strTemp;
static __declspec(dllexport) void Test1();
static __declspec(dllexport) void FtpCloseConnection();
};
}
----------------------------------------source test.cpp -------------------------------------------
//using namespace std;
#include <iostream>
#include "test.h"
// using namespace std;
namespace Win32_FTP
{
void FTP_Win32_Client::Test1()
{
cout << "This is a test from Cplus to Csharp DLL";
}
void FTP_Win32_Client::FtpCloseConnection()
{
InternetCloseHandle(FTP_Win32_Client::hFind);
InternetCloseHandle(FTP_Win32_Client::hFTPConnect);
InternetCloseHandle(FTP_Win32_Client::hConnect);
}
}
***************************LINK ERRORS ************************
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP::FTP_Win32_Client::hFTPConnect" (?hFTPConnect@FTP_Win32_Client@Win32_FTP@@2PAXA)
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP::FTP_Win32_Client::hFind" (?hFind@FTP_Win32_Client@Win32_FTP@@2PAXA)
Test.obj : error LNK2001: unresolved external symbol "public: static void * Win32_FTP::FTP_Win32_Client::hConnect" (?hConnect@FTP_Win32_Client@Win32_FTP@@2PAXA)
|
|
|
|
|
|
Class static variables, like Win32_FTP::FTP_Win32_Client::hFTPConnect must be also defined in one source file. That is you have to write
HINTERNET Win32_FTP::FTP_Win32_Client::hFTPConnect; (and so on...) in your test.cpp source file.
An altrernative, available only with C++ 17 compliant compilers is to declare such variables inline inside the header file (see, for instance INLINE VARIABLES[^]).
|
|
|
|
|
Why are you creating a class and then making everything static?
|
|
|
|
|
Definitely remove the line: using namespace std;
My advice: NEVER do that.
|
|
|
|
|
There is Base class A which has child's B, C, D, ... Z.
I need to add some methods to Z as virtual ones so that they will be overridden in Z child classes Z1, Z2, .. Zn.
Is it possible, how can I do it.
Note: I cannot add to base class A.
|
|
|
|
|
Start by telling us why you need to add the virtual methods. And why you can't add to the base class A.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Well I have over 50 classes there. All of them have base class the same.
Those functions (about 20 functions) are needed only for my one 5 level class (Class A5 : public A4) and two 6 level child classes ( Class A61 : public A5 and Class A62 : public A5).
Reason because I want to make one handler class which will use A5 class virtual functions and will handle A61 and A62.
|
|
|
|
|
Quote: I need to add some methods to Z as virtual ones so that they will be overridden in Z child classes Z1, Z2, .. Zn.
Is it possible, how can I do it. Yes it is possible. Just do it.
|
|
|
|
|
You might not need to make them virtual.
|
|
|
|