|
mehrdadov wrote: I want to get my Computer IP.How can I do it?
gethostbyaddr
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Hi,
I got 3 files, main.cpp, aaa.h, aaa.cpp
+ aaa.h is blank
+ in aaa.cpp
<br />
#include "aaa.h"<br />
static float asd;<br />
+ in main.cpp
<br />
#include "aaa.h"<br />
int main(){<br />
asd=1;<br />
return 0;<br />
}<br />
I do this and compiler said asd is undefined in main.cpp
|
|
|
|
|
|
I change static float asd; to extern float asd; but it still doesn't work, I did something wrong?
|
|
|
|
|
You need to declare the variable normally in one file and in all other files where you need access to it you need to declare it exactly the same, just with the extern keyword in front of it.
In aaa.cpp, declare:
float asd = 0.0f;
In main.cpp, declare:
extern float asd;
Best regards
Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
(doesn't work on NT)
|
|
|
|
|
|
static is the exact opposite of extern .
--
Pictures[^] from my Japan trip.
|
|
|
|
|
Using static as the opposite of extern is deprecated. Anonymous namespaces are now the preferred mechanism for achieving this effect. ie.
namespace
{
int a;
}
I believe this change was made because the extern keyword was overloaded too much (had too many meanings).
Steve
|
|
|
|
|
There's a downside to this though. Eventhough you put the variable into an anonymous namespace, the name will still be around in the exported symboltable, although mangled to ensure uniqueness. At least that's how I understood an article (I think it was one of Sutter's). One can only hope that static library archivers and DLL linkers will remove the anonymous symbol table entries from the final symbol table(s). God knows what happens if there's a name collision.
|
|
|
|
|
I've never had any problems with name collision (using anonymous namespaces) in VC6.
Steve
|
|
|
|
|
I'm a beginner programmer with a question about hashtables.
when I create a hashtable like so:
Hashtable *hashTable = new Hashtable;
Do I need to delete the hastahble in my destructor?
It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use.
But when I try to delete the hashtable, I receive an error message about an illegal delete.
So do I need to delete them somehow to avoid a memory leak?
Thanks,
Jody Blau
|
|
|
|
|
jblau wrote: It was my understanding from the stuff I read that you should always have a DELETE for every NEW that you use.
That's right, if you are programming in C++! But I have a suspicious feeling that you are using C#, or a language that uses the .NET framework. These manages there own memory stack/heap, so deletes are redundant.
I Dream of Absolute Zero
|
|
|
|
|
Hold it right there! Delete is not redundant. Mangaged just means that if you do not clean up after your self, then it will eventualy be clean up for you. Not cleaning up after your self, is a very bad idea.
INTP
Every thing is relative...
|
|
|
|
|
Are you using normal or managed C++?
Are you getting a compiler error? Post your code and the errors.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
"Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"
-- Rico Mariani, CLR perf guy
|
|
|
|
|
Michael Dunn wrote: Are you using normal or managed C++?
I would like to see the abnormal ones
-Prakash
|
|
|
|
|
|
It is true that for every NEW you need to call DELETE. The only reason that you should receive an error, is that it was already deleted. That implies an error in your coding, becuase if the the pointer to the hash-table is a member of your class, then the memory-block should exist until the class object is destoryed.
If there is the slitest chance that the memory pointed-to by the pointer might be be dealocated by another function, then you should set the pointer to NULL after you deallocate it (delete does nothing if the pointer is NULL). The only problem with this, is that it hides the fact that you are trying to delete the same memory-block two or more times.
My recommendation is that you put a trace (or out-put it to a file) statement after every NEW and DELETE associated with the the hash-table. That way you will know when it is allocated and when it is destoryed. If the NEWs and DELETEs do not match up, then you will know that some thing else is going on.
INTP
Every thing is relative...
|
|
|
|
|
I hope u didnt say delete Hashtable
it should be delete hashTable
its kinda not rite to name the variable name same as the datatype with difference in the case, Sometimes it creates hard to understand compiler errors, like this one.
-Prakash
|
|
|
|
|
wow, I am surprised at how many people responded, thanks everyone!
Here is some more information which might allow you guys to set me straight.
I am using Visual Studio.net 2003
The type of project that I opened is a "Windows Form Application (.Net)"
(so I don't know if that means that I am using "Normal" or "Managed" C++ ?)
My code is something like this:
void LoadLists()
{
Hashtable *hashSelectedItems = new Hashtable;
......(code working with the hashtable)
delete hashSelectedItems;
hashSelectedItems = NULL;
}
When I try to build the project, I get this error message: error C3841: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined
I'm thinking that perhaps it is, as one of you mentioned, being automatically managed, but I'm not certain.
It was my impression that if I wanted to use a Managed object I would specifiy so in the declaration, for example, in declaring an array, if I wanted it to be a managed array I would declare it like so: int c __gc[] = new int __gc[12]; but if I didn't want it Managed, I would leave out the __gc part.
************So given all of this, what do I need to know about declaring Hashtables without leaving any memory leaks?
Thanks,
Jody Blau
|
|
|
|
|
I do not work with managed any thing, but I can tell you where you made a mistake. Do not allocate an object and delete an object in the same function, unless you have to. Instead you should just declare it as a stack object and let it clean up after its self.
void LoadLists()
{
Hashtable hashSelectedItems;
......(code working with the hashtable)
}
The above code does not require a delete, becuase no new was called. The 'hashSelectedItems' variable is on the stack and will clean up after its self when the function returns.
INTP
Every thing is relative...
|
|
|
|
|
John R. Shaw wrote: Do not allocate an object and delete an object in the same function, unless you have to
Why? That's a perfectly normal thing to do, especially when the object in question has some runtime-determined characteristic (e.g. a non-trivial constructor). It's appropriate if you don't need the object for the entire life of the function. Also, if the object is large, you tend to get better working set behavior if you allocate it from the heap rather than the stack.
Software Zen: delete this;
|
|
|
|
|
Gary R. Wheeler wrote: Why?
It is like doing this:
char* buffer = new char[32];
when you should be doing this:
char buffer[32];
The allocation in this case is not a normal thing to do.
Another example:
class MyClass
{
int n1,n2;
public:
......
};
void MyFunc1()
{
MyClass* mc = new MyClass;
......
delete mc;
}
void MyFunc2()
{
MyClass mc;
......
}
Gary R. Wheeler wrote: It's appropriate if you don't need the object for the entire life of the function.
Insted of allocating it, you can use scoping. The pratice of using scoping to limit an objects life time is common and is used in the MFC library its self.
void MyFunc()
{
......
{
MyClass mc;
......
}
......
}
When you allocate unnecessarily, you introduce one more factor that can go wrong.
INTP
Every thing is relative...
|
|
|
|
|
|
jblau wrote: illegal delete expression: managed type 'System::Collections::Hashtable' does not have a destructor defined
OK, that shows that you're creating a managed object. Don't delete it, the CLR garbage collector will take care of that for you. The reason you don't need __gc in the declaration is the compiler infers it from the context - Hashtable is a managed object so __gc is the only possibility.
Yes, this is confusing and it's the main reason why the MC++ syntax was replaced with C++/CLI in VC8.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
"That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."
-- Buffy
|
|
|
|
|
Thanks for all of the helpful information.
As I understand it now, the way my project is setup, the hashtable is a managed object, so I can't delete it mannualy, (as I get errors when I try).
So my follow up question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well?
Example:
MyObject *tempObject = new MyObject;
Hashtable *hashObjects = new Hashtable;
hashObjects->Add(key, tempObject);
So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself?
If I do need to delete them, will a call to hashObjects->clear(); delete the objects?
Thanks,
Jody Blau
-- modified at 15:53 Sunday 1st January, 2006
|
|
|
|