NO

Author Topic: debugger error with pointers  (Read 3084 times)

czerny

  • Guest
debugger error with pointers
« on: April 25, 2011, 12:15:05 AM »
Hallo,

in the following example it is not possible to get any values from the pointer x:

#include <stdio.h>

int main(int argc, char *argv[])
{
   int *x=(int *)malloc(20);
   x++;
   x++;
   x++;
   x++;

   printf("%x\n",x);

    return 0;
}

and here

#include <stdio.h>

int main(int argc, char *argv[])
{
   int *x=(int *)malloc(20);
   *x++=1;
   *x++=2;
   *x++=3;
   *x++=4;
// last increment not displayed in debugger

   printf("%x\n",x);

// wrong value in debugger

    return 0;
}

czerny

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: debugger error with pointers
« Reply #1 on: April 26, 2011, 08:36:55 AM »
I tried it with Pelles C 6.50 RC4 and I found no real problems. The only thing I found, was that you could not step in the first example through the x++; section. If you look there in the disassemlby you see that the 4 x++; are translated to 2 instructions
Code: [Select]
lea eax, byte ptr [eax+8]
best regards
 Alex ;)

czerny

  • Guest
Re: debugger error with pointers
« Reply #2 on: April 26, 2011, 07:25:06 PM »
In Example 2:  Do you really see the printf'ed value after the last increment?
In Example 1:  I got no adress for x after the malloc at all, nor get I a adress value after the four
increments.  x is neither listed in local nor in global vars. If I force it (Schnellüberwachung) i got an error (Fehler im Ausdruck). Only the register value is correct.

czerny

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2107
Re: debugger error with pointers
« Reply #3 on: April 26, 2011, 08:16:26 PM »
Quote
In Example 2:  Do you really see the printf'ed value after the last increment?
Only if i take optimizations off, because without that x was in register eax.

Quote
In Example 1:  I got no adress for x after the malloc at all, nor get I a adress value after the four
Same thing,  x is in register eax if optimized.
With optimized:
Code: [Select]
_main:
  [00401000] push            ebp
  [00401001] mov             ebp,esp
{
   int *x=(int *)malloc(20);
  [00401003] push            +14
  [00401005] call            00401580
  [0040100A] pop             ecx
   x++;
   x++;
   x++;
   x++;
   printf("%x\n",x);
  [0040100B] add             eax,+8
  [0040100E] add             eax,+8
  [00401011] push            eax
  [00401012] push            00407000
  [00401017] call            004015F0
  [0040101C] add             esp,+8
    return 0;
  [0040101F] xor             eax,eax
}
  [00401021] pop             ebp
  [00401022] ret            
Without optimizations:
Code: [Select]
_main:
  [00401000] push            ebp
  [00401001] mov             ebp,esp
  [00401003] sub             esp,+4
{
   int *x=(int *)malloc(20);
  [00401006] push            +14
  [00401008] call            _malloc
  [0040100D] add             esp,+4
   x++;
   x++;
   x++;
   x++;
  [00401010] lea             eax,byte ptr [eax+8]
  [00401013] lea             eax,byte ptr [eax+8]
  [00401016] mov             dword ptr [ebp-4],eax
   printf("%x\n",x);
  [00401019] mov             eax,dword ptr [ebp-4]
  [0040101C] push            eax
  [0040101D] push            00407000
  [00401022] call            _printf
  [00401027] add             esp,+8
    return 0;
  [0040102A] mov             eax,00000000
}
  [0040102F] mov             esp,ebp
  [00401031] pop             ebp
  [00401032] ret            
Example 2:
Code: [Select]
_main:
  [00401000] 55                     push            ebp
  [00401001] 89E5                   mov             ebp,esp
{
   int *x=(int *)malloc(20);
  [00401003] 6A14                   push            +14
  [00401005] E896050000             call            _malloc
  [0040100A] 59                     pop             ecx
   *x++=1;
  [0040100B] C70001000000           mov             dword ptr [eax],00000001
  [00401011] 83C004                 add             eax,+4
   *x++=2;
  [00401014] C70002000000           mov             dword ptr [eax],00000002
  [0040101A] 83C004                 add             eax,+4
   *x++=3;
  [0040101D] C70003000000           mov             dword ptr [eax],00000003
  [00401023] 83C004                 add             eax,+4
   *x++=4;
  [00401026] C70004000000           mov             dword ptr [eax],00000004
// last increment not displayed in debugger

   printf("%x\n",x);
  [0040102C] 83C004                 add             eax,+4
  [0040102F] 50                     push            eax
  [00401030] 6800704000             push            00407000
  [00401035] E8D6050000             call            _printf
  [0040103A] 83C408                 add             esp,+8

// wrong value in debugger

    return 0;
  [0040103D] 31C0                   xor             eax,eax
}
  [0040103F] 5D                     pop             ebp
  [00401040] C3                     ret             
« Last Edit: April 27, 2011, 03:38:24 PM by timovjl »
May the source be with you

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: debugger error with pointers
« Reply #4 on: April 27, 2011, 09:45:18 AM »
In Example 1:  I got no adress for x after the malloc at all, nor get I a adress value after the four
increments.  x is neither listed in local nor in global vars. If I force it (Schnellüberwachung) i got an error (Fehler im Ausdruck). Only the register value is correct.
When I compile the first example at command line with "cc /Go /Ze /Zx /Zi /DEBUG test_pdg.c" and load the EXE-file in the IDE it works for me as expected.
When I compile it with a default console application and debug info the last increment is not shown.

PS: If I need messages for the forum, I start poide with the parameter /l 9. So the IDE starts in english and I get the original messages, which everybody here should be able to understand. ;)
best regards
 Alex ;)