7 Replies - 390 Views - Last Post: 14 June 2012 - 01:09 PM Rate Topic: -----

#1 lilBullseye  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-May 12

Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:24 AM

So I'm relearning all about C++ and I'm on pointers and while making a really really simple program to test pointers something stops the program half way, after the initialization of my second pointer. Look:
    int test = 5;
    int *testPtr;
    *testPtr = test;
    cout << testPtr << endl;
    cout << *testPtr << endl;
    int *cPtr;
    int c = 10; //Comment this line out and it works fine
    *cPtr = c; //Also comment this out of course.

    cout << *cPtr<<endl;


That's all that's in main() and the program starts and then stops with a "Program1.exe has stopped working" then it prints out the results from the first test "testPtr" but nothing else. I'm using Code::Blocks in case it matters to anyone. Any help is really appreciated! Thanks --LilB

Is This A Good Question/Topic? 0
  • +

Replies To: Error while using multiple pointers, stops program.

#2 lilBullseye  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-May 12

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:29 AM

So I'm relearning all about C++ and I'm on pointers and while making a really really simple program to test pointers something stops the program half way, after the initialization of my second pointer. Look:
    int test = 5;
    int *testPtr;
    *testPtr = test;
    cout << testPtr << endl;
    cout << *testPtr << endl;
    int *cPtr;
    int c = 10; //Comment this line out and it works fine
    *cPtr = c; //Also comment this out of course.

    cout << *cPtr<<endl;


That's all that's in main() and the program starts and then stops with a "Program1.exe has stopped working" then it prints out the results from the first test "testPtr" but nothing else. I'm using Code::Blocks in case it matters to anyone. Any help is really appreciated! Thanks --LilB
Was This Post Helpful? 0
  • +
  • -

#3 lilBullseye  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-May 12

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:39 AM

Figured it out! I put c's initialization after the pointer's, making the program go crazy. Fixed.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:43 AM

The program works when you initialize the integer before the integer pointer. So in other words swap lines 6 and 7. Not sure why this happens but hopefully a more seasoned C++ developer will answer that question.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:44 AM

On lines 3 and 8 you're dereferencing uninitialized pointers. That's not allowed. A pointer must point to valid memory (like the address of a variable or an address returned by new) before you can dereference it.

PS: The fact that it seems to work when you comment out lines 7 and 8 is pure coincidence. It could have just as well crashed on line 3.

View PostlilBullseye, on 14 June 2012 - 07:39 PM, said:

Figured it out! I put c's initialization after the pointer's, making the program go crazy. Fixed.


Again that's pure coincidence. Your program is still invoking undefined behavior.

This post has been edited by sepp2k: 14 June 2012 - 01:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3989
  • View blog
  • Posts: 12,307
  • Joined: 25-December 09

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 10:45 AM

Never mind

This post has been edited by jimblumberg: 14 June 2012 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 fr0st  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 12:52 PM

Seems you skipped the part on memory allocation, check new and delete operators in c++.
Check this link for more info: http://www.cplusplus...torial/dynamic/

Basically when you declare a pointer, you need a memory location for the pointer to "point".
The pointer is nothing more than a variable containing a memory location,
that memory location can be used by an existing variable or a piece of dynamically allocated memory
through the new operator, whatever suits your needs.

What you did was trying to put some values in random places in memory, that wasn't meant to be used
by your program, at least not in that way, sometimes it passes unnoticed (not so sure about this),
but more often, you'll get a crash like you did.

Here's an example short example on both:

#include <iostream>
#include <new>

using namespace std;

int main()
{
	
	// using pointers to manipulate existing variables
	int a = 2;
	int *pointer_to_a;

	pointer_to_a = &a;
	(*pointer_to_a)++;
	cout << "The value of a: " << *pointer_to_a << endl; // 3

	// using pointers with dynamic memory allocation
	int *memory_pointer;
	memory_pointer = new int[5]; // this makes the pointer point to a location
								 // which contains an array of ints

	// so you can use it like
	cout << "Printing dynamically allocated array members" << endl;
	for (int i = 0; i < 5; i++)
	{
		memory_pointer[i] = i + 1;
		cout << "Value of the member[" << i <<"]: " << memory_pointer[i] << endl; // 1 2 3 4 5
	}

		return 0;
}




If interested in further learning this can push you in the right direction:
http://www.learncpp....k-and-the-heap/

P.S. I see you wrote relearning so I hope I didn't insult you by simplifing this
as much as I could, and if you or anyone have something to add/correct me, please do.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Error while using multiple pointers, stops program.

Posted 14 June 2012 - 01:09 PM

I've merged your duplicate threads into one. Please don't post multiple threads for one topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1