Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have to write 2 functions type of void which works on address of pointers. First function reserve memory for array of objects of class. Second function reserve memory for array of pointers to object of class. I don't know how it should work and how can I deliver and work on adress of pointer.

What I have tried:

C++
#include <stdlib.h>
#include <stdio.h>

class my_class {
	int i;
};

void FAlocate(my_class ***Tab, int amount) {
	Tab = (my_class**)malloc(sizeof(my_class*)*amount);
	for (int i = 0; i < amount; i++) {
		Tab = (my_class*)malloc(sizeof(my_class));
	}
}

void FAlocate(my_class **Tab, int amount) {
	*Tab = (my_class*)malloc(sizeof(my_class)*amount);
}

int main(void)
{
	int N = 10;
	my_class **Tab=NULL;
	my_class *Tab1 = NULL;
	FAlocate(&*Tab, N);
	FAlocate(&Tab1, N);
}
Posted
Updated 2-Nov-18 12:54pm
v2

1 solution

First thing, this is not C, this is C++ (you have a class in it!), secondly, since this is C++, why involve too much of a pointer stuff, when you can easily use other methods to do the same thing—why pointer-to-pointer-to-pointer? Maybe you are confusing the pointers-are-arrays statement a bit much, in your function,
C++
void FAlocate(my_class ***Tab, int amount) {
	Tab = (my_class**)malloc(sizeof(my_class*)*amount); // my_class** to my_class***? 
	for (int i = 0; i < amount; i++) {
		Tab = (my_class*)malloc(sizeof(my_class)); // my_class* to my_class***
	}
}
You allocate the memory for pointer-to-pointer, and then keep updating that with a pointer value inside the loop. Instead what you are doing is, that you are trying to make the compiler build a program where the language constructs and type safety are not being respected. Don't you think it should be, this?
C++
void FAlocate(my_class ***Tab, int amount) {
	Tab = (my_class**)malloc(sizeof(my_class*)*amount);
	for (int i = 0; i < amount; i++) {
		Tab[i] = (my_class*)malloc(sizeof(my_class)); // I know the cast is "still" incorrect
	}
}
This should allocate the memory for the list items. I am ignoring the 3-dimensional array concept here, on purpose, because the code itself does not make much sense.

I will end this with this, in C++, use new and delete! There is a reason they are created in the language on top of malloc and free functions provided in C standard library.

c++ - What is the difference between new/delete and malloc/free? - Stack Overflow[^]
 
Share this answer
 
Comments
Member 14042074 3-Nov-18 3:36am    
I know this code is confusing and much much easier would be use C++ new and delete operators or just create function which return type of class, but it is excersise which gave me my professor on university - he didn't explain how I have to write it to do it - just do it T_T
Afzaal Ahmad Zeeshan 3-Nov-18 9:45am    
Right, that is understandable. In that case you should try to learn what a pointer is. Underlying the high-level features, an array is implemented using pointers and access by pointers, but a pointer itself is different. When you create an array, the overall procedure of memory allocation is different as to when a pointer to a variable has to be created.

Try reading, here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900