Pointer error

This is a discussion on Pointer error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { int * pointer = 0x100579C; //ERROR ZONE *pointer = 1; delete ...

  1. #1
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19

    Talking Pointer error

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int * pointer = 0x100579C; //ERROR ZONE
    	*pointer = 1;
    	delete pointer;
    	system("PAUSE");
    	return 0;
    }
    It's supposed to change the value at 0x100579C. I don't see anything wrong with it. But when I run the code, the compiler produces an error.

    invalid conversion from `int' to `int*'
    How would you fix this? Thanks!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think the compiler is just trying to tell you that you're about to do something bad (i.e. assigning a number to a pointer). You can easily cast away this error, but you'd crash very soon after running the program, since you have no idea who owns the memory at address 0x100579C.

    Then you try to delete a pointer which you didn't new. If the *pointer = 1; didn't kill you, the delete surely would.

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    "0x100579C" is an integer. You need to cast it as a pointer to an integer. e.g, (int*)

  4. #4
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19
    I took away the delete. How do I cast it as a pointer to an integer? The example was hard to follow. Like, a longer example. Thanks!!
    #WriteProcessMemory

  5. #5
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    int * pointer = (int*)0x100579C; //ERROR ZONE

  6. #6
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19
    Thank you! Yeah, the program didn't work but atleast I learned something. It didn't exactly crash, I got a error message and it exited. Anyway, thanks!
    #WriteProcessMemory

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    this is c-style cast - that should be probably left to C...

    FAQ: Difference between C and C++ style casting
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It didn't exactly crash, I got a error message and it exited.
    What makes you think that's not crashing?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by CornedBee View Post
    What makes you think that's not crashing?
    It is because his harddisk was still working, and no other hardware was springing out of the desktop to splash into 1000 little pieces arond his room...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21