|
Thanks for the advice.
You mean I should using pointers ? I am a programer from Fortran/Java. I have not got used to pointers. Give me detailed or further advice please.
I particularly use six big arrays a[8200],b[8200],c[8200]
Ideally I need to expand my program to a[16400],b[16400],c[16400]. However, I tried these expansion, the program stopped to work. message says: memery problem.
-- modified at 15:27 Monday 24th April, 2006
|
|
|
|
|
using pointers will not fix all your problems but will help you deal with memory problems.
normally, when allocating memory on the heap ( with new ) you will have a immediate result telling you that the allocation worked or not.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Thanks
I need to expand the size of the arrays. How can I do this ?
Ideally I should also use "double" instead of "float" to improve the accuracy of the
calculations. However, I tried I can not do it now due to the memery limitations.
Please help me to get a way to deal with this problem
Thanks
|
|
|
|
|
|
i agree, Use heap instead of stack. If you ar ein C++ now, then you won't be really be able to avoid pointers and dynamic allocation.
|
|
|
|
|
would you please give me some code example segments for using GlobalAlloc()?
Thanks
|
|
|
|
|
mrby123 wrote: would you please give me some code example segments for using GlobalAlloc()?
There's no reason to use GlocalAlloc() , unless you are maintaining old 16-bit code. Use new and delete instead.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb
|
|
|
|
|
I would strongly sugest you not to use GlobalAlloc(). Why limit your code to Windows if you don't really need to? Standard ANSI should be fine for your problem. I assume, of course, that you are programming for Win32. If this is not the case then my comments don't apply. But if you avoid GlobalAlloc() you can easilly port to LINUX, which is very similar to Win32 but where GlobalAlloc() doesn't exist.
The first thing you need to do is estimate how much memory you are demanding from the system. From your figures I assume the largest is 3 arrays of doubles with 16400 entries each. Since each double is 8 bytes on most platforms, this results in a total of 393600 bytes of RAM. That's peanuts by today's standards... I assume your computer is equipped with 16MB of RAM or more, so memory outage is surelly not the problem. I have 512 MB, and I once needed 1.3 GB for a simulation, and the virtual memory manager tolerated it just fine. So I'm sure memory outage is not a problem.
There are no real limits on array sizes except those you declare on the stack. Stack is the only real limited resource. It is the space where you declare local variables, and the space the compiler uses to remember where to return to after a function call and where it places the parameters that you pass to functions.
If you are not used to pointers then I would suggest you use a little class to wrap the pointer code. I will paste an example below. This will hide the complexities of allocation and will tolerate bad pointer coding practices.
template <class t="">
class Array {
protected:
T* itsBuffer;
long itsBufferCount;
void Crash(void) { char* p=0; *p=0; }
public:
Array(long count) {
itsBuffer=new T[count];
itsBufferCount=count;
if (!itsBuffer) Crash();
}
Array(Array<t>& arr); // dummy
~Array() {
if (itsBuffer) delete [] itsBuffer;
}
long GetCount(void) {
return itsBufferCount;
}
T* Get(long index_0_based=0) {
if (index_0_based<0) Crash();
if (index_0_based>=itsBufferCount) Crash();
return itsBuffer+index_0_based;
}
T& operator[](long index_0_based) {
return *Get(index_0_based);
}
};
long main_2(Array<double> arr) {
return arr.GetCount();
}
long main_3(Array<double>& arr) {
return arr.GetCount();
}
void main(void) {
long i;
long arr_size=16400;
Array<double> arr_a(arr_size);
Array<double> arr_b(arr_size);
Array<double> arr_c(arr_size);
#ifdef _DEBUG
// slow protected implementation to detect software errors
Array<double>& a=arr_a;
Array<double>& b=arr_b;
Array<double>& c=arr_c;
#else
// fast unprotected implementation for release builds
double* a=arr_a.Get();
double* b=arr_b.Get();
double* c=arr_c.Get();
#endif
for(i=0; i<arr_size; i++)="" {
="" a[i]="100000+i;
" b[i]="200000+i;
" c[i]="300000+i;
" }
="" call="" functions
="" main_2(arr_a);
="" main_3(arr_a);
="" check="" protection
="" a[-1]="316.7;
}
The" example="" above="" shows="" you="" how="" to="" declare="" arrays="" of="" doubles.="" the="" code="" is="" reusable,="" so="" could="" as="" easilly="" integers,="" classes,="" or="" structs.="" class="" protected="" against="" "java="" coding="" style",="" meaning="" that="" don't="" have="" worry="" about="" forgetting="" delete="" array.="" destructor="" will="" do="" for="" you.
the="" protection="" fairly="" simple="" in="" this="" example.="" array="" simply="" crash="" if="" something="" gets="" out="" hand.="" although="" a="" real="" application="" would="" probably="" catch="" these="" exceptions,="" i="" just="" software="" keep="" simple.="" however,="" already="" can="" track="" errors="" other="" causes,="" like="" bad="" addresses.
for="" _debug="" define="" cause="" "a",="" "b",="" and="" "c"="" identifiers="" be="" array<double=""> objects, referencing their respective original arrays. Thus, accessing them with "a[i]", "b[i]", or "c[i]" will invoke the operator[] of class Array. This will be slow, since for every access a function will be called, the parameter will be checked, and if all goes well then the reference to the array entry is finally returned to be used.
For example, the last line has an invalid access to array index -1. If you run this code then this line will cause a crash. If you activate the display of the call stack in the VC debugger you can see where the fault was detected, and climb up the stack to find out where the cause is. For the a[-1] the 4th entry of the call stack will show you the error location. This is a very usefull way of tracking down bugs burried inside complex code.
When you build the release version then the _DEBUG define will not be defined, as, as a result, the "a", "b", and "c" will be compiled as pointers to the buffer of doubles for fast direct access. Setting up "a", "b", and "c" by calling Get() is not the most efficient way to set up the pointers, but typically the impact is not relevant and the code is clean and readable. As a downside no checking is performed, and the a[-1] may not be detected. So build the release after the debug has shown that there are no more bugs. If performance is not an issue, then use only the operator[] version.
I tested the code above with array sizes of 10,000,000 (yes, 10 million!!) and it works just fine. Granted my system has 512 MB, but the theoretical 240,000,000 bytes (10,000,000 entries * 8 bytes per entry * 3 arrays) was confirmed by taking a look at the task manager pausing at a breakpoint after array creation (235 MB in total, with the data taking about 228 MB).
Another thing to note: in JAVA references are implicit, but in C/C++ they are not. So, if you called main_2 then a copy of the array would be made. This would require more memory, and a copy of the contents to be created. In main_3 this does not happen, since the array is passed as a reference. In C/C++ don't forget the & when passing a reference.
To protect the code against such mistakes I added the copy constructor to the Array but with no implementation (// dummy line). If I didn't add the dummy copy contructor line then the compiler would feel free to make a flat copy, and that would be a bad copy for the object (which is not flat). I wouldn't notice it except that the code would use up more memory than necessary and eventually crashed (if I got lucky). No warning or error is issued during compilation stage. This is a very bad flaw in C++ design.
Since I don't want to make copies (in this example) I want a way to protect myself against my own mistakes (of forgetting the & in function prototypes). So I state that I have my own copy constructor (with the dummy line), and so the compiler is not allowed to use its own copy contructor, and, instead, will have to use mine.
But since I don't provide an implementation for the copy constructor then the compiler will issue a link error stating that my copy contructor is unresolved. When I see this error I know I have made a mistake somewhere and after finding the bad line (invocation of main_2) I correct it and the error disappears (everything will be ok, I don't provide a copy contructor implementation but my code doesn't need one, so the compiler has no problem with the missing copy constructor).
I hope this helps.
Rilhas
|
|
|
|
|
Using new and delete [] or GlobalAlloc will work but it is operating at an unnecessarily low level. Standard C++ has a container called a vector which will suit your needs and has many advantages including the fact that it automatically frees memory saving you from the burden of remembering to call delete [] .
To use it first include its header file like this:
#include <vector>
Now you can whip up a vector of float s like this:
std::vector<float> YourVectorOfFloats
Now you can use this as before but you don't need to call delete [] and it has lots of member functions that you can use.
For example, you want to expand it? No worries:
YourVectorOfFloats.resize(400);
Unless he has highly specialized needs there is no good reason for a C++ programmer to use new and delete [] or Win32 APIs such as GlobalAlloc to make dynamic arrays - In general it is a mistake to do so.
Steve
|
|
|
|
|
Thanks Steve, I will implement it.
|
|
|
|
|
I have a very large VC++ (Visual Studio 2003) Win32 console application who's memory grows at a steady rate over time. I've tried:
* DevPartner BoundsChecker - System runs out of memory when application is running from BoundsChecker (we use a lot of memory and have lots of allocations)
* umdh - This free tool from Microsoft can't handle the number of allocatino we do, so we can get one, maybe two, tests run before this tool stops giving data.
* Purify - This tool worked to some degree, but also had limitations on how many test iterations it would handle.
* HeapAgent - Just downloaded it.
Any suggestions on tools that can handle a really big application?
Barry Etter
|
|
|
|
|
try to scope down your problem, if there is some point in your program that you can actually "stop" doing something ( by putting some code in comment, for example ) do it, see if it helps those tools.
try to implement some unit tests to see if those can be used to find the memory leaks.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Start with this:
http://www.codeproject.com/tools/leakfinder.asp[^]
Customize to fit your needs. As Max said, you'll want to cut down the scope to where it is manageable. A good way to do this is to identify operations that *shouldn't* leave memory allocated, and wrap them in checks. Starting with the larger operations and narrowing scope should let you zone in on the problem fairly quickly.
Oh, and i should also mention this tool:
http://www.softwareverify.com/memoryValidator/index.html[^]
I've found it to be a good deal faster than BoundsChecker, and it also has the ability to start and stop data collection, which can be quite nice when you want to ignore initial allocations.
|
|
|
|
|
One thing I've done with BoundsChecker in similar situations is to limit the source files that were instrumented.
For what it's worth, I've tried HeapAgent in the past and hated it. Your mileage may vary.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Could you tell me : How to get my IP address ???
What's happened if my computer don't connect to the Internet or LAN, or i don't install IIS ???
And:
If i connect over LAN,
how to get other Computer name if i know its IP
and how to get other computer's IP if i know its name ?
|
|
|
|
|
Surivevoli wrote: How to get my IP address ???
On windows: Open the dos command prompt and type: ipconfig
Then type enter, and your ip address will come up.
Surivevoli wrote: And:
If i connect over LAN,
how to get other Computer name if i know its IP
and how to get other computer's IP if i know its name ?
What OS are you running?
|
|
|
|
|
oh,my God!
I want get it from C/MFC code !!!
could you tell me ???
|
|
|
|
|
|
You've made my day...
Nibu thomas
Software Developer
|
|
|
|
|
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb
|
|
|
|
|
I have created the file open/save dialog box using CFile dialog.
I selected the lengthy file path which is more than 512 bytes ( Jsapanese file path)
the return value from the doModal() is IDOK.
if I try to get the file name using the GetPathName(), it returns invalid truncated string.
Please let me know any way to get whether the file path returned is truncated or how to get the correct full path.
even the GetLastError() function return zero at tthis scenario.
Thanks in advance
sandeep
|
|
|
|
|
What is the size fo the buffer that you passed?
There is a member variable in the CFileDialog class called m_ofn which is an OPENFILENAME structure.
Get hold of the lpstrFile and pass a TCHAR buffer that is enough to hold the file name.
Nibu thomas
Software Developer
|
|
|
|
|
Yes I tried that by passing the maximum buffer, but still it return only truncated string. I looked into the implementation of the GetPathName() function and came to know that, it is returning only MAX_PATH. Current selected path is beyond the MAX_PATH.
|
|
|
|
|
An update to my analyses is, it can return the file path upto 260 characters ( not bytes). If the selected path is beyond the 260 characters, it truncates.
This scenario is possible when the folder path it self will be almost equal to 260 characters. Now if we select that folder from save as dialog to save some file, it truncates the path.
Please le mt know whether I can change this to return me the full path.
Thanks in advance
Sandeep
|
|
|
|
|
UNICODE chars are two bytes long.
So make sure you supply the required buffer for this purpose. If you are using MAX_PATH then use (MAX_PATH*2) .
Nibu thomas
Software Developer
|
|
|
|
|