-1

I'm working with an array, and an array of arrays :

typedef std::valarray<Complex> CArray;

CArray ca;

CArray x[16];

Does doing :

x[0] = ca;

do a hardcopy or just reference?

How to make that CArray x[16] is just a list of 16 pointers which will be assigned later to existing arrays? i.e. how to avoid hard-copying?

I'm not sure about this:

CArray *x[16];
x[0] = *ca;
Basj
  • 41,386
  • 99
  • 383
  • 673
  • Hard copy... BTW, you could have just as well run this in a debugger and see for yourself... – barak manos Jul 05 '16 at 12:44
  • Ok @barakmanos. How to avoid hard copy? – Basj Jul 05 '16 at 12:44
  • Like you said: make them pointers. – Sam Varshavchik Jul 05 '16 at 12:45
  • The context of your code is missing, so I cannot answer the question. For example, you could use `x[0]=&ca`, but you can rely on this only within the scope of the function (which, again, is missing from the context of your question). – barak manos Jul 05 '16 at 12:45
  • @SamVarshavchik I posted a possible solution at the end of question, but it doesn't work. What's wrong? – Basj Jul 05 '16 at 12:45
  • I suggest you learn some basic C++ before attempting to write in C++. – juanchopanza Jul 05 '16 at 12:57
  • @juanchopanza I'm learning it, but I like to combine both learning and actually making my project at the same time. That's how I did with math, with my PhD (it worked), with music, etc. This involves doing mistakes, but still at least you're working on making a project happen rather then learning theoritical things – Basj Jul 05 '16 at 13:08

2 Answers2

2

To create an array of pointers, you indeed should use

CArray *x[16];

To assign from a value or reference type, use & operator:

x[0] = &ca;

There are two things to keep in mind:

  • CArray ca is an automatic object, thus the lifetime of it is limited to scope of the variable.
  • The array x is not initialized to any value, you have to initialize it yourself if you rely on null pointers being there.
majk
  • 827
  • 5
  • 12
  • Isn't there a system that makes that the array will exist until there are no reference anymore for it? (I'm coming from Python, which garbage collector!) – Basj Jul 05 '16 at 12:50
  • You might want to look at std::shared_pointers. Here is a [quick reference](http://en.cppreference.com/w/cpp/memory/shared_ptr) – rtmh Jul 05 '16 at 12:53
  • 1
    C++ does not have a garbage collector per se. I would recommend looking at [Dynamic memory](http://www.cplusplus.com/doc/tutorial/dynamic/) first, then continue to shared_ptr. – majk Jul 05 '16 at 12:55
  • 1
    Good advice @majk. I've also got a better reference for shared(or 'smart') pointers [here](http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one). It's a beautiful SO write-up about them. – rtmh Jul 05 '16 at 13:00
1

You are right, x[0] = ca will perform copy-assignment, which apparently is not what you want.

You are close in your pointer example, but you instead need to get the address of ca:

CArray *x[16];
x[0] = &ca;

However, you must be very careful to manage the lifetime of ca properly lest it goes out of scope prematurely.

You may want to look into using smart pointers.

Edit: Given your comment on the other answer

"Isn't there a system that makes that the array will exist until there are no reference anymore for it? (I'm coming from Python, which garbage collector!"

Yes, like I mentioned before, you may want to use smart pointers. You could begin with std::unique_ptr if x will be the sole owner:

std::unique_ptr<CArray> x[16];

Will create an array of 16 unique pointers that will go out of scope after they are no longer being referenced. Here's how you could assign:

x[0] = std::make_unique<CArray>();

If you want other objects to participate in the lifetime of the memory (that is, the memory will not be deallocated until multiple people are finished referencing it), the process is much the same to use a shared_ptr:

std::shared_ptr<CArray> x[16];
x[0] = std::make_shared<CArray>();

Shared pointers are easier to work with because they're readily copied, but you pay a bit of cost in time and space overhead.

Community
  • 1
  • 1
AndyG
  • 39,700
  • 8
  • 109
  • 143