0

My problem is when I declare an array int** arr =new* int[n] and I want to assign to it pointer to array and later change that pointer to a different pointer which is copy of it values + one other number ,it brakes down and appears (probably) infinite loop . Can you say how to do this in proper way using some low tools with c++/c or can you correct my code?

Additional explenation: the code is producing very simple output but it is not important. I want to create program to change in array pointer(int*arr) in specific index pointer to diffrent pointer . But additionally pointers direct first element in arrays .Also diffrennce beetween new and old array (which is changed in int**arr in index for example 0) is that new is bigger on a new element (int this case new number).So this output is only checking if it works.

enter image description here

Below is my whole code


#include <iostream>
using namespace std;
void stepwise_fill_array(int ** arr, int N, int index)
{
   for(int j=1;j<=10;j++)
   {
       int* poi=arr[index];//getting pointer to array which i wannna change
       int size=0;
       while(poi){poi++;size++;}//getting size of pointer array from arr
       int* n= new int[size+1];//declaring the new array
       for(int i=0; i<size;i++)//copying from all values from old array to new one
           n[i]=poi[i];
       delete[] poi;    
       n[size]=j;//adding to the end new value
       arr[index]=n;//asigning arr[0] to new  diffrent array
   }
      for(int i=0;i<10;i++)
       cout<<arr[0][i]<<" ";
       //should print 1 2 3 4 5 6 7 8 9 10
}
int main(){
    int N = 10; // how big array should be and how many times it should expand
   int** arr = new int*[N];//declaring our array to pointer
   for(int i=0;i<N;i++)
   {
           arr[i]=nullptr;
   }
   int index =0;//index where I would change the pointer of arr   
  
   stepwise_fill_array(arr,N,index);
}

In advance thanks for your help :)

Sartyro
  • 29
  • 6
  • Is this a learning exercise? If so, make use of the compiler's abilities (like `-fsanitize=address`) shown [here](https://godbolt.org/z/hf3353neq) - otherwise, use one of the container classes in the standard library. – Ted Lyngmo Apr 27 '21 at 15:54
  • 1
    In the last 10 years of programming C++, I've not used nor needed `new`. Instead, I mostly use `std::unique_ptr` or `std::vector`, and rarely some other smart pointer, smart union (`std::variant` or `std::any`), or another appropriate container. – Eljay Apr 27 '21 at 15:57
  • `while(poi){poi++;size++;}` never executes. `arr[0]` is `nullptr`. The Boolean expression starts off false. Your comment about copying does no such thing, you've haven't done a single thing that could be copied yet. What is the **actual** goal of this program? `N` being global makes no sense. For a function called `print_this()`, it sure is doing a lot more than printing. – sweenish Apr 27 '21 at 15:58
  • In your updated code, you declare `N` after you've tried to use it. Please compile your code before submitting it. – Ted Lyngmo Apr 27 '21 at 16:12
  • New edit doesn't call the new function. And so far I am left wondering why you need a 2D array? You only ever work with just one row. – sweenish Apr 27 '21 at 16:12
  • I need it for this https://www.hackerrank.com/challenges/dynamic-array/problem. – Sartyro Apr 27 '21 at 16:23
  • Is there any reason why you are not using `std::vector> arr(N)` for this? – Ted Lyngmo Apr 27 '21 at 16:29
  • Did you see the result in godbolt? Your program has _undefined behavior_. – Ted Lyngmo Apr 27 '21 at 16:30
  • 3
    Honestly, that's a terrible question, and I still don't know what the purpose of it is. They use variables before they've defined them, the explanation of inputs is lacking, it's just a mess. And the comments on the question generally agree. I'd say cut your losses, and try to learn dynamic arrays (dynamic programming is something else entirely) from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – sweenish Apr 27 '21 at 16:34
  • I 'm so sad that no one understand what I want . Life is brutal :( – Sartyro Apr 27 '21 at 17:36
  • You don't make it easy to help though. Now there's a massive picture in the mix, but this isn't a question about processing images and therefore everything should be written as text in the question. Also, your code is formatted in a way that makes it very hard to read it. Indentation is a way to guide people reading the code and yours is _way_ off. – Ted Lyngmo Apr 27 '21 at 18:51
  • I must be a special one to not be able explain 20 line of code. @Ted Lyngmo Can you tell me what i should correct or what do you recommend to learn for me? – Sartyro Apr 27 '21 at 22:18
  • 1
    I think it's best explained in [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). You'll find some things that are obviously, like "_**DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question_" and other things that are more suttle, but by reading it you should get a pretty good idea of how to ask a good question. – Ted Lyngmo Apr 27 '21 at 22:26

1 Answers1

0

Your style of coding and explaining of problem is tragic , but fortunately I copied with it. When you are trying to get size from while(poi){poi++;size++;} you are getting in trouble. In C\C++ is no possibility to check size of array from pointer to this array. Instead you need increment size in every iteration of function stepwise_fill_array. Below I give you correct solution(in code are leaks but I doesn't affect in much way on efficiency):

void stepwise_fill_array(int **arr, int N, int index)
{
int size = 0;
for (int j = 1; j <= 10; j++)
{
  int *poi = arr[index];      //getting pointer to array which i wannna change
  int *n = new int[size + 1]; //declaring the new array
  for (int i = 0; i < size; i++)
  {
    n[i] = poi[i]; //copying from all values from old array to new one
  }
  n[size] = j;    //adding to the end new value
  arr[index] = n; //asigning arr[0] to new  diffrent array
  size++;
}
for (int i = 0; i < 10; i++)
  cout << arr[0][i] << " ";
//should print 1 2 3 4 5 6 7 8 9 10

}
Sartyro
  • 29
  • 6