Pointer's

This is a discussion on Pointer's within the C++ Programming forums, part of the General Programming Boards category; Hello, here's the code Code: #include <iostream.h> int main() { int x; int *ptr = new int; ptr=&x; cin>>x; cout<<*ptr; ...

  1. #1
    Unregistered
    Guest

    Pointer's

    Hello, here's the code
    Code:
    #include <iostream.h>
    int main()
    {
      int x;
      int *ptr = new int;
      ptr=&x;
      cin>>x;
      cout<<*ptr;
      delete ptr;
      return 0;
    }
    I get the following error's when executing. 12 is my input.
    Code:
    12
    12Exiting due to signal SIGSEGV
    Page fault at eip=000057c4, error=0006
    eax=000be93c ebx=000be938 ecx=000be800 edx=000be93c esi=00000054 edi=0003e960
    ebp=000be8e8 esp=000be8dc program=C:\DJGPP\WTF\POINTER.EXE
    cs: sel=00a7  base=8563a000  limit=ffb13fff
    ds: sel=00b7  base=8563a000  limit=ffb13fff
    es: sel=00b7  base=8563a000  limit=ffb13fff
    fs: sel=0087  base=0000c220  limit=0000ffff
    gs: sel=00c7  base=00000000  limit=0010ffff
    ss: sel=00b7  base=8563a000  limit=ffb13fff
    App stack: [000be960..0003e960]  Exceptn stack: [0003e8bc..0003c97c]
    
    Call frame traceback EIPs:
      0x000057c4
      0x0000f941
      0x00001633
      0x00005188
    I know the error arn't code, i just wanted to keep it seperate.
    My compiler is djgpp.
    Any help would be much appreaciated.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you're right about the code... it's fine...

    i see you're using djgpp compiler.....i am not a big fan of it.....

    seams like the compiler has a conflict in allocating memory between the stack and the heap......

    that's what i am sensing out.....

    i suggest a different compiler or maybe try installing this one all over again...

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you point ptr to x, then you don't need to allocate space for ptr.
    Code:
    #include <iostream.h>
    int main()
    {
      int x;
      int *ptr;
      ptr=&x;
      cin>>x;
      cout<<*ptr;
      return 0;
    }
    If you want to use dynamic allocation, then you don't need x.
    Code:
    #include <iostream.h>
    int main()
    {
      int *ptr = new int;
      cin>>*ptr;
      cout<<*ptr;
      delete ptr;
      return 0;
    }

  4. #4
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    ahh i think i see.

    Thank's swoopy, i jotted that down.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 11:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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