Dynamic memory allocaion in c++ [duplicate] - c++

This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.

The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.

Related

What are the differences between char** and char*& in CPP [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 4 months ago.
For an assignment I came across this question.
What is the result of the statement following the definitions given below?
char c='a';
char *pc=&c;
char *&rc=pc ;
(*rc)++;
after printing all the different variables it shows that now variable c stores 'b'.
I didn't understand why.
Can anyone please explain?
Both char** (pointer-to-pointer) and char*& (reference-to-pointer) are second level abstractions of a value. (There's no pointer-to-reference type btw.)
The differences are really the same as they are for regular pointers vs. references: nullable vs. non-nullable etc. See here for more: https://www.geeksforgeeks.org/pointers-vs-references-cpp/
In your example, incrementing *rc does the same as would with using *pc.
Note that you don't need a symbol for accessing a references value as you would with * when using a pointer.
Just for fun: Try char** ppc = &pc. Can you tell what this does ?

What happens when you add an integer value to an array? [duplicate]

This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 3 years ago.
In many posts I have seen answers like
std::sort(arr1, arr1 + size1);
I do not understand what is happening at arr1 + size1 where an array is being added with an integer, is it concatenation, or is it adding size1 to each element of the array? Neither of them seem like a logical thing required to sort an array. I tried printing the result but, it isn't possible to print arrays in C++. Could someone please explain what is going on here?
It's pointer arithmetic. arr1 is a pointer to the beginning of the array, and arr1 + size1 points to just beyond the end of the array.
It's adding the integer to the address of the array (the address of the first element). Yielding an address (if the arithmetic is correct) to an element in the array.

Why does making an array pointer work better than an array in this situation [duplicate]

This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 3 years ago.
I have two ways of creating an array of N+1 elements, all filled with zeroes.
long int *a=new long int[N+1]();
vs.
long int a[N+1] = {0};
why does the first approach work better with larger inputs while the second one usually results in segmentation fault?

Passed array inconsistent bahevior; C++ [duplicate]

This question already has answers here:
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 5 years ago.
I'm trying to learn some basic C++ and the moment I started to think I got a grasp of all those pointers I stumbled across this problem:
int sizeOf(string texts[]) {
return sizeof(texts);
}
int main() {
string texts[] = {"apple", "banana", "orange", "watermelon"};
cout << sizeof(texts) << endl;
cout << sizeOf(texts) << endl;
}
and this function returns
128
8
My question is: what is happening when I pass this array as an argument? Why suddenly C++ forgets that this is an array? I have tried to dereference it inside a method (return sizeof(*texts)) but it returned 32 instead which is the size of one string element, not a whole array. Is it possible to do what I want to do? Where am I mistaken?

How does C++ process this [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.