Thread: weird pointer issue

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    weird pointer issue

    Hey everyone,
    I haven't programmed in quite some time and was trying to get back into it. So I wrote a small tester program that's already giving me an issue:

    Code:
    int* arr = new int[10];
    
    for (int i = 0; i < 10; ++i)
            arr[i] = i * 2;
    
    for (int i = 0; i < 10; ++i)
            cout << arr[i] << endl;
    
    arr++;
    	
    delete [] arr;
    When I debug this, arr has 4 added to it (correct) and *arr equals the next element up (correct), but I'm getting an assertion error. Any insight into this crazy stuff?

    Thanks a lot!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    My forte is C, but my guess would be that you have to use delete on the original pointer value, but yours is being modified.
    Last edited by itsme86; 07-31-2006 at 06:38 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    wow

    Fantastic observation, and you have just taught me something new. Thanks a lot! Random question, is it possible to call delete on a modified pointer like this?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you could, you wouldn't be getting an assertion error
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Chaplin> Random question, is it possible to call delete on a modified pointer like this?

    A better question is "how does delete/free() know how much memory to free when being called with a pointer?"
    Think about it.

    ...A pretty common implementation is to also allocate some space just before the pointer, and there store the size of memory being allocated. That way, when delete or free() is being called they just move back and find the size of the chunk to free.

    And to furthermore answer your question; no, it isn't.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    And remember by delete[] you are deleting the array that pointer is pointing to, not the pointer itself, pointer remains in the stack until the end of its scope (if its automatic like your case).
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM