C++ question including pointer, array and function - c++

So I have two programs here,
the first one is using dynamic allocation
and the second one is using fixed sized array.
Now the question is, by using dynamic allocation, the program runs fine AND outputs are correctly printed as expected.
However, when using fixed sized array (the second program), the program runs without errors BUT the outputs are not what I wanted.
The programs are almost same except how the arrays are created..but still both the arrays are same so shouldn't outputs be the same?
What are the reasons?? Please help me understand..
First Program Example:
input1 output1
1 1
2 2
3 3
4 4
5 5
Second Program Example:
input1 output1
1 1
2 5
3 2058618480
4 32766
5 5
// Using Dynamic Allocation
#include <iostream>
int *readNumbers(int n) {
int *a ;
a = new int[n];
for (int i=0; i<n; i++) {
std::cout << "enter for a["<<i<<"]: ";
std::cin >> a[i];
}
int *ptr;
ptr= &a[0];
return ptr;
}
void printNumbers(int *numbers,int length){
for (int i=0; i<length; i++) {
std::cout << *(numbers+i) << "\n";
}
}
int main(){
int n;
std::cout << "enter for n: " ;
std::cin >> n;
int *ptr;
ptr = readNumbers(n);
printNumbers(ptr,n);
delete [] ptr;
ptr = NULL;
return 0;
}
And another one is
// Using fixed size array
#include <iostream>
int *readNumbers(int n) {
int a[5]={};
for (int i=0; i<5; i++) {
std::cout << "enter for a["<<i<<"]: ";
std::cin >> a[i];
}
int *ptr;
ptr = &a[0];
return ptr;
}
void printNumbers(int *numbers,int length){
for (int i=0; i<length; i++) {
std::cout << *(numbers+i) << "\n";
}
}
int main(){
int *ptr;
ptr = readNumbers(5);
printNumbers(ptr,5);
return 0;
}

In your second piece of code your array is allocated on the stack inside the readNumbers function. Then you return a pointer to that stack memory to the calling function. This memory is no longer valid when printNumbers is run. It has likely been overwritten by locals in printNumbers.
Allocate the array in main and then the second example should also work.

I feel in first case, when you call new operator to allocate memory for storing multiple int values, heap memory is allocated. Now this memory is available when you pass it around functions and this memory is valid till programming is running until someone calls delete operator. So you could pass this pointer from readNumbers, main and printNumber and it is valid.
For second case you created array of int as local variable in function, so it is created in stack. Scope of the local variable is only till the function is running. In your example readNumbers created array and once the function is over the stack is cleared. That is all the local variables created in function are no longer valid.
Hence when you use this memory location in other functions like main and printNumbers it will give undefined behaviour. Sometime the result will be expected sometimes invalid result. So you need to be careful what are you passing or returning from one function to another.
If you still want to get expected result in second case, declare arrray as static.
Hope this helps.

Related

Using pointer variables to print specific array variables

I am a C++ beginner and my task is as follows:
Define and initialise a single-dimensional integer array. Next, define a pointer that points to the first element in the array and passes the pointer to a function.
Using only pointer variables (and looping constructs), print only the array values that are exact multiples of 7 from start to finish to standard output. The only program output should be the numbers, one per line with no white space.
I have tried:
void print_sevens(int* nums, int length)
{
int array[5] = { 5,8,21,43,70 };
int* ptr = array;
int* num = array;
for (int i = 0; i < length; i++)
{
*num++;
if (num[i] % 7 == 0) {
cout << num[i] << endl;
}
}
}
int main()
{
int array[5] = { 5,8,21,43,70 };
int* ptr = array;
print_sevens(ptr, 5);
}
It compiles but does not output anything.
I am also confused about passing the pointer to a function. Should this be done in the main file or in the function file?
You are creating an additional array in the print_sevens function, which is unnecessary as you already passed the pointer to the first element of the array created in the main()(i.e. array).
Removing that unnecessary array and related codes from the function will make the program run perfectly. (See online)
void print_sevens(int* nums, int length)
{
for (int i = 0; i < length; i++)
{
if (nums[i] % 7 == 0)
std::cout << nums[i] << std::endl;
}
}
and in the main you only need to do the follows, as arrays decay to the pointer pointing to its first element.
int main()
{
int array[5]{ 5,8,21,43,70 };
print_sevens(array, 5);
}
Note that,
num++ increments the pointer, not the underline element it is
pointing to (due to the higher operator precedence of operator++ than operator*). If you meant to increment the element(pointee) you
should have (*num)++.
Secondly, do not practice with using namespace std;. Read more:
Why is "using namespace std;" considered bad practice?
You are modifying the contents of your array when you do *num++.

Why do we really need pointers in Programming specifically C++

I can't really understand the difference between Dynamic and static allocation,they say Dynamic allocation happens while executing the program and static only while compiling and we can't allocate manually while execution but,
#include <iostream>
using namespace std;
int main()
{
int size , a = 0;
cout << "Enter the size of Array: ";
cin >> size;
int A[size][size];
for(int i = 0 ; i < size ; i++)
{
for(int j = 0 ; j < size ; j++)
cout << a++ << '\t';
cout << endl;
}
system("pause");
return 0;
}
This program will allocate the Array size while execution.
The real point of dynamic allocation is that you control the lifetime of the objects being allocated. Dynamically allocated objects exist until you deallocate them. It's not really anything to do with arrays, although that is often the context in which beginners are first taught about allocation.
Consider these two functions
int* bad()
{
int x = 123;
return &x;
}
int* good()
{
int* x = new int(123);
return x;
}
Both functions create an int and return a pointer to that int.
The bad function is incorrect because the x variable is destroyed when the function exits, so it returns a pointer to an object which has been destroyed.
The good function creates an int dynamically, that object will never be destroyed (unless the program deletes it). So this function is correct.
Incidentally int size; ... int A[size][size]; is not legal C++. Some compilers allow it, but other compilers would not.

using delete[] in main when new int [] was declared in a function

My first job is this:
"The first function, readNumbers, is to read a set of 10 numbers from std::cin and use them to initialise a dynamically allocated array of int of length 10 (dynamic allocation is on the heap). The function must return a pointer to the array. You may assume that the input will be 10 integer values and nothing else."
which is fine, however i need to then:
"The main function for this problem must call your readNumbers function, then pass the new array to your printNumbers function and finally delete the array."
However i am unsure of how to handle the delete in main, when the new int [] has been declared in a function. here is the relevant parts of my code.
int *readNumbers(){
int * numbers;
numbers = new int [10];
for (int i = 0; i < 10; i++){
int userinput;
cin >> userinput;
*(numbers+i) = userinput;
}
return numbers;
}
and
int main(){
printNumbers(readNumbers(),10);
delete[] numbers;
}
obviously the delete[] request in main is a syntax error, i am unsure of the solution.
You need to assign the result of readNumbers() to a variable, then you can delete it.
int main() {
int *numbers = readNumbers();
printNumbers(numbers, 10);
delete[] numbers;
return 0;
}

Can passing an array of pointers to a function as an 'int' parameter convert the addresses to integers?

Beginner c++ programmer, writing code to solve Sudoku puzzles. I'll keep the background info short to avoid confusion.
I have an array of pointers, int *P[9], I have assigned each entry a specific address. I want to assign these addresses to another array of pointers, int *B[81].
P[0] should correspond to B[0], P[1] to B[8], and so on.
When I pass these to a function:
void (int B[ ], int P[ ] ) {...}
it seems like the function is converting the address P[ ] is pointing to into an integer value. Before the function is called P[0] points to the address 0x7fff978d46b0, if I check the value of P[0] inside the function it's a number like `48782346 .
#include<iostream>
using namespace std;
void assign_box(int matrix[], int P[])
{
cout << "P[0] in function: " << P[0] << "\n";
matrix[0]=P[0];
}
int main()
{
int table[9][9];
//Initialise table entries to 0
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
table[i][j]=0;
}
}
//Assign addresses to vector P, for brevity P is of length one
int *P[1];
P[0]=&table[0][0];
cout<< "P[0] before function: " << P[0] << "\n";
int*B[81];
assign_box(B[81], P[9]);
}
If it did this and worked I wouldn't care, but unfortunately when I assign B[0] = P[0], it hits me with a Segmentation fault (core dumped), which makes me wonder is the function trying to assign the pointer B[0] to the address 48782346.
Is it possible for the function to convert an address into an integer value?
Apologies if my question is unclear or verbose, first time asker. And thank you for edits.
If you dereference int*[] (or int**), you get an int*. If you dereference an int*, you get an int. This is exactly what you are doing, and why you end up with an int at the end.
//main
int *P[1]; //Array of pointers to int
int *B[81]; //Array of pointer to int
assign_box(B[81], P[9]); //Pass in two pointers to int
//assign_box
matrix[0]=P[0]; //assign int to int
You probably meant to call assign_box like assign_box(B, P), and have the signature be void assign_box(int *B[], int *P[]);. This would then allow you to assign one pointer inside an array to another pointer inside an array.
There are multiple things that could be causing segmentation faults, but they all stem from invalid array indices. If an array is declared like type identifier[size];, it has valid indices from 0 to size - 1. So, int *B[81]; means B[81] is invalid.
You're passing in the wrong parameters. You're trying to pass in an array object B[81] which does NOT EXIST. You only have B[0] - B[80]. Also, B[80] isn't an int pointer. It's an int within an int array. P[9] is a pointer to an array of integers. So, you're trying to pass an integer in an array slot that does not exist into a parameter that does not take integers -- it takes integer arrays.
#include<iostream>
using namespace std;
void assign_box(int matrix[], int P[])
{
cout << "P[0] in function: " << P[0] << "\n";
matrix[0]=P[0];
}
int main()
{
int table[9][9];
//Initialise table entries to 0
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
table[i][j]=0;
}
}
//Assign addresses to vector P, for brevity P is of length one
int *P[1];
P[0]=&table[0][0];
cout<< "P[0] before function: " << P[0] << "\n";
int*B[81];
assign_box(B[81], P[9]); // WRONG
}

After passing by reference to modify an array, why it stays the same?

I am practicing pointers by creating a Big Number struct, which has numDigits (number of digits) and digits (contents of the big number).
I create a function called removeZero(). After passing the integer array and the size n into it, because of passing by reference, I am supposed to cut down the leading zeros for my input. It works, when the integer array is in main function. However, when I pass an array that is in readDigits, it does not return with a non-leading-zero version. Why? How to fix it?
struct BigNum{
int numDigits;
int *digits; //the content of the big num
};
int main(){
int A[] = {0,0,0,0,0,0,1,2,3};
int n=9;
int *B=A;
//removeZero(A,n); If I use this, it cannot compile
//error: invalid initialization of non-const reference of type ‘int*&’ from an rvalue of type ‘int*’
removeZero(B,n);
for (int i=0; i<n; i++){
std::cout << *(B+i) << std::endl;
}
BigNum *num = readDigits();
return 0;
}
BigNum* readDigits(){
std::string digits;
std::cout << "Input a big number:" << std::endl;
std::cin >> digits;
//resultPt in heap or in stack?
int *resultPt = new int[digits.length()]; //in heap
int n = digits.length();
toInt(digits,resultPt);
removeZero(resultPt,n);
//Output the leading zeros, why?
for (int i=0; i<n; i++){
std::cout << *(resultPt +i) << std::endl;
}
BigNum *numPtr = new BigNum();
numPtr->numDigits = n;
numPtr->digits = resultPt;
return numPtr;
}
void toInt(std::string& str, int *result){
for (int i=0;i<str.length() ;i++ ){
result[str.length()-i-1] = (int)(str[i]-'0');
}
}
void removeZero(int* &A,int& n){
int i=0;
while (A[i]==0){
i++;
}
A=A+i; //memory leak?
n=n-i;
}
bool areDigits(std::string num){
for(int i=0;i<num.length();i++){
if(num[i]<'0' || num[i] >'9'){
return false;
}
}
return true;
}
Note that an array and a pointer are two different things. When you pass an array to a function, it degrades to a const pointer. This means that you cannot pass an array to a function which expects a int*&.
It could be the problem of scope of numPtr.numPtr is local variable of function readDigits(). Instead of returning pointer. Pass num to readDigits().
The signature of your removeZero function is:
void removeZero(int* &A,int& n);
That means the forst parameter is a reference of a pointer but the pointer is a non-const one, and you cannot therefore pass an array there, as array is a constant pointer (starting address cannot be changed).
In fact you are changing the starting address within removeZero.
With removeZero, the while loop shopuld be changed from:
while (A[i]==0){
to:
while ((A[i]==0) && (i<n)){
You have a logic error in toInt.
void toInt(std::string& str, int *result){
for (int i=0;i<str.length() ;i++ ){
// This stores the digits in the reverse order.
result[str.length()-i-1] = (int)(str[i]-'0');
}
}
That line should be
result[i] = (int)(str[i]-'0');
If you intend to keep the digits in reverse order, then removeZero has to be changed keeping that in mind.
`
When you say
int *B=A;
you are just creating a pointer to point to the same memory
of the Array A. Just by incrementing the pointer(*B) within the function
removeZero
A=A+i;
you are not deleting anything but you are just incrementing the pointer(*B)
to point to subsequent memory location within the array.
The original array memory pointed to by A remains the same, since you
have not changed any contents of the array, but you have just
incremented a pointer pointing to the same memory location as that of the array.
Also there are so many problems, like "Debasish Jana" mentioned,
you have to change your while loop. ""Code-Apprentice" gave you the reason for your
compilation error when you uncomment your commented code.
Also within "removeZero" you are incrementing A by i instead of "1" like
A=A+1;
This is one of the reason for the strange behavior you experience
Even after changing all this, you cannot see your array getting changed,
since you are not modifying any of the contents of your array.
If you really want to delete the contents of the array and change it dynamically,
you have to go for Vector<>. With static memory allocation you cannot cut the
array size short by removing some elements here and there. Learn Vector<>!