Does this make an array of pointers? - c++

So I'm trying to make a dynamically allocated array. To do that I'm trying to make an array of pointers. I used typedef to define the pointer type and tried to create the array of pointers using this code.
emprec * arrptr = new emprec[size];
my question is, is this the correct way to create an array of pointers? If not, what would be a better way of doing so.
emprec is defined in the program as seen below
struct emprec
{
int id;
float salary;
};
typedef emprec * emprecptr;
typedef emprec arr[MAXDBSIZE];
(I am a student and I'm trying to learn more about dynamic allocation)
After the kind help of you guys on here, it was made clear that I originally made an array of emprec which would be an array of structs. I didn't want that. Changing the code to
emprecptr* DBarray = new emprecptr[dbsize];
now gives me an array of pointers.

Given
struct emprec
{
int id;
float salary;
};
then, no
emprec * arrptr = new emprec[size];
is not an array of pointers, but an array of emprec's. But since you also had typedef emprec * emprecptr;, then
emprecptr * arrptr = new emprecptr[size];
would be an array of pointers (to emprec). Without the typedef you would have to write it as:
emprec* * arrptr = new emprec*[size];
But as you can see, the typedef simplifies the understanding of the code and possibly, makes it easier to read.

First point is not to confuse an array of pointers with a pointer to an array. You said the first, so I'm going to take you at your word. But what follows is misleading if you really meant the second.
In general to create a dynamic array of T you write
T* arr = new T[size];
Since you want an array of pointers T must be a pointer. For instance here is how to create an array of integer pointers.
int** arr = new int*[size];
Similarly an array of pointers to emprec would be
emprec** arr = new emprec*[size];
This can be simplifed using the typedef in your question to
emprecptr* arr = new emprecptr[size];

Related

Does new int[] return the address of the first element in the array or the address of the entire array?

int *a = new int[16];
How do I access the array afterwards?
Does a hold the address of the entire array or just the first element in the array?
Basically, I have this code,
struct student {
string name;
int age;
};
int num_students = get_number_of_students();
struct student *students = new student[num_students];
Now, how do I access the elements in that array?
a is simply a pointer to an int. It points to the beginning of the block of memory you have allocated. It does not carry any information about the size of the array.
As advised in comments, best practice in C++ is to use a std::vector for this, letting it handle the memory allocation (and importantly de-allocation) and provide convenient functions (via STL) for determining size and iterating over its elements.
std::vector<student> students(num_students);

How make a dynamic array using void** and void*?

I want to make a dynamic memory array function where in the arguments I can put in any type I want, the count, and the item I want. I've been googling and looking at YT videos, but none of them explain how to do just THAT, and when the item I want is a pointer. For example, I would have this:
struct Entity
{
int health;
int level;
int experience;
char* name;
}
Entity** entitylist = NULL;
int entitycount = 0;
Entity* CreateEntity(/*args for creating an entity*/)
{
Entity* newentity = malloc(sizeof(Entity));
// All of the entity creation stuff and at the end...
AddItemToList(&Entity, &newentity, &entitycount);
}
I know in the function I want to create I would need to pass in references to the specific list, but from that I'm pretty much clueless. I've tried to use malloc and realloc but either crashes the program or does nothing. And would new and delete work for this type of stuff?
And how would removing something like this work? I haven't seen anything on the internet yet about removing an item from a list, only adding them.
Thanks!
Using a double pointer for a data type such as int** with give you a dynamic 2D array, or a dynamic array of pointer objects, depending on your implementation, and a single int* is just a normal dynamic array. To full instantiate and allocate the memory for these, here's how you do it:
1D Dynamic array:
int* arr;
arr = new int[SIZE];
2D Dynamic Array:
int** arr;
arr = new int*[SIZE]; //<- stop here for 1D array of pointer objects
for (int i = 0; i < SIZE; i++)
arr[i] = new int[SIZE2];

initialize an int[][] with new()

I am a c++ newbie. While learning I came across this.
if I have a pointer like this
int (*a)[2][3]
cdecl.org describe this as declare a as pointer to array 2 of array 3 of int:
When I try
int x[2][3];
a = &x;
this works.
My question is how I can initialize a when using with new() say something like
a = new int [] [];
I tried some combinations but doesn't get it quite right.
Any help will be appreciated.
You will have to do it in two steps - first allocate an array of pointers to pointers(dynamically allocated arrays) and then, allocate each of them in turn. Overall I believe a better option is simply to use std::vector - that is the preferred C++ way of doing this kind of things.
Still here is an example on how to achieve what you want:
int a**;
a = new int*[2];
for (int i =0; i< 2;++i){
a[i] = new int[3]
}
... use them ...
// Don't forget to free the memory!
for (int i = 0; i< 2; ++i) {
delete [] a[i];
}
delete [] a;
EDIT: and as requested by Default - the vector version:
std::vector<std::vector<int> > a(2, std::vector<int>(3,0));
// Use a and C++ will take care to free the memory.
It's probably not the answer you're looking for, but what you
need is a new expression whose return type is (*)[2][3] This
is fairly simple to do; that's the return type of new int
[n][2][3], for example. Do this, and a will point to the
first element of an array of [2] of array of [3] int. A three
dimensional array, in sum.
The problem is that new doesn't return a pointer to the top
level array type; it returns a pointer to the first element of
the array. So if you do new int[2][3], the expression
allocates an array of 2 array of 3 int, but it returns
a pointer to an array of 3 int (int (*a)[3]), because in C++,
arrays are broken (for reasons of C compatibility). And there's
no way of forcing it to do otherwise. So if you want it to
return a pointer to a two dimensional array, you have to
allocate a three dimensional array. (The first dimension can be
1, so new [1][2][3] would do the trick, and effectively only
allocate a single [2][3].)
A better solution might be to wrap the array in a struct:
struct Array
{
int data[2][3];
};
You can then use new Array, and everything works as expected.
Except that the syntax needed to access the array will be
different.

double pointers to class

I remember seeing this line of code on my final exam, still to this day I can't figure it out or a use for it. I tired googling "double pointers" and "Pointer to Array of Classes" and still can't find an answer.
I want to know why you would use something like this (practical use) or direct me to a website that explain this for me. Thank you.
vendingMachine **va = new vendingMachine*[numMachines];
It doesn't really matter that the type involved is a class. It could just as well be a basic type:
int **va = new int*[numInts];
The semantics are the same. You have a pointer that points to a pointer. Since dynamically allocated arrays are implemented with pointers, this becomes an array of pointers. Here is a use case:
int a, b;
va[0] = &a;
va[1] = &b;
But since, as mentioned, you can implement arrays with pointers, you can also treat it as an array of arrays. So you have another use case:
va[0] = new int[10];
va[1] = new int[10];
// ...
va[numInts - 1] = new int[10];
va[0][0] = 2;
va[0][1] = 8;
// ...
va[numInts - 1][9] = 3;
So you have an array of arrays. Or in other words, a two dimensional array of ints. If you now change back from int to vendingMachine, you'd have a 2D array of vendingMachine objects.

What is ** in C++

I am currently reading some C++ source code, and I came across this:
double **out;
// ... lots of code here
// allocate memory for out
out = new double*[num];
Not entirely sure what it does, or what it means. Is it a pointer... to another pointer?
There is also the following:
double ***weight;
// allocate memory for weight
weight = new double**[numl];
I am quite confused :P, any help is appreciated.
new double*[num] is an array of double pointers i.e. each element of the array is a double*. You can allocate memory for each element using out[i] = new double; Similarly weight is an array of double**. You can allocate the memory for each weight element using new double*[num] (if it is supposed to be an array of double*)
It's a pointer to pointer to double. Or array of pointers to double. Or if every pointer itself allocates array it might be a matrix.
out = new double*[num]; // array of pointers
Now it depents if out[0] is allocated like this:
out[0] = new double; // one double
or like this:
out[0] = new double[num]; // now you've got a matrix
Actually, writing
double*[] out;
is in C/C++ equal to
double** out;
and it means an array of pointers to double. Or a pointer to pointers of double. Because an array is nothing more than just a pointer. So this is in essence a two-dimensional array.
You could as well write
double[][] out;
And likewise, adding another pointer level, will add another dimension to your array.
So
double ***weight;
is actually a pointer to a three-dimensional array.
Basically both of your code fragments allocate array of pointers. For allocation it does not matters to what. Correct declaration is needed only for type checks. Square bracjets should be read separately and means only it is array.
Consider following code as quick example:
#include <stdio.h>
int main()
{
unsigned num = 10;
double **p1, ***p2;
p1 = new double*[num];
p2 = new double**[num];
printf("%d\n", sizeof(p1));
printf("%d\n", sizeof(p2));
delete [] p1;
delete [] p2;
return 0;
}
Yes, both are just pointers. And memory allocated is sizeof(double*) * num.