|
|
Ehi... I make a little research on internet....and I found the opposite of what you sad:
Static variables (as int i[100];) goes on the stack.
Dynamic variables (as int*p=new int[100] ) is allocated in the heap (of course: p is in the stack and *p is in the heap)
Is the heap and the stack the same thing?
Russell
|
|
|
|
|
no, stack can be overflowed too but, this is very rarely. You should never allocate big data in stack. The stack size on my mashine is 1 mB, and I was not right when sad that code will not compile when variable is greater then stack size. It will but it will throw ecxeption. You should allocate big data in heap because the only limit of memory is 4 Gb, in Windows memory can be projected on hard disk. The chance to end out of memory, allocating in the heap is very small. Allocation in heap is longer because this is a process of looking for free space in memory while allocating in stack is very fast. So the choice is for you.
|
|
|
|
|
progDes wrote: The stack size on my mashine is 1 mB
On Windows, every thread running on your machine can have a different stack size, and it can be larger than
1 MB Executables also have a default stack size for new threads created by the process.
Thread Stack Size[^]
Cheers!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Yes,
int a[100]; going in stack
int *p = new int[100]; going in heap
but you dont have to check stack overflow because stack overflow is already very critical situation, the program can't work, except the exception system when you can catch this situation (i dont exactly know, is there is ability to catch stack overflow). But if you allocating much data in heap you should check if "new" return NULL pointer. In this case you can stop the program or try to free some memory in heap and try to allocate memory again.
|
|
|
|
|
Now it is perfectly clear.
Thank you
Russell
|
|
|
|
|
I should have been more clear with the term "stack variable"...
I should have separated static and automatic variables:
int a[10000];
void SomeFunc()
{
...
}
void SomeFunc()
{
static int a[10000];
...
}
void SomeFunc()
{
int a[10000];
...
}
Objects created with "new" are generally on the runtime heap (unless you're
using a custom new operator and allocating memory some other way).
The stack is created at runtime when creating threads. The default size of the stack
for the primary thread of an executable is set by the linker, so I can't imagine
the compiler can tell you you've created too big of a stack variable. At runtime, however,
there will be a fault if you use too much stack space (stack overflow).
The stack is good for small, temporary objects. Allocating room on the stack is much faster
than allocating memory on the heap. Large objects should be created on the heap
regardless of their longevity. How big "small" and "large" objects are varies, depending
on stack size, nested function call depth, how many other objects are on the stack, etc.
Usually it's recommended that objects larger than a few KB should be dynamically allocated
on the heap, but again, that's arbitrary, and depends on the design of the application and the
system it's running on.
Hopefully a little more info,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thank you ... It's mere then clear now.
This now remember me a question that is posted here days ago:
Why is preferred the way 2 to the (simpler) way 1?
way 1:
CDialog dlg;
way 2
CDialog *p;
p=new CDialog ;
This is giving to him another valid reason of this choose:
to let the stack be more free!
sizeof(CDialog) vs sizeof(CDialog*)
Russell
|
|
|
|
|
Hello all,
I have an iPhone and made a stupid mistake.
1) I got an iPhone and then did a 'sync' which put all my songs etc in the iTunes Music Library onto my iPhone
2) Then, thinking "Oh well, the songs are all on my iPhone now...to save some disk space I can erase my iTunes Music Library folders and ALL the files within!"
Duhhhh....
3) Having done that and regretting it, now iTunes won't let me put my songs back on my computer because it says, "The music library on this iPhone came from another computer."
Since I deleted ALL iTunes music files from my computer -- including the .xml etc control files, I can see where it might say this. However, it happens to not be the case.
Just to be clear...I do know I can copy iTunes Music Store-purchased stuff back to my computer, and that's great.
However, being the C++ and Windows programmer that I am, I am looking for a code hack to use the Windows API etc. to open the iPhone from my own code and copy files off of it. It's not immediately obvious if any of the standard Drive APIs will work, since it looks to the computer to be somewhat like a USB Mass Storage Device, although only somewhat....it has no drive letter when I plug the sync cable in.
However the "Scanner and Camera Wizard" from Windows always pops up when I plug the sync cable in and the iPhone shows up in My Computer with no drive letter, just as if it was a Digital Camera only...so I am wondering if there is anyway to crack the iPhone and get the songs off of it...
I believe there is a solution somewhere to this problem, since Tansee iPhone Transfer Software has obviously implemented some sort of working solution. However, it's not obvious that the aforementioned package is written in C++ and that's my favorite language to use...
Any suggestions would be greatly appreciated.
Sincerely Yours,
Brian Hart
Department of Physics and Astronomy
University of California, Irvine
|
|
|
|
|
Can anybody point me to any documentation / sample code on this???
My MSDN library does not know what CTabStrip is too!
Thanks a million
Vaclav
|
|
|
|
|
The MFC and CRT source code have never heard of it either.
What the heck is a CTabStrip?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It is the control containing just the tabs.
(Microsoft Forms 2.0 TabStrip.lnk in Registsered Active X controls in gallery _ VC6.0) )
I am hacking " visual framework "
( http://www.codeproject.com/dialog/visualfx.asp)
and I need to add a spinner control to move the tabs into focus when there are too many tabs in the tab client area.
I am having great time hacking this code and need to speed thigs up a little.
What I was going to do is make invisible tab control to gain acces to the spinner. But it is getting to complicated.
Vaclav
|
|
|
|
|
OK. With your help I managed to finish my project. One last thing to do.
How to build an exe file from my mfc project so it can run on any windows base computer?
|
|
|
|
|
|
|
Oh yeah, good One Bookmarked.
Thanks,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
In my app, i need large float data, so i create a class for it.
but existing functions, such as sin(.), log(.) ..., are all use double, so I have to create similar functions to suit my large float numbers.
How to create my own sin(.), log(.) etc?
|
|
|
|
|
includeh10 wrote: so I have to create similar functions to suit my large float numbers.
http://www.netlib.org/cephes/[^] already exists.
But if you really want to write your own, pull the sources for GNU compilers standard math libraries, and extend them to long double.
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
|
Hi people,
I m facing a problem with sockets.
My application connects to a device that has an IP and a port. If i switch on the device first and then my application i m able to establish consistent communication between them; however if i run the application first and then turn on the device it doesnt connect then.
P.S: I m using a Connect-button on the front-end to connect
Please help.
Button Connect function is as follows:
m_sConnectSocket1.Create();
m_sConnectSocket1.Connect(m_strIP1,m_iPort1);
|
|
|
|
|
You should check error codes...
m_sConnectSocket1.Create();
if (!m_sConnectSocket1.Connect(m_strIP1,m_iPort1))
{
int sockerrcode = m_sConnectSocket1.GetLastError();
if (sockerrcode == WSAEWOULDBLOCK)
{
}
else
{
}
}
...
void MySocketClass::OnConnect(int nErrorCode)
{
if (nErrorCode != 0)
{
return;
}
...
}
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I get error code 10035-WSAEWOULDBLOCK, that is when it connects but if i run the apllication first and then the device i get errorcode 10022 WSAEINVAL
If OnConnect() event is triggered shouldnt the error code be 10056?? Or, conversely, what error code(s) are/is possible if OnConnect() gets triggered??
By your code it seems a successful connection would produce an err-code of WOULDBLOCK.
Actually what i m doing in my new code is i keep trying to connect till the error code is 10056 or a timeout of 2 seconds, whichever is first. This i guess could be a wrong procedure. But it always works if i start the device first.
|
|
|
|
|
The possible error codes for Connect() are listed in the documentation for
CAsyncSocket::Connect() and also can be found in the Winsock docs:
connect() (Windows Sockets 2)[^]
Any time you use asynchronous sockets, an error of WSAEWOULDBLOCK
means the operation has NOT completed yet so no error info is available
at that time. To find out if the operation succeeded or not, you need
to get the results in whatever asynchronous completion method you're using.
In your case, that's in an overridden OnConnect() method.
yashveer wrote: Actually what i m doing in my new code is i keep trying to connect till the error code is 10056
That's not a proper way to check for a successful connection. Either there's an error or there's not.
You can find that out using the example code (or similar) that I posted previously.
And please use "WSAEISCONN" instead of "10056" when posting error codes.
I don't have them all memorized so I have to look them up. They'll make your code more
readable as well
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have a problem. I am trying to catch some exception and I still haven't succeded. I tried with very simple example but still isn't working. Cakb someon help. Here is the code;
try
{
int a=5, b=0, c;
c = a/b;
}
catch (CException* e)
{
MessageBox("Division by zero exception!");
}
My program ends with error without executing the code in catch block.?????
|
|
|
|
|
try
{
int a=5, b=0, c;
c = a/b;
}
catch (...)
{
MessageBox("Division by zero exception!");
}
|
|
|
|