0

In a CUDA C project, I have a pointer to float inside a structure called "p". It is a pointer to device memory and it is called "p->deviceOutput". I am using CUDA-GDB to check the content of this pointer during execution: when I try to print it this is what happens:

(gdb) p *p->deviceOutput
$1 = 0

As you can see the printing returns something that looks like an int, definitely not a float. I am really sure the pointer is a pointer to float so I am really confused by this behaviour. Specifying the float format does not help:

(gdb) p/f *p->deviceOutput
$2 = 0

Actually I get the same behaviour using GDB as well. I am working on Ubuntu 14.10 and my code was compiled with nvcc using -O0 and -g options.

Can anybody please explain what is going on and what should I do to inspect this memory location correctly? Thanks

Andrea Crespi
  • 21
  • 1
  • 4

1 Answers1

1

gdb uses the g format specifier to printf (internally) when printing floating point values. Here's the interesting part of the description of g from the man page for printf:

Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

So you're getting 0 because the floating point value is 0, and the g format specifier (used by gdb) prints an exact 0 without a decimal point, or any trailing 0's.

You can check that your variable is of type float using the ptype command, like this:

(gdb) ptype p->deviceOutput
Andrew
  • 3,770
  • 15
  • 22
  • Thanks, this explains the unexpected behaviour. The ptype command returns "type = float *". The printf command returns "0.000000", which confirms your statements. – Andrea Crespi Dec 07 '15 at 12:03