-6

Dont know how to do this. Tried something like below.Want more optimisation in code.
Everyting should be in one function only,
guide me how to open close a file,
how to find character in each line,
increase the counter.

void simpleFileIn(void) {

    string line;
    ifstream myfile("example.txt");
    if (myfile.is_open()) {
        while (getline(myfile, line)) {
            //found(line);
            size_t size = strlen(line);
            cout << line << '\n';
        }
        myfile.close();
    }
    else
        cout << "Unable to open file";
}

the function simpleFileIn() should work, to open a file then close after work is done.
find out character a and count the integers. want to close/ delete this question as i am in ban asking more help me. situation getting worse day after day

6
  • 2
    What is the question?
    – Yunnosch
    Commented Aug 26, 2017 at 9:45
  • Please epxlain what you think this line does *pptr=ptr;. Especially what the difference is between its effect on pptr[0] and pptr[1].
    – Yunnosch
    Commented Aug 26, 2017 at 9:46
  • 2
    Obligatory "use std::vector instead of raw arrays" Commented Aug 26, 2017 at 9:49
  • I'm not sure how so many people are asking "what are you really asking". To me it seems clear enough: OP wants to use his dynamically-allocated 2D array, but finds that he cannot. The question is: why? Answer: he's allocating it wrong: see all existing answers on this thread.
    – Dúthomhas
    Commented Aug 26, 2017 at 21:38
  • 1
    @Askish Kamble It is not allowed here to change question (especially after there are answers and you accept one). For new question, create new question (thread). Thanks.
    – kocica
    Commented Oct 18, 2018 at 11:35

4 Answers 4

3

You need to allocate rows in a loop as you go:

int** pptr = new int* [rows]; // <<== rows, not cols
for(int i=0;i<rows;i++){
    pptr[i] = new int[cols]; // <<== Add this line
    for(int j=0;j<cols;j++){
        cout<<"Enter value at "<<i<<j<<endl;
        cin>>pptr[i][j];
        cout<<"Value is "<<pptr[i][j]<<endl;
    }
}

You also need to delete individual rows before deleting the array of pointers to them. Use delete[] operator with square brackets:

for(int i=0;i<rows;i++){
    delete[] pptr[i];
}
delete[] pptr;

You do not need to assign NULLs to deleted pointers, unless you plan to reuse the pointer for something else later on.

2

You are allocing array of pointers wrong.

First you have to allocate enough space for row pointers

int** pptr = new int* [rows];

For every pointer enough space for col integers

for (int i = 0; i < cols; i++)
{
    pptr[i] = new int [cols];
}

To delete arrays use delete[] instead of delete.

Delete every individual row

for (int i = 0; i < rows; i++)
{
    delete [] pptr[i];
}

Then delte array of pointers

delete [] pptr;

There is no need to assigning NULL to deleted pointer since you wont use them again. Also in you should use nullptr instead of NULL.

Here is the correct using of array of pointers.


Your mistakes

int* ptr = new int [rows];
int** pptr = new int* [cols];
*pptr=ptr;
  • Swapped rows & cols
  • Allocated memory only for first pointer/row, others were uninitialized -> UB
  • Used delete instead of delete[]
1

So there seems to be some confusion with allocation. From your code

int* ptr = new int [rows];
int** pptr = new int* [cols];
*pptr=ptr;

you have now created to 1-dimensional arrays. You then dereference the pptr and assign to it ptr this is the same as

pptr[0] = ptr;

So you are only initializing the very first column. You want to change this code to be

int** pptr = new int* [cols];
for (int i = 0; i < cols; ++i) {
    pptr[i] = new int [rows];
}

This will allocate the memory properly

1

You can create a constructor of sorts for your 2D array so that you have one-line bookkeeping:

#include <iostream>

template <typename T>
T** new_( std::size_t rows, std::size_t columns )
{
  auto dsize = rows    * sizeof(T*);
  auto rsize = columns * sizeof(T);
  auto tsize = rows    * rsize;

  unsigned char* data = new unsigned char[ dsize + tsize ];
  T** result = (T**)data;
  T*  table  = (T*)(data + dsize);

  while (rows--) 
    result[ rows ] = table + rows * columns;

  return result;
}

int main()
{
  int m; std::cout << "m? ";  std::cin >> m;
  int n; std::cout << "n? ";  std::cin >> n;

  // Create the new matrix
  int** a = new_<int>( m, n );

  // Do stuff with a[ r ][ c ] here.
  // It looks and behaves JUST LIKE a normal 2D C array
  // in all respects EXCEPT one: &a != &(a[0][0]).
  // Use the latter when passing to a flat function!

  // Delete it
  delete [] a;
}

Enjoy the weirdness.

Not the answer you're looking for? Browse other questions tagged or ask your own question.