Using GDB to tell if variables are signed or unsigned

0

So I am new to using GDB and I am using the following program as an excercise

#include <stdio.h>
#include <stdlib.h>

int main() {

    unsigned int value1, abs_value1, abs_value2, abs_value3;
    int value2, value3;
    char myarray[10]; 

    printf("Enter 10 characters:");
    fgets(myarray, 11, stdin); 

    printf("Enter an integer between 0 and 10,000:");
    scanf("%d", &value1);
    abs_value1 = abs(value1);

    printf("Enter an integer between -10,000 and 0:");
    scanf("%d", &value2);
    abs_value2 = abs(value2);

    printf("Enter an integer between -10,000 and 10,000:");
    scanf("%d", &value3);
    abs_value3 = abs(value3);

    // set breakpoint here

    return 0;
}

The values I've entered are as follows...

myarray[10] = "characters"
value1 = 578
value2 = -1123
value3 = 999

After running some commands I got the following output...

x/1x myarray : 0x63
x/1c myarray : 99 'c'
x/1s myarray : "characters"
x/1d &abs_value1 : 578
x/1x &value1 : 0x00000242
x/1d &abs_value2 : 1123
x/1x &value2 : 0xfffffb9d
x/1d &abs_value3 : 999
x/1x &value3 :0x000003e7
x/1c &value1 : 66 'B'
x/1c &value2 : -99 '\235'

So my question is, without looking at the code and using only the previous commands, can we tell if value1, value2, and value3 are signed or unsigned?

To my knowledge I don't think there is enough information to tell if they are signed or unsigned. My first instinct was to look for negative values but since we are taking the absolute value of our variables there is no way to be certain if the the value began as a negative number based of just looking at the commands.

Are there some different ways we might be able to deduce if the variables are signed or unsigned?

gdb
unsigned
signed
asked on Stack Overflow Sep 11, 2020 by Nicholas

1 Answer

1

Are there some different ways we might be able to deduce if the variables are signed or unsigned?

With debug info enabled this is quite easy. You can use ptype command but you should build the binary with -g option to make it work.

(gdb) ptype value1 
type = unsigned int
(gdb) ptype abs_value1 
type = unsigned int
(gdb) ptype abs_value2 
type = unsigned int
(gdb) ptype abs_value3 
type = unsigned int
(gdb) ptype value2 
type = int
(gdb) ptype value3 
type = int
(gdb) ptype myarray 
type = char [10]
(gdb) 
answered on Stack Overflow Sep 11, 2020 by ks1322

User contributions licensed under CC BY-SA 3.0