STL疑问 #include <windows.h> class DistDrivers { public: virtual void GetLogicalDriveStrings()=0; public: int m_LogicalNum;//系统中逻辑驱动器的个数 std::vector<wchar_t*> m_LogicalNames;//系统中所有的驱动器字符串的信息 int m_DriveType;//驱动器类型 float m_i64TotalBytes;//磁盘总容量 float m_i64FreeBytesToCaller;//磁盘剩余容量 }; class MyDistDrivers:public DistDrivers { public: void GetLogicalDriveStrings() { TCHAR buf[100]; DWORD len=::GetLogicalDriveStrings(sizeof(buf)/sizeof(WCHAR),buf); for(wchar_t* s=buf;*s;s+=wcslen(s)+1) { LPTSTR sDrivePath=s;//单个盘符 m_LogicalNames.push_back(sDrivePath); } vector<wchar_t*>::iterator iter=m_LogicalNames.begin(); for(;iter!=m_LogicalNames.end();iter++) { wcout<<*iter<<" "; } cout<<endl; } }; int main() { DistDrivers *p=new MyDistDrivers(); p->GetLogicalDriveStrings(); MessageBox(NULL,*p->m_LogicalNames.begin(),*p->m_LogicalNames.begin(),NULL); return 0; }
出错的图片如下:
解释: 以上的程序犯了一个很原则的错误,就是局部变量的内存在他的生命期结束后就一定会释放他占用的内存。这时候,vector中包含的指针都是野指针。造成了程序的错误。
改正如下:
#include <windows.h> class DistDrivers { public: virtual DWORD GetLogicalDrives(void)=0; virtual void GetLogicalDriveStrings()=0; virtual UINT GetDriveType(LPCTSTR lpRootPathName)=0; virtual BOOL GetDiskFreeSpaceEx(LPCTSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailable, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes)=0; public: int m_LogicalNum;//系统中逻辑驱动器的个数 std::vector<wchar_t*> m_LogicalNames;//系统中所有的驱动器字符串的信息 int m_DriveType;//驱动器类型 float m_i64TotalBytes;//磁盘总容量 float m_i64FreeBytesToCaller;//磁盘剩余容量 LPTSTR sDrivePath;//新增加的变量 用来临时存储字符串。这个内存只会在程序结束的时候才会释放掉。 };
#include <string.h> #include <wchar.h> class MyDistDrivers:public DistDrivers { public: DWORD GetLogicalDrives(void) { int DiskCount=0; DWORD DiskInfo=::GetLogicalDrives(); while(DiskInfo) { if(DiskInfo&1) { ++DiskCount; } DiskInfo=DiskInfo>>1; } m_LogicalNum=DiskCount; return m_LogicalNum; } void GetLogicalDriveStrings() { TCHAR buf[100]; DWORD len=::GetLogicalDriveStrings(sizeof(buf)/sizeof(WCHAR),buf); for(wchar_t* s=buf;*s;s+=wcslen(s)+1) { sDrivePath=s;//单个盘符 sDrivePath=(WCHAR*)malloc(sizeof(WCHAR)*(wcslen(s)+1)); wcscpy(sDrivePath,s); cout<<"Address of String[i]:"<<&sDrivePath<<endl; int i=0; m_LogicalNames.push_back(sDrivePath); cout<<"Address of String[i] in vector:"<<&m_LogicalNames.at(i)<<endl; i++; } } UINT GetDriveType(LPCTSTR lpRootPathName){return 1;} BOOL GetDiskFreeSpaceEx(LPCTSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailable, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes){return TRUE;} };
#include <string.h> #include <wchar.h> class MyDistDrivers:public DistDrivers { public: DWORD GetLogicalDrives(void) { int DiskCount=0; DWORD DiskInfo=::GetLogicalDrives(); while(DiskInfo) { if(DiskInfo&1) { ++DiskCount; } DiskInfo=DiskInfo>>1; } m_LogicalNum=DiskCount; return m_LogicalNum; } void GetLogicalDriveStrings() { TCHAR buf[100]; DWORD len=::GetLogicalDriveStrings(sizeof(buf)/sizeof(WCHAR),buf); for(wchar_t* s=buf;*s;s+=wcslen(s)+1) { sDrivePath=s;//单个盘符
//将局部变量s的内存拷贝到类成员变量sDrivePath中 sDrivePath=(WCHAR*)malloc(sizeof(WCHAR)*(wcslen(s)+1)); wcscpy(sDrivePath,s); cout<<"Address of String[i]:"<<&sDrivePath<<endl; int i=0; m_LogicalNames.push_back(sDrivePath); cout<<"Address of String[i] in vector:"<<&m_LogicalNames.at(i)<<endl; i++; } } UINT GetDriveType(LPCTSTR lpRootPathName){return 1;} BOOL GetDiskFreeSpaceEx(LPCTSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailable, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes){return TRUE;} };
以上就解决了这个问题。