Allocating array in quadriple pointer [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Suppose I have int ****ptr;. I want to allocate a dynamic 1-dimensional array on the end of this ptr, so when I type ***ptr[4] I would yield an element. Please, help.
p.s.
I am not using this code in my real applications, it's just an intellectual exercise to understand how pointers work.
I cannot directly do ***ptr = malloc(sizeof(int)*size_of_arr)); right? BEcause this way i will not be able to yield any elements

int ****ptr;
ptr = new int***();
*ptr = new int**();
**ptr = new int*();
***ptr = new int[size_of_arr];
//access (***ptr)[index]
delete[] ***ptr;
delete **ptr;
delete *ptr;
delete ptr;

Note that ***ptr[4] and (***ptr)[4] are not the same thing. The index operator ([4]) takes precedence over the dereference operator (*).
int ****ptr;
ptr = new int***[5];
ptr[4] = new int**;
*ptr[4] = new int*;
**ptr[4] = new int;
***ptr[4] = 77;
delete **ptr[4];
delete *ptr[4];
delete ptr[4];
delete [] ptr;

Related

Can someone tell me what is wrong with this statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How is this statement
int* p = new int[10];
p[10] = 5;
delete[] p;
different with this statement ?
int* p = new int[10];
*p = 5;
delete[] p;
I wanna what is wrong with the first code. I'm kinda new to C++, so any explanation is appreciated. Thanks.
The behaviour of p[10] is undefined as the array has only 10 elements and the first element is at position 0. You can access p[0] (which is the same as *p) to p[9] inclusive.
In other words, arrays in C++ are zero-based. Cf. Fortran, for example, where they are one-based.

In C++, why should I use "new" if I can directly assign an integer to the pointer without using "new"? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Assuming my pointer is not pointing to an array, how is this block:
int *ptr_a;
ptr_a = new int;
*ptr_a = 1;
different compared with:
int *ptr_a = 1;
Is there a difference in memory allocation and when would I use one over the other?
int *ptr_a = 1;doesn't create a new int, this creates a pointer ptr_a and assigns a value of 1 to it, meaning that this pointer will point to the address 0x00000001. It's not an integer. If you try to use the pointer later with *ptr_a = 2, you will get a segmentation fault because the pointer doesn't point to an allocated memory space (in this precise case, it points to kernel memory, which is a no-no).
A good principle nowadays is to used std::unique_ptr<int> ptr_a = std::make_unique<int>(1)which will allocate a new int with a value of 1 and deallocate the memory once ptr_a gets out of scope.

C++ Convert number to pointer of type? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following bit of code:
int* anInt = new int(5);
uintptr_t memAddr = (uintptr_t)&anInt;
Log("memAddr is: " + std::to_string(memAddr));
int* anotherInt = (int*)&memAddr;
Log("anInt is: " + std::to_string(*anInt));
Log("anotherInt is: " + std::to_string(*anotherInt));
Now I want anotherInt to point to the same value as anInt, but the current code makes anotherInt point to the value of memAddr. How can I set anotherInt to point to the value of anInt using only memAddr?
You can type cast a pointer directly to an integer. This will make anotherInt point to the same int as anInt.
uintptr_t memAddr = (uintptr_t)anInt;
...
int* anotherInt = (int*)memAddr;
Alternatively you can have memAddr store the address of the pointer (a pointer to a pointer) and use it as such:
uintptr_t memAddr = (uintptr_t)&anInt;
...
// only works if the variable "anInt" is still alive
int* anotherInt = *(int**)memAddr;

How do explicitly create temporary by pointer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
int i=int(); //OK
int* pi=int*(); //error
How to do it correctly?
You can default initialize both of them with uniform initialization.
int i{};
int *ip{};
The constructor like calls are unnecessary and convoluted, as of course, behave differently for some types as you've found out yourself.
If what you're trying to do is have the pointer point to space allocated for an int, you'll need to allocate with new
int *ip = new int;
or give it the address of an existing int
int i{};
int *ip = &i;
You can't construct a pointer. You get it by:
Getting a reference:
int x;
int* xpointer = &x; //pointer to x
Creating using new:
int* x = new int;
Try
int * i = new int();
This is basic C++, maybe you need to familiarize yourself with dynamic memory allocation. This is a good enough resource on the subject: http://www.cplusplus.com/doc/tutorial/dynamic/

C++ lack of logic on pointer initialisation & use [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is not logic :
int *ptr = &otherInt;
When we do that, ptr gives adress of otherInt, *ptr gives otherInt.
But we write explicitly that (*ptr) equals adress *ptr = &otherInt !
Logically, *ptr should give the adress, and ptr the otherInt.
Don't you think ?
I think you got it all wrong. Consider this:
int *ptr;
int otherInt = 10;
ptr = &otherInt; // Notice this line.
The asterisk is a part of the declaration, not the assignment.