deferencing uninitialized pointer

This is a discussion on deferencing uninitialized pointer within the C Programming forums, part of the General Programming Boards category; Code: void swap (int *x, int *y) { int *temp; *temp = *x; *x = *y; *y = *temp; } ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    deferencing uninitialized pointer

    Code:
    void swap (int *x, int *y) {
        int *temp;
        *temp = *x;
        *x = *y;
        *y = *temp;
    }
    This code, as you know, has a good chance of crashing depending on where the temp pointer points to. My problem is to write a procedure that guarantees its crashing when run like this:

    Code:
    int main () {
        int a = 1, b = 2;
        proc(/* Some args might go here */);
        swap(&a, &b);
    }
    Any ideas?

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    void swap (int *x, int *y) {
        int *temp = 0;
        *temp = *x;
        *x = *y;
        *y = *temp;
    }
    That'll crash it every time for sure
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    No no no, I'm asking about how to write a procedure 'proc', that when run in the 2nd segment of code, guarantees the deference in swap to crash.

    Can't modify swap itself.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why whould you want it to crash?
    Just pass a NULL or invalid pointer as one of swap's parameters.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,313
    I refuse to answer this question based on its absurdity, and the fact that the only reason I can see to want to know such a thing is to write an exploit of some kind.
    If that's not the case you'll have to exlpain your reasons and justify them too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    No idea why you'd want to, but passing NULL for either argument would probably be bad.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    It's a purely academic question. The arguments to swap are fixed. A hint that I received is that proc should do something to the stack. The crash has to happen on deferencing temp, not the arguments to swap.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    As such, it's not a C question, it's a question about the underlying architecture.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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