Skip to content

dmgoldschmidt/code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Array.h is a template which implements dynamically allocated arrays which are refcounted.
Compiler generated copies and user assignments are shallow (pointer only) and therefore 
very efficient.  Deep copying can be done with the copy method.  Example of use: 

#include "Array.h"

Array<int> fill_it(int n){
  Array<int> a(n);
  for(int i = 0;i < n;i++)a[i] = i;
  return a;
}
int main(int argc, char** argv){
  Array<int> b;
  b = fill_it(10);
  cout << "b: "<<b<<endl;
}
Notes:
1. The declaration in fill_it allocates an array of n ints on the heap and puts a pointer on the stack.
2. The return statement in fill_it really just returns a pointer to a. The data is not copied.
3. The declaration in main does not allocate anything on the heap.  A pointer is created on the stack.
4. The assignment statement in main just resets the pointer.  When b goes out of scope and no other 
   pointers to the array exist, the heap memory is released. 
  

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published