How do explicitly create temporary by 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 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/

Related

is a star followed by a class name defining an array? c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I programmed several years ago, in which I wrote everything in the main function instead of using classes and other functions so I'm pretty confused. I have a project that has a specified format, and the way the array is defined is confusing me. I initially thought that xArray was a dynamic array, but when I try to assign a size to it, it gives error E0513 "a value of type "int *" cannot be assigned to an entity of type Group1
Group1{
int x,y;
}
Group2{
int a,b;
Group1* xArray;
}
void main{
Group1 g1;
int length;
cout <<"enter the length"<< endl;
cin >> length;
g1.xArray = new int[length];
}
so basically, I have a few questions, 1. what exactly is xArray(pointer? array?) 2. if it is a dynamic array, can I assign a length to it (which would be an input variable) outside the class where it's defined?
Your issue is that you are attempting to assign an array of one type, to a pointer of another. Here is a simpler example:
struct OneInt {
int a;
}
...
OneInt *o = new int[5]; // This cannot work, int is not the same type as OneInt
OneInt *o = new OneInt[5]; // This will allocate memory for an array of type OneInt
But honestly, you should do neither. You are looking for a dynamically sized vector, so c++ helps you by providing std::vector:
Group2{
int a,b;
std::vector<Group1> xArray; // By using an array, the memory is initialized and
// handled automatically
}
Group2 g;
g.xArray.resize(5);
This way, we don't need to mess around with new and delete and we have better, exception safe, encapsulated code. This is a technique of modern c++ called Resource Aquisition is Initialization (RAII).

Returning pointer to an array of arrays created 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 3 years ago.
Improve this question
I have created an array of arrays using char (*H)[N] = new char[M][N];
I want to return this pointer to my main function. My question is what should the function return type be in this case? Am I allowed to have a return type as a pointer to an array of arrays.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes, this would be the syntax for your particular case:
char (*get_array())[N] {
return H;
}
But you really should consider using either std::unique_ptr<char[N]> or std::array<std::array<char, N>, M>.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes. One way to do that would be to define a type alias and use it as the return type.
using MyArrayPointer = char (*)[N];
MyArrayPointer foo()
{
auto ptr = new char[M][N];
return ptr;
}

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.

Allocating array in quadriple 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 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;

2 value in one variable (const and const_cast) c++ [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
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.