Hello,
I am working on the code for a while but I could hardly make this better but some changes manually in the code which is very inefficient and error prone when I need to use more input and output for testing and verify the result. I am looking for some experts suggestions to make this code better. My programming experience is new but I am eager to improve this for writing a better code. Any suggestions and comments as well as advice will be highly appreciated
What conserns the memory...
Replace each memory allocation like float *predCol = (float *)malloc(sizeof(float)*rdata.frames);
with float *predCol = newfloat[rdata.frames];
new operator of this type requires number of elements in brackets, not number of bytes.
Take into account that operator new not always returns NULL in case of failure. Verstion of new from standard C++ library throws bad_alloc exception.
But this is not giving me result as expected but some negative numbers which can not be true since I am calculting Euclidean distance between the elementents in each row in the two dmension two data file.
I will appreciate any help and advice in this regard.
I am having memory allocation for a while. I attach a part of my attempt for your kind review and advice. I have error as
error:
malloc: *** error for object 0xbffff370: Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
So far, I have learned memory deallocation is essential part. But in my task (even this is not completed and not doing what is expected yet,) the programs run without giving me error when I do not use delete [] localdist. But I have a recursion computation and this is quite large ( more than 3000 float computation). Later at one stage, I noticed I had to force quit and reboot my MacBook since I am using MacOS. Please advice me if the memory allocation is reasonable here.
Thank you for your attention and response. (Hope I am able to post the message correctly this time.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
float *localdist[reference.n];
try{
for (int i = 0; i < reference.n; i++){
localdist[i] = new (nothrow) float(ByteSwapFloat(reference.data[i]));
}
}
catch (bad_alloc){
cerr << "error in memory allocation "<<endl;
exit(1);
}
delete [] local dist;
delete [] reference.data;
Your code doesn't fully illustrate your intention. At first glance it appears you are trying to create an array of pointers to floats (essentially a Matrix/2D Array). But I think you've trying to create a dynamic array correct?
Dynamic Array code:
1 2 3 4 5 6 7 8
float *localdist = newfloat[reference.n];
for (int i = 0; i < reference.n; ++i) {
localdist[i] = ByteSwapFloat(reference.data[i]);
}
delete [] localdist;
// Why are you deleting reference.data here? It should be deleted in destructor of object
I have another question and some hints will be very helpful.
I have a prototype asspeech_form readmfc(char *filename)
I need to pass a list of files in two phases in the mentioned prototype: in one phase filename will be change and there will be more files(i.e., now this is 20 files) and in the second phase the filename is same which will be used for some computations by the files listed in the first phase: that is
argv[1] - argv[2] where argv[1] will be the diskstorage files which will be changed by some file names for some files but argv[2] will be the input argument(cin) and this will remain the same for one test and this continues where argv[2] will be changed while there are more tests (and argv[2] will be changed as input when the test changes but argv[1] will be changed in each test several times i.e., file1 - argv[2], file2- argv[2] ....file20-argv[2]). I was trying this by putting the file names manually typing but it seems very inefficient poor work and I am asked to make this automatic so the program shows the output when one can give speech input on the microphone. because this is an speech related application If the explanation is not clear and confusing; perhaps I can post my effort for your kind suggestions for making this good.
That prototype doesn't support passing in a list. Only a string (char *).
e.g readmfc("my file");
Again, you need to simplify how you are asking your question down to specific points. It's very difficult to comprehend your intention.
From what I can extract, your asking how you can auto-load a list of files? You want to put them into a directory, or give them a unique extension. From here you can use Filesystem API's to scan for these files in the target location and load them dynamically.
Thank you very much for the help, Zaita. I am going through the suggested site. I was thinking to use something in the STL library to read all the files contents one file at a time in a directory not just listing the filename. My present program just gives me the list of the file names in a directory( including some . .. syntax). I am using MacOS Leopard 10.5.5 but the result of the task has to be platform independent.
I have some confusion in memory allocation and deallocation. I do not understand when to deallocate memory because as a rule of thumb when I try to deallocate memory, I receive error message. I do not know if my program is taking my memory away due to my present lack of understanding about this memory management. Is there any away to know how the memory is missued or leaking or causing some problems on the diskspace. I do not understand this at the moment because my new Macbook has 2GH memory and more than 100 G harddrive space available right now; so this is fast now. But continuous memory leaking in my program perhaps harmful since I am using many speech files which takes huge space. How the damage or memory loss can be notified earlier than last moment. I understand that there is some problems in my elementary but improving programming skills but how can I learn more and deppt about dynamic memory management in C++ data structure problems for an elegant program?
Well, theoretically memory leaks will "go away" if you restart your computer. Although, obviously it's better to never have them happen in the first place XD. As for notifying of memory leaks...well, you can't...the best you can do it proofread your own code or switch over to smart pointers (Boost is good here)/automatically managed containers (std::vector, etc).
You only need to de-allocate what you allocate manually. So unless your using the new keyword there is no need to de-allocate. However, every variable you create has a useful lifetime. You de-allocate the memory once it's usefulness is complete. I typically allocate variables in a constructor, and de-allocate them in the destructor.
@firedraco: Actually, there are many profiling tools that will notify you of memory leaks. I use a professional tool called AQTime, but unfortunately it has a hefty price tag. There are some free tools out there, but I have not tried any.