Hello everyone,

I'm pretty new here.

I'm facing a problem, the compiler says "Segmentation fault".

Please take a look at the code and tell me where I'm going wrong. I tried hard to figure it out, but I couldn't.

Thanks a lot.

int **PointerArray;
    *PointerArray = new int [LENGTH];

    for (int cnt = HEIGHT; cnt > 0; cnt--)
	PointerArray[LENGTH] = new int [HEIGHT];

    for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	PointerArray++;	
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    **PointerArray = cnt2;
	   *PointerArray++; 
	}
    }
   

     for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	PointerArray++;	
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    cout << **PointerArray << endl;
	   *PointerArray++; 
	}
    }

PointerArray is a two-dimensional array, right ? You have to allocate both dimensions

PointerArray = new int *[HEIGHT]; // allocate an array of int pointers
for(int i = 0; i < HEIGHT; i++)
    PointerArray[i] = new int[LENGTH];

The above will allocate what would be statically allocted like this: int PointerArray[HEIGHT][LENGTH];

Thanks a lot for your quick answer. I tried what you mentioned, yet having the same problem. Let me explain the whole story. I have been asked to write 2 big 2-dimensional array. One is done by subscripting and the other is done using pointer arithmetic. Then I'm supposed to take the time duration for both cases and consider which one is more efficient.

Please give me some ideas how to do it. This is what I have done up until now...

#include<iostream>
#include<ctime>
using namespace std;

#define LENGTH 400
#define HEIGHT 400

int main()
{
    time_t timeStart, timeEnd, PTimeStart, PTimeEnd;
    timeStart = time(0);

    //******************** Index Referencing ********************

    int IndexedArray[LENGTH][HEIGHT];
//
//   for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
//	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
//	    IndexedArray[cnt1][cnt2] = cnt2;
//
//
//   for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
//	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
//	   cout << IndexedArray[cnt1][cnt2] << " ";
//
//   timeEnd = time(0);
//  cout << "Performing time: " << difftime(timeEnd, timeStart) << " Seconds(s)." << endl;
//

    //******************** End of Index Referencing ********************

    //******************** Pointer Referencing ********************
    PTimeStart = time(0);

    int **PointerArray;
    PointerArray = new int* [HEIGHT];
    for (int cnt = HEIGHT; cnt > 0; cnt--)
	PointerArray[cnt] = new int [LENGTH];
    for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	PointerArray++;	
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    **PointerArray = cnt2;
	   *PointerArray++; 
	}
    }
   
     for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	PointerArray++;	
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    cout << **PointerArray << endl;
	   *PointerArray++; 
	}
    }

    PTimeEnd = time(0);
   cout << "Performing time: " << difftime(PTimeEnd, PTimeStart) << " Seconds(s)." << endl;
    //******************** End of Pointer Referencing ********************
}

After allocating the memory for the array you can not increment or decrement the pointer. When you do, you destroy the memory that was allocated because the pointer to the original memory is lost.

If you want to increment/decrement pointers, then create another pointer for that purpose, but leave the original pointer unchanged.

commented: I can't explain this in a better way :) ! +1

thanks again, But I didn't get what you meant in your answer. Could you please give me more details???

Thanks a lot

thanks again, But I didn't get what you meant in your answer. Could you please give me more details???

Thanks a lot

I think you may be looking at a segmentation fault here:

int **PointerArray;
    PointerArray = new int* [HEIGHT];
    for (int cnt = HEIGHT; cnt > 0; cnt--)
	PointerArray[cnt] = new int [LENGTH];

Indexes should be from 0 to HEIGHT - 1, not 1 to HEIGHT.

You don't want to change PointerArray because you want to be able to go back to it any time. If you change it, you potentially won't be able to "find" where it originally pointed and therefore you will have "lost" the data that it points to.

Incrementing and decrementing pointers is fine, but make a second variable like PointerArray2 and increment and decrement THAT pointer:

int** PointerArray;
// code to allocate, initialize array
int** PointerArray2 = PointerArray;

Now increment and decrement PointerArray2 . At any time, you can re-assign PointerArray2 to equal PointerArray again if you need to.

I did what you said, but still can't get the answer. It's really starting to get annoying. Please let me know if still there is something I'm going wrong.

int **pointerArray, **dummyPointer;

    pointerArray = new int* [HEIGHT];
    dummyPointer = pointerArray;

    for (int cnt = 0; cnt < HEIGHT; cnt++)
	pointerArray[cnt] = new int [LENGTH];

    for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    **dummyPointer = cnt2;
	    *dummyPointer++; 
	}
	dummyPointer++;	
    }

    dummyPointer = pointerArray;
   
     for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
	for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	{
	    cout << **dummyPointer << endl;
	    *dummyPointer++; 
	}
	dummyPointer++;	
    }

you are doing too much work!

#include <iostream>
using namespace std;
#pragma warning(disable: 4996)

const int HEIGHT = 5;
const int LENGTH = 4;
int main()
{
   int **pointerArray, *dummyPointer;

    pointerArray = new int* [HEIGHT];

    for (int cnt = 0; cnt < HEIGHT; cnt++)
	    pointerArray[cnt] = new int [LENGTH];

    for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
        dummyPointer = pointerArray[cnt1];
	    for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	    {
	        *dummyPointer = cnt2;
	        dummyPointer++; 
	    }
    }

   
    for (int cnt1 = 0; cnt1 < LENGTH; cnt1++)
    {
        dummyPointer = pointerArray[cnt1];
	    for (int cnt2 = 0; cnt2 < HEIGHT; cnt2++)
	    {
	        cout << *dummyPointer << endl;
	        dummyPointer++; 
	    }
    }
}

Thanks a lot, the problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.