Memory corruption?

Discussion in 'C' started by priyatendulkar, Nov 15, 2011.

  1. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi,

    Consider ,the following code

    unsigned int num =10;
    unsigned int * ptr = #

    however,When i look into ptr ,the address stored in "ptr" IS NOT EQUAL to the ADDRESS of num..

    I know this seems to be strange,but this is what I have obsreved..
    Is this any kind of memory corruption or some known issue wich I may not be aware of?
     
  2. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi
    You have got it wrong. The following program below should show it.
    ++++++++++++++++++++++++++++++++++++++++++++++
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main(){
    	unsigned int  num =100;
    	unsigned int *ptr=&num;
    
    	printf("%d\n",num);
    	
    	//if (ptr!=&num){
    	//	printf("Something is wrong");
    	//}
    	printf("&num:%d\n",&num);
    	printf("ptr:%d\n",ptr);
    	getchar();
    }
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     
    Last edited by a moderator: Nov 16, 2011
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Can you please share your code??
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Are you looking at (a) the address of ptr, (b) ptr itself, or (c) what ptr is pointing at? If (b) then it should be exactly equal and if it isn't, your compiler is critically broken and you need a new one. But given the question you are asking, I suspect you're a beginner and the difference is actually down to a or c.
     
  5. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    hi,

    i know this sounds strange..
    but I am looking at

    b) ptr itself...
     
  6. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Can you please share your code? If we can have a look at your code, we would be able to understand your problem better.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You'll have to share your code. Also let us know what version of what compiler, and what version of what operating system, you're using. This code proves ptr should equal &num:
    Code:
    void test44()
    {
    	unsigned int num=10;
    	unsigned int *ptr=&num;
    	printf("%d %d %x %x\n",num,*ptr,&num,ptr);
    }
    
    Output (Visual C++ 2010):
    10 10 1af79c 1af79c

    So upload a short demo like the one here that shows the problem and include the output showing the different values of &num and ptr. Because ptr should be exactly equal to &num, and if it isn't then as I said before, either your compiler is terminally broken and you should never use it again, or you are looking at the wrong thing.

    >>"When i look into ptr"
    How exactly? Are you using a debugger?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice