|
|
Programm3r wrote: char test[MAX_BUF_SIZE]; // Max amount of character ??.
The maximum amount will be governed by the program's stack size, which defaults to 1MB.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Programm3r wrote: Can it be any bigger ?
The short answer is yes!
Now for the long answer! (Which is still a short answer).
It depends on the amount of memory available at the time. When we where programming for 16-bit processors the limit was 65536 (unless you threw in a compiler specific modifier), but if you where not allocating the buffer from the heap, you would have to increase the size of the program stack to accommodate it.
When working with memory the size can vary depending on many things, my best recommendation is to use the STL ‘vector’ template. The vector template is just a dynamic buffer (or array); meaning that you do not need to know the number of items you are going to store ahead of time. If you do know the number of items (elements) then you can set it to that size, so that it will not have to look for another continuous memory block latter.
If for some reason you need a ridiculous buffer size (like 3GB or more) then you will have to turn to the Windows specific functions dealing with virtual memory. Virtual memory is limited to all available memory, including the amount of space left on your hard drive.
When ever possible use the STL containers and avoid using ‘new’ when you need to store more than one item. This will simplify your code and in the long run save you a lot of head aches.
I will end this by saying that many courses taught today center around managed memory (.Net), which compounds a problem I use to think was just in their heads. That is people failing to realize that if you take something (allocate memory) you have to return it or it will not be available latter when you need it. A ‘vector’ like most STL containers solves the same problem, if you use a ‘vector’ then you do not need to remember to ‘delete’ the memory you allocated using ‘new’, because it will be done automatically when the ‘vector’ goes out of scope.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
John, thank you very much, that helps alot.
The only programmers that are better than C programmers are those who code in 1's and 0's.....
Programm3r My Blog: ^_^
|
|
|
|
|
Programm3r wrote: The only programmers that are better than C programmers are those who code in 1's and 0's
Funny you should mention that because that is where I started! I mean that literally, as the only programming I was ever taught by any one was at the machine level (below assembly). A good C programmer can look at the code and see the assembly code lying just under the surface, but that is still one level above machine code. I will always like C, but I prefer C++ because it allows you to express your ideas in a much fuller way, with less work. If anyone ever tells you that they can do something in C++ that you can not do in C, tell them to go suck a rotten egg. If it can not be done in C then it can not be done period.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
John R. Shaw wrote: tell them to go suck a rotten egg
LMAO ...
The only programmers that are better than C programmers are those who code in 1's and 0's.....
Programm3r My Blog: ^_^
|
|
|
|
|
John R. Shaw wrote: If anyone ever tells you that they can do something in C++ that you can not do in C, tell them to go suck a rotten egg.
Are you sure about this, I can agree with you that one can write any program in C if its written in C++, but aren't there some features in C++ which can't be simulated in C ?
I know that one can simulate polymorphisme in C, its hard, its hard coded for that matter but still doable. But I don't know if everything is possible?
codito ergo sum
|
|
|
|
|
Well I used to do public and private (private being hidden entirely), but I could not see how to do protected. Hard-coding inheritance is not really worth the effort, but is doable.
There are other ways to simulate other features of C++ in C, but they are all a pain.
Think about this: The original front end for C++ converted the C++ code into C and then passed the C files to a C compiler and linker.
I think, Bjarne Stroustrup first proposed using macros for creating templates instead of a separate mechanism. This was something I had done in the passed (in C) and is very doable.
I do not know if the majority of OSs are still written in C, but I suspect they still are.
None of the above really matters though; the point was that any application that is written in C++ can be written in C. It is just harder to do, because nothing is hidden (normally) and you have to be more careful (be more responsible). On the plus side, when you make a function call in C you know it, which cuts down on surprises.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
The maximum size of a character buffer is what your software specifications specify! If your application needs a buffer to handle 10 bytes of data, there is no need to allocate 2KB of memory. The maximum size that you can allocate depends on the type of memory management being used.
[Stack]
In Win32, your stack starts at 1MB, so depending on how your code works and where it is being called, you may be able to allocate much of that on the stack. Grabbing 32KB from the stack is generally nothing unless you are dealing with recursive functions.
[RTL Memory Management]
Allocating from the standard RTL heap imposes other limits, and these limits may be platform-specific. For example on Win9x, you can only allocate a bit less than 256MB using the RTL allocation routines (this is a limitation of HeapAlloc(...) on Win9x).
[Win32 Memory Management]
You can manage larger amounts of memory using things like VirtualAlloc(...) , or using a memory-mapped file. You can also run into address space exaustion when dealing with really large amounts of memory, but this is soemthing the average developer does not experience - at least, not unless they have a serious bug somewhere...
The important thing to ask is how much memory do you need? Some developers will scoff when they see something that they believe to be arbitrary buffer sizes of 32, 64, 128, 256, etc., but these limits are specified by the requirements of the software. If your program needs a Username , and the specifications state that Username shall be limited to 31 characters, a character buffer size of 32 is perfectly fine.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
James R. Twine wrote: The maximum size of a character buffer is what your software specifications specify!
That is one of the most ridiculous statements I have ever read. Beyond that you give a lot of useful information.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Thanks... I think...
Why is that ridiculous? I know that it implies that you should have software specifications that [1] exist and [2] are accurate, but...
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Modern day OSs and hardware give you a lot more leeway than they use to, but the limitation is still determined by the hardware and not by the software specifications. I could specify in the software specification that a terabyte was require for the buffer size, but if the hardware can not provide that then the specification becomes meaningless. Software specifications are limited by the hardware, not the other way around.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
OK - I see your POV.
I was speaking in general terms - there is no need to worry about whither my hardware can handle an allocation of 2GB if I only need 32 bytes at any one time.
To offer a different POV, the software specifications dictate the hardware. If my software uses 100MB of allocated memory, then I need hardware that can handle that. I rarely encounter a software specification that starts with a hardware requirement unless dealing with a fixed/embedded/mobile system. IME, hardware requirements generally change during the development lifetime of a product for a variety of reasons (performance requirements, feature-creep, etc).
If I am ever given software specifications that state that I need to allocate a terabyte of memory(!), I better come up with suitable hardware and an addressing scheme to handle it!
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Thanks, I am glad you understand.
In the world I learned to program in the PC was very limited and every byte counted. At one point I had to write my own virtual memory manager (even with extended memory) to squeeze a little more out of it. When I wrote firmware (Z80) I had to balance the stack size against the number (size) of global variables, so that the stack did not start stepping on the globals. That also related to how much functionality you could code in because that space was limited too. I pushed it to the limits, another 16-bytes or so and the whole thing would probably fall apart. The limitations of the hardware was not something that could be adjusted, I had to work within the limitations that it gave me.
I love modern machines because I have not even seen the wall yet and have been ignoring the fact that I know it is there somewhere.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
On a side note, why use macros when enum s will work just as well? Better in fact. For example try doing this with macros:
void Function1()
{
enum { buffersize = 1024 };
char buffer[buffersize];
}
void Function2()
{
enum { buffersize = 2048 };
char buffer[buffersize];
}
The problem with macros in this context is that they don’t obey C/C++ scoping rules. Using macros to define constants is old fashioned and should be avoided.
Steve
|
|
|
|
|
I want to list all the directories in a folder including the hidden dir's.Which function to be used?
Thanks..
|
|
|
|
|
Use CFileFind .
CFileFind finder;
BOOL bWorking = finder.FindFile(L"*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsHidden())
{
}
}
|
|
|
|
|
How about the FindFirstFile() /FindNextFile() pair?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
I need to display the file open dialog and browse directory dialog at the center of the screen. it's display right now at the top left corner ,can any body help me to display it at the center.
is there any realation with my main application window. Because the mainwindow is not display at center of screen now.
Amit
|
|
|
|
|
amitmistry_petlad wrote: I need to display the file open dialog and browse directory dialog at the center of the screen
Use CWnd::CenterWindow .
|
|
|
|
|
But Master, it's WIN32.
aMIT
|
|
|
|
|
Oh ! I guess, you had asked same question few days back[^]. And I'll answer same thing agains, Look, how CWnd::CenterWindow is implemented.
Code for above function I'm pasing,
void CWnd::CenterWindow(CWnd* pAlternateOwner)
{
ASSERT(::IsWindow(m_hWnd));
DWORD dwStyle = GetStyle();
HWND hWndCenter = pAlternateOwner->GetSafeHwnd();
if (pAlternateOwner == NULL)
{
if (dwStyle & WS_CHILD)
hWndCenter = ::GetParent(m_hWnd);
else
hWndCenter = ::GetWindow(m_hWnd, GW_OWNER);
if (hWndCenter != NULL)
{
HWND hWndTemp =
(HWND)::SendMessage(hWndCenter, WM_QUERYCENTERWND, 0, 0);
if (hWndTemp != NULL)
hWndCenter = hWndTemp;
}
}
CRect rcDlg;
GetWindowRect(&rcDlg);
CRect rcArea;
CRect rcCenter;
HWND hWndParent;
if (!(dwStyle & WS_CHILD))
{
if (hWndCenter != NULL)
{
DWORD dwAlternateStyle = ::GetWindowLong(hWndCenter, GWL_STYLE);
if (!(dwAlternateStyle & WS_VISIBLE) || (dwAlternateStyle & WS_MINIMIZE))
hWndCenter = NULL;
}
MONITORINFO mi;
mi.cbSize = sizeof(mi);
if (hWndCenter == NULL)
{
HWND hwDefault = AfxGetMainWnd()->GetSafeHwnd();
GetMonitorInfo(
MonitorFromWindow(hwDefault, MONITOR_DEFAULTTOPRIMARY), &mi);
rcCenter = mi.rcWork;
rcArea = mi.rcWork;
}
else
{
::GetWindowRect(hWndCenter, &rcCenter);
GetMonitorInfo(
MonitorFromWindow(hWndCenter, MONITOR_DEFAULTTONEAREST), &mi);
rcArea = mi.rcWork;
}
}
else
{
hWndParent = ::GetParent(m_hWnd);
ASSERT(::IsWindow(hWndParent));
::GetClientRect(hWndParent, &rcArea);
ASSERT(::IsWindow(hWndCenter));
::GetClientRect(hWndCenter, &rcCenter);
::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
}
int xLeft = (rcCenter.left + rcCenter.right) / 2 - rcDlg.Width() / 2;
int yTop = (rcCenter.top + rcCenter.bottom) / 2 - rcDlg.Height() / 2;
if (xLeft < rcArea.left)
xLeft = rcArea.left;
else if (xLeft + rcDlg.Width() > rcArea.right)
xLeft = rcArea.right - rcDlg.Width();
if (yTop < rcArea.top)
yTop = rcArea.top;
else if (yTop + rcDlg.Height() > rcArea.bottom)
yTop = rcArea.bottom - rcDlg.Height();
SetWindowPos(NULL, xLeft, yTop, -1, -1,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
|
|
|
|
|
ya i am in proccess of modifing that one . Is it real need to make the main screen in the center for display the system window in center?
|
|
|
|
|
amitmistry_petlad wrote: Is it real need to make the main screen in the center for display the system window in center?
Actually, If you had a look at CWnd::CenterWindow , it has flexiblity to decide the window, which should be considered for centering window in question. By default its desktop window.
John R. Shaw has shown you code snippet, having similar logic, which will center the window.
|
|
|
|
|