steve_bank
Diabetic retinopathy and poor eyesight. Typos ...
Why use dynamic memory allocation?
If you are dealngi with large arrays and declare arrays x[N] it goes on the stack. Stack memory is where local variables and functions operate. You can run out of stack memory with arays.
In the day of 64k memory that was easy to do and swapping both code and data in and out of memory from disk was common.
You can increase the size of an array on the stack int x[N] until the compiler says it is too much.
The example shows how I used OOP. In the 80s I read OOP is ‘the melding of data and the mehods operating on the data’, IOW encapsulation.
On the automated manufacturing systems I designed I had to deal with multiple files. I would pass the file names through the command line parameters main( int argc,char *argv[],char **envp ). T
Using classes ensured the file data could not be inadvertently corrupted by code outside the class. The class contains error checking. I made use of log files. If something did go wrong I’d have a staring point.
As the saying goes 'A gram of prevention is worth a kilogram of cure'.
It would have been impossible using random code. Using structured programming and functions could have worked but it would have been much more complicated and harder to debug. I would debug and test an object and put in a library. Then I’d write the top level code naking use of pretested objects. More work at the start, but a lot less work at the end. Easier maintenance and changes.
You can look at OOP as a form of reductionism.
OOP is more prevalent then you might think, you just don’t see it.
When you click on a folder a window appears. The OS dynamically spawns an object for the graphic window. I don’t know Windows classes. As you drag the window cursor xy is updated as something like window_handle.x widow_handle.y. When the window goes away the object is deallocated.
Same thing in my Scilab tool. When I create a graphics window to plot something I get an object to set parameters.
Stack vs Heap Memory – Difference Between Them
What is a Stack? A stack is a special area of computer's memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime. It is a
www.guru99.com
Memory in C – the stack, the heap, and static
The great thing about C is that it is so intertwined with memory – and by that I mean that the programmer has quite a good understanding of “what goes where”. C has three differen…
craftofcoding.wordpress.com
Note that there is generally a limit on the size of the stack – which can vary with the operating system (for example OSX currently has a default stack size of 8MB). If a program tries to put too much information on the stack, stack overflow will occur. Stack overflow happens when all the memory in the stack has been allocated, and further allocations begin overflowing into other sections of memory. Stack overflow also occurs in situations where recursion is incorrectly used.
Heap memory
The heap is the diametrical opposite of the stack. The heap is a large pool of memory that can be used dynamically – it is also known as the “free store”. This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc), and deallocate (e.g. free) the memory. Failure to free the memory when you are finished with it will result in what is known as a memory leak – memory that is still “being used”, and not available to other processes. Unlike the stack, there are generally no restrictions on the size of the heap (or the variables it creates), other than the physical size of memory in the machine. Variables created on the heap are accessible anywhere in the program.
If you are dealngi with large arrays and declare arrays x[N] it goes on the stack. Stack memory is where local variables and functions operate. You can run out of stack memory with arays.
In the day of 64k memory that was easy to do and swapping both code and data in and out of memory from disk was common.
You can increase the size of an array on the stack int x[N] until the compiler says it is too much.
The example shows how I used OOP. In the 80s I read OOP is ‘the melding of data and the mehods operating on the data’, IOW encapsulation.
On the automated manufacturing systems I designed I had to deal with multiple files. I would pass the file names through the command line parameters main( int argc,char *argv[],char **envp ). T
Using classes ensured the file data could not be inadvertently corrupted by code outside the class. The class contains error checking. I made use of log files. If something did go wrong I’d have a staring point.
As the saying goes 'A gram of prevention is worth a kilogram of cure'.
It would have been impossible using random code. Using structured programming and functions could have worked but it would have been much more complicated and harder to debug. I would debug and test an object and put in a library. Then I’d write the top level code naking use of pretested objects. More work at the start, but a lot less work at the end. Easier maintenance and changes.
You can look at OOP as a form of reductionism.
OOP is more prevalent then you might think, you just don’t see it.
When you click on a folder a window appears. The OS dynamically spawns an object for the graphic window. I don’t know Windows classes. As you drag the window cursor xy is updated as something like window_handle.x widow_handle.y. When the window goes away the object is deallocated.
Same thing in my Scilab tool. When I create a graphics window to plot something I get an object to set parameters.
Code:
const int FILE_ERROR = -1;
const int LIMIT_ERROR = -2;
const int MAX_SIZE = 20;
#define ABORT(){cout<<"CHECK LOG"<<endl;return(-1);}
class LoadFile{
private:
int fsize = 0;
double *array_ptr;;
void error_log(int i){
char err_str[3][20] = {"NO EOF",
"ARRAY LIMIT",
"FILE ERROR"};
FILE *p = fopen("log.txt","a");
fprintf(p,"%s\n",err_str[i]);
fclose(p);
}
public:
int open_file(char *fname){
int x, i = 0;
FILE *ptr = fopen(fname,"r");
if(!ptr){
error_log(2);
return(FILE_ERROR);
}
while(fscanf(ptr,"%d",&x) == 1){
fsize += 1;
if(fsize > MAX_SIZE){
error_log(0);
return(FILE_ERROR);
}
}
array_ptr = new double[fsize];
rewind(ptr);
for(i = 0;i<fsize;i++){fscanf(ptr,"%d",&x);array_ptr[i] = x;}
fclose(ptr);
return(fsize);
}//open_file()
int read(int i){
//prevent over running the array
if(i >= 0 && i < fsize)return(array_ptr[i]);
error_log(1);
return(array_ptr[fsize-1]); //return last entry
}//read()
~LoadFile(void){cout<<"Goodbye"<<endl;}
};//LoadFile
int main(){
LoadFile f[2]; // create an array of file objects
char fname[2][20] = {"d1.txt","d2.txt"};
int i = 0,n[2],n_files = 2;
// boot initialization
for(i = 0;i<n_files;i++){
n[i] = f[i].open_file(fname[i]);
if(n[i] == FILE_ERROR)ABORT()
}
for(i = 0; i<n[0];i++){
cout<<f[0].read(i)<<endl;
}
for(i = 0; i<n[1];i++){
cout<<f[1].read(i)<<endl;
}
// destroy the objects.
for(i = 0;i<n_files;i++)f[i].~LoadFile();
return 0;
}
Last edited: