|
|
If I define a variable in a function, its scope remains local to that function.
Suppose I define a new instance of a class and a pointer in a function. Is the pointer local to the function? Can it be referenced outside the function? Do I have to declare the pointer in the header if I want to use it outside the function?
For example:
X::SomeFunction()<br />
{<br />
int ABC = 0;<br />
Y* pYY = new Y; //Creates new instance of class Y
}
Integer ABC is limited in scope to function SomeFunction().
Is there any limitation to the scope of pYY ? Can pYY be used in any other function other than SomeFunction() ?
Thanks
|
|
|
|
|
The scope of the variable pYY itself is limited to X::SomeFunction . However, given that the object that pYY is pointing to has been allocated on the heap that object continues to exist beyond X::SomeFunction . You could for instance return pYY from X::SomeFunction and use it at the caller site.
|
|
|
|
|
Hello..
Is there anyone here have the code for detecting MAC address in windows XP in C++. Im using Visual C++ 6.0. Thank you
|
|
|
|
|
|
GetAdapterInfo requires the iphlpapi.lib/h. It seems that i dont have that lib and header file. I tried to download it separately and i try run the sample program from msdn but it still have all these syntax error which i couldnt understand why it occured (eg: missing ; but i see that there is no problem with the syntax). Currently im using Visual C++ 6.0. Must I upgrade the latest SDK and install the latest Visual C++. The latest is Visual C++ 2005 right? Since im still a student doing my final year project, i found that im kinda slow in this. What are other steps that i could do to upgrade my programming kit and also, is there any code that can provide me a full code to detect MAC address?
|
|
|
|
|
Go to the very FIRST Syntax error, and see how it occurred. There may be a Header Missing, but often you can write your own to get out of a bind. The most common reason with downloaded stuff like that is, incorrect or missing manifest constants, or esoteric looking typedeffed names, ultimately resolving to an int of the correct size. A bit of experimentation is both educational, and often gives less headaches than upgrading the entire SDK.
Regards,
Bram van Kampen
|
|
|
|
|
No you do not need to upgrade from VC++ 6.0 just to use the IP Helper API. And yes the latest Visual C++ production release from Microsoft as of date is Visual C++ 2005. As for the syntax errors you should probably just listen to Bram van Kampen!
|
|
|
|
|
Thanks. I will try my best to solve this problem. Thanks again. Hopefully if i have any problem, you guys can help me right? Really appreciate that. Thanks a lot for the tips.
|
|
|
|
|
Hello,
Does anyone know how to get files list in a directory in c++? i searched it on google, i didnt find proper information about it?
can somebody know where i can get more specific information about it?
thanks
|
|
|
|
|
Hi,
kernel32.dll holds FindFirstFile() and FindNextFile() functions for this.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Gofur Halmurat wrote: Does anyone know how to get files list in a directory in c++?
Technically, C++ has no such functionality. You can, however, use FindFirstFile() and FindNextFile() .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
////////////Use of FindFirstFile/FindnextFile
WIN32_FIND_DATA win32;
BOOL Bool=1;
HANDLE handle;
handle=FindFirstFile(_T("*.*"),&win32);
if(handle!=INVALID_HANDLE_VALUE)
while(Bool!=0)
{
m_List.AddString(win32.cFileName);
Bool=FindNextFile(handle,&win32);
}
FindClose(handle);
/////////////use of DlgDirList for replace contents of ListBox with directorys
TCHAR lpt[260];
lpt[0]='\0';
DlgDirList(lpt,IDC_LIST1,NULL,DDL_DIRECTORY|DDL_ARCHIVE);
|
|
|
|
|
Hamid. wrote: BOOL Bool=1;
while(Bool!=0)
why don't you just write this:
BOOL isFileFound = TRUE;
while (!isFileFound) ??
in a general mean, prefer naming your variables with a representative name of what they are used for ?
|
|
|
|
|
I think this code is better:
WIN32_FIND_DATA win32;
HANDLE handle;
handle=FindFirstFile(_T("*.*"),&win32);
if(handle!=INVALID_HANDLE_VALUE)
do
{
m_List.AddString(win32.cFileName);
}while(FindNextFile(handle,&win32)!=0);
FindClose(handle);
and we dont need to any boolean variable.;)
|
|
|
|
|
Just looking for fancy C++ sockets library -- client side:
Must:
* Support SSL connections
* Support AES 128
Should:
* Use I/O Completion
* not create hidden windows
I already use the AsyncSocket classes -- but find I need a bit more than it offers.
Commercial Software OK -- can pay.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<A HREF="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</A> *
|
|
|
|
|
ACE is pretty nice, it's quite comprehensive. Docs are so-so (or used to be - I haven't used it in a while).
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
VCF Blog
|
|
|
|
|
Hello all.
I have a class Item and a typedef std::vector<Item> Items; . Both reside within a DLL. Then, from my exe, I do something like:
void FillList(Items& items)
{
}
bool Foo()
{
Items items;
FillList(items);
return true;
}
when ending Foo , the compiler (VC8) is throwing an assert. After a close research I found out that the problem is de std::vector 's destructor. Furthermore, the problem is when the destructor of the allocator deallocates my memory. Don't know why is this going on, I'd really appreciate any clue on this.
For more information, the Item class doesn't do any weird or exotic operations, it just has some members with the getters and setters. It has a default constructor and a copy constructor, as well as an overloaded assignment operator.
Thanks in advance.
-- modified at 1:16 Sunday 11th November, 2007
Solved
Well, it's solved now. As Nemanja suggested and gleat greatly explained, this was due because I was linking statically to the CRT. Didn't realized, my DLL projects already linked dynamically to the CRT, it was just the WTL app that didn't.
While I had this problem I made some research and it seems it's happening over many places (MySQL and Qt libraries, just to mention two). Hope this thread will be indexed soon in google and may it be able to help other fellows.
Thanks given to Mark, Gleat, Nemanja and David. You saved my life (literally as I was thinking on re-doing all this stuff in C# --don't tell anybody ).
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
Why is a destructor being called where you indicated?
Is there an object at the wrong scope?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, the scope of items is the Foo function, it is destroyed after the function finalizes.
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
Is there something in Items (like a pointer) that gets shallow copied then
deleted twice?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Nope. It is something like:
class __declspec(dllexport) Items
{
public:
Items();
Items(const Items& items);
virtual ~Items();
int GetId() const;
void SetId(int id);
private:
int _id;
};
typedef std::vector<Item> Items;
Mmm... well, the only thing is that I use ATL::CString. And many Item-derived classes uses AT::CString members. Could that be?
Thanks.
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
Yes, that could be the problem. STL and DLLs aren't really the best of friends! You need to be careful about the CRT you use. The problem has to do with the Win32 heap that's used by the CRT that you link to from the DLL and EXE. Whenever we use the the C++ new and delete operators, ultimately, they just translate into calls to HeapAlloc and HeapFree both of which require a handle to the heap. The CRT creates and maintains this handle as a global variable some place. If the DLL and the EXE are using different CRTs and you new up some memory from the former and try to delete it from the latter or vice versa, then the Heap * APIs respond with a big fat undefined behavior i.e., if you did this for instance:
- Allocate a
char buffer from the EXE using new
- Pass a pointer to that buffer to a DLL routine
- Delete the memory from the DLL routine via
delete
you would typically see what your seeing. This happens because we are allocating memory from one heap and freeing it from another!
In your case I guess the CString objects new up some memory from the Item class's constructor from the EXE which then gets deleted from the virtual destructor defined in the DLL. As Nemanja Trifunovic has pointed out, you might want to dynamically link to the multi-threaded CRT from both the DLL and the EXE. That might solve the problem. A nasty kind of issue to run into though!
|
|
|
|
|
I see. Yes, indeed CString allocates and de-allocates by using new . So does CSimpleArray and std::vector .
I did some research and I found several questions regarding this issue, at least with some MySQL and Qt libraries. This particular assert seems to be a feature that was introduced with VC8: the compiler uses an internal (virtual perhaps?) heap in order to locate possible flaws of one application. That's why it only happens when launching the app from within VC, whereas it runs with no problemo if launched outside VC.
In five years this is the first time that it happens to me. I feel like a rookie . Anyway, it's solved now. Thanks for your help and the explanation.
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
Fernando A. Gomez F. wrote: Both reside within a DLL. Then, from my exe,
Uh, oh! Are you maybe statically linking the CRT?
|
|
|
|