Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>

int n = 0;

void foo() {
    void* x;
    printf("%d\n", ++n);
    if (n >= 100) {
        exit(0);
    }
    *((char**) (&x + 4)) -= 5;
}

int main() {
    foo();
    return 1;
}


What I have tried:

i have tried to understand but i did not get it. please explain me .
Posted
Updated 19-Dec-16 23:12pm
v2
Comments
Kornfeld Eliyahu Peter 19-Dec-16 15:16pm    
How about using your debugger!?
[no name] 19-Dec-16 15:16pm    
Which specific part of what line is it that you do not understand?
[no name] 19-Dec-16 21:13pm    
It is NOT working. How have you "tried to understand"? Compile and run it if you can. Then break out the debugger.

As far as I can understand, the program is accessing unallocated memeory, since x is not initialized and it is very likely pointing to garbage. In other words it is flawed.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 20-Dec-16 6:23am    
Don't spoil the fun! :-)
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900