|
Is there an easy way to check available memory before / after using *p = new int[ ] ? I tried CMemoryState, without success.
|
|
|
|
|
I am not sure if there is a single function call that can give an answer to your question. This would have been a simple question before the advent of virtual memory, but that is no longer the case. If you want to know the physical memory available then you may have to experitment, but that limits you to the machine you are on. Write your programm in such a way that it can compensate for the lack of phisycal memory, or can inform the user if it can not.
I am just saying that we no longer know the amount of memery available and you have to make sure that if it is not, your progam catches that problem before it crashes some bodies machine.
INTP
Every thing is relative...
|
|
|
|
|
Thanks John, it´s clear that new returns 0 if allocation failed. I´m just looking for a way of checking available memory before and after such operations. Due to the dynamic memory model in windows it may be impossible to get exact required data. Purpose is not implemeting it in a commercial application but memory usage demonstration to students. In DOS times ( a century ago ) we had a similar function.
|
|
|
|
|
Have a look at the GlobalMemoryStatus() function.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I doubt it, since freed memory does not automatically go right back to the pool of available memory. Windows' memory manager is a very complex beast.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
pls give the code for getting Ip address of the computer using VC++
|
|
|
|
|
There are lots of examples here on CP about this topic, but here is some quick code:
WSADATA data;<br />
WSAStartup(MAKEWORD(2,2),&data);<br />
char szHostname[256];<br />
if (gethostname(szHostname, sizeof(szHostname)))<br />
{<br />
WSAGetLastError());<br />
}<br />
HOSTENT* pHostEnt = gethostbyname(szHostname);<br />
if (pHostEnt == NULL)<br />
{<br />
WSAGetLastError());<br />
}<br />
if (pHostEnt->h_length != 4)<br />
{<br />
}<br />
int nAdapter = 0;<br />
in_addr address;<br />
CopyMemory(&address.S_un.S_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length);<br />
dwIP = MAKEIPADDRESS(address.S_un.S_un_b.s_b4, address.S_un.S_un_b.s_b3, address.S_un.S_un_b.s_b2, address.S_un.S_un_b.s_b1);
Now the address is in dwIP as a DWORD also.
Also available through the:
address.S_un.S_un_b.s_b1<br />
address.S_un.S_un_b.s_b2<br />
address.S_un.S_un_b.s_b3<br />
address.S_un.S_un_b.s_b4
as individual unsigned bytes.
this is this.
|
|
|
|
|
go thru this one.
char szHostName[128];
if( gethostname(szHostName, 128) == 0 )
{
// Get host adresses
struct hostent * pHost;
int i;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
CString str;
int j;
for( j = 0; j < pHost->h_length; j++ )
{
CString addr;
if( j > 0 )
str += ".";
addr.Format("%u", (unsigned int)((unsigned
char*)pHost->h_addr_list[i])[j]);
str += addr;
}
// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
}
}
viveks
|
|
|
|
|
mailsafe wrote: pls give the code for getting Ip address of the computer using VC++
Just a ?, Why you need IP address of a computer!
"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
|
|
|
|
|
Using App wizard i chose to create a MDI application. In such cases when we run the program the MDI will run first. But i want to insert a dialog eg a login dialog such that when we run the project the dialog should be displayed first. And on the button(which is placed in the dialog)-click, the MDI interface should open up. pls give a solution
-- modified at 4:43 Wednesday 1st March, 2006
|
|
|
|
|
So what have you done till now? Where are you doing this?
Nibu thomas
Software Developer
|
|
|
|
|
Hi,
I am trying to use an RTP code (.lib) i downloaded from the net. When I try to use the same code as a Win32 DLL the compiler doesnot identify the ntohl and other such functions in the Winsock2.h header. I can do without the socket calls as I have to use the RTP in a C# code and I can use sockets in C#, but ntohl etc. must be used.
Can some one tell me what could be the problem and how it can be fixed?.
I have tried searching the net and came across managed and unmanaged dll's etc. But I cannot even understand what they mean by it, bcoz I am creating (and using) DLL for the first time.
Thanks
castratroi
|
|
|
|
|
I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO
<br />
<br />
<br />
class ABC<br />
{<br />
public:<br />
virtual void FOO(BAR& foo);<br />
};<br />
<br />
<br />
class ABC<br />
{<br />
public:<br />
virtual void FOO(BAR& foo) = 0;<br />
};<br />
My question is this,
What does the "= 0" indicate?
Why is it needed?
When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!
|
|
|
|
|
I think I have found the answer,
The "= 0" is to indicate to the c(++) compiler that this function is purely virtual and there is no implementation in the base class, which therefore cannot be instantiated directly (which is what I intended). Is this correct?
When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!
|
|
|
|
|
Exact but it doesn't correspond to your error (because it was the other way: it worked if you add this '=0' and not if you retrieve it).
|
|
|
|
|
First, it is always usefull to add the exact error message when you got one. We cannot guess what the error message is (and in general they are really helpfull).
Now, to answer your question, the =0 means that this is a pure virtual function, that is a function that NEED to be redefined in the child classes. If you don't redefine them in child classes and if you try to instantiate your object, you will get an erro telling you that it cannot instantiate the class due to pure vitual function.
But in your example, I don't see why you get an unresolved external error if you don't add this =0. Maybe try to post more code and the exact error message.
|
|
|
|
|
lastgen wrote: What does the "= 0" indicate?
This means FOO is a pure virtual function, implementation for which will be provided in a derived class of the class ABC .
ABC cannot be instantiated directly. But derived classes for ABC which provide an implementation for FOO can be instantiated.
Nibu thomas
Software Developer
|
|
|
|
|
I am making an assumption that you did not define the function in the original class and that the second version worked. If you had defined the function (or method) in the first class you would not have had an error. Your could have simply declared it as this virtual void FOO (BAR& foo) {}; and there would not have been an error, you would have just had a function that did not do anything.
The second version declares a pure virtual function, which needs to be implemented by a derived class. That is you inherited the interface from the defined base class (ABC) and must provide a function for the inherited interface. What that means is in the base class you have said this function must exit in the derived class, because it does not actually exist in the base class.
Let’s try this,
If you declare a function in a class the compiler will complain if that function does not exist. Therefore you must define the function so any one using that class will have that function to call. If you make the function as a pure virtual function, then the class is designed as a base interface class that is not intended to be used by its self, but is intended to be used as a base for derived classes.
I know I sound like some text book, but I have been doing this a long time. Look up the words “pure virtual” and “virtual base class” along with “C++” on the Web; you should come up with a few decryptions. Amongst those may be instrument and car, all cars have things in common like press the accelerator, press the brake, and ect… . The base class specifies all things that classes derived from it have in common and forces all derived classes to implement those behaviors.
Think of the “= 0” as a requirement that you impose on all users of the class that you have defined.
I hope that helps,
John R. Shaw
INTP
Every thing is relative...
|
|
|
|
|
What is the difference between unions and structures .
Yaaqub
|
|
|
|
|
A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. But that's not the case with structures. In structures fields don't overlay each other and any field can be used any time.
The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members.
Nibu thomas
Software Developer
|
|
|
|
|
Each field of a union starts at the same location in memory, whereas each field in a structure gets separate memory locations.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Just to explain so that you can get a clear view of what the difference is:
suppose we have a structure
struct abc
{
int a;
char b;
}a1;
When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes and a char value of 1 byte .
The total size of the structure would thus be 3
On the other hand if you use unions and declare it as
union def{
int a;
char ch;
}u1;
Then the size of operator will show 2. As the memory of variables in a union is shared and the size is of the highest occupant in the union.
Vision is Always important and so is your ATTITUDE.
Wishes.
Anshuman Dandekar
|
|
|
|
|
AnShUmAn_VCPP wrote: When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes...
I think you mean 4 bytes.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
yes . The value that will be shown is compiler dependent.
If you work on turbo c you will get 2
if you work on visula studio you will get the size of an int in this case
Vision is Always important and so is your ATTITUDE.
Wishes.
Anshuman Dandekar
|
|
|
|
|
AnShUmAn_VCPP wrote: If you work on turbo c you will get 2
But this is a Visual C++ forum...
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|