0

I came across this statement in a book.

new employee *[num];

where employee has already been declared as struct employee and num is an int;

this is on the right hand side of = operator on the statement.

So, what is meant by that statment? The book doesn't offer any explanation of the said statement.

Master Chief
  • 2,520
  • 19
  • 28
  • It allocates a new employee* array of size num. The return value is never caught. – Ben Jun 22 '14 at 11:32
  • 2
    IMO this is an example of why the * should go with the rest of the type. – Ben Jun 22 '14 at 11:33
  • This is creating an array of `num` pointers to `employee` – haccks Jun 22 '14 at 11:38
  • why the downvotes for a legitimate question? maybe not all of us are pure c++ pros... – xmoex Jun 22 '14 at 11:38
  • @xmoex Thanks for some support. The book doesn't give any explanation for the declaration. Only usage in middle of a program. – Master Chief Jun 22 '14 at 11:43
  • @xmoex Doesn't have to do with the lack of c++ knowledge shown in this question, but it's incomplete in form and badly asked. It's unlikely the answer helps anyone else than the OP. – πάντα ῥεῖ Jun 22 '14 at 11:44
  • @xmoex: also some basic google search will help the OP find the answer. – NirMH Jun 22 '14 at 11:48
  • @πάνταῥεῖ Which is disturbing since all the _possible_ duplicates I've found thus far are even less clear. – Captain Obvlious Jun 22 '14 at 11:49
  • @CaptainObvlious Then at least the title should be edited to be something more meaningful in relation to the question. – πάντα ῥεῖ Jun 22 '14 at 11:50
  • 1
    i don't want to initiate a chat about this but i think what one could find via google or elsewhere depends on how familiar you are with a certain topic. i think it's a perfectly legal question for so and from my point of view it's not at all "badly asked". +1 solidarity upvote... – xmoex Jun 22 '14 at 11:51
  • @πάνταῥεῖ ... then maybe helping in improving the question would be a better idea than just downvoting... – xmoex Jun 22 '14 at 11:52
  • @xmoex Improvement is primarily left to the OP. I'm willing to retract my close/down vote if the question is made clearer and better. – πάντα ῥεῖ Jun 22 '14 at 11:54
  • Okay I came here after googling. I know that to search for answer is faster than to post question and then to wait for someone to answer it. But I could not find any relevant answer or for that matter any kind of related post. – Master Chief Jun 22 '14 at 11:54
  • 1
    The question @ http://stackoverflow.com/questions/5776529/int-array-new-intn-what-is-this-function-actually-doing is slightly different but the answers address your query. – Captain Obvlious Jun 22 '14 at 12:03
  • 1
    I came across that, and readily understood that, but here the * was creating problem for me. Now I understand that we are creating array of pointers at run-time. In that question array of int was being created. After seeing the answers here now I am able to find the similarity between the two. But I was all confused before. – Master Chief Jun 22 '14 at 12:10

2 Answers2

7

This will allocate a memory required to hold num number of pointers to employee on the free store.

e.g:

employee** a = new employee* [2]; // 2 pointers on the heap

heap:

address a (e.g. 0x97a0008): pointer1 to employee

address a + 1 ( 0x97a000c): pointer2 to employee

Side note: you use delete[] on arrays so you can delete above with delete[] a BUT you will have to loop first through all entries if you have allocated memory for them so it have to be freed before you loose the pointer.

https://stackoverflow.com/a/13477214/1141471

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118
2

Think it is like this,

typedef employee* TEmployee;
TEmployee * ap = new TEmployee[10];

So, it allocates memory for dynamic array of size 10 type TEmployee (not employee). It is as simple as,

int * aip = new int[10];

TEmployee itself is a pointer type to employee.

Doonyx
  • 580
  • 3
  • 12