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.
Related
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 5 years ago.
Improve this question
I need store in a uint8_t pointer determinate positions of array.
Example:
uint8_t array[40] = {1,2,3,etc...}
uint8_t *pointer = array[5] - [25] . Save in pointer only positions between 5 to 25. I need in C++ language.
Thanks Community!
// Indices for the desired range of values.
constexpr std::size_t first = 5;
constexpr std::size_t last = 15;
// Create a new array with the appropriate size.
uint8_t array2[last - first];
// Copy the data to the array.
std::copy(array + first, array + last, std::begin(array2));
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;
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/
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.
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.