0
\$\begingroup\$

I have three cases, and for each of this, the UART prints different results, though they have to print the same results, as in the Case 1. This was done on TICC3200 LP.

Case 1 : This one prints the right results. I am doing the following

#define SL_IPV4_BYTE(val,index)                  ( (val >> (index*8)) & 0xFF )

IPDeviceScanP Device_Hooter;
UART_PRINT("Test Simulation of Device Health Check and Scan Software\n\r");

Device_Hooter->SecureIP = 0xC0A80390;    //SecureIP is unsigned long data type

UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);
UART_PRINT("%d.",SL_IPV4_BYTE(Device_Hooter->SecureIP, 3));
UART_PRINT("%d.",SL_IPV4_BYTE(Device_Hooter->SecureIP, 2));
UART_PRINT("%d.",SL_IPV4_BYTE(Device_Hooter->SecureIP, 1));
UART_PRINT("%d\r\n",SL_IPV4_BYTE(Device_Hooter->SecureIP, 0));
UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);

Output

Test Simulation of Device Health Check and Scan Software
Simulating Secure IP Address for Testing: c0a80390
192.168.3.144
Simulating Secure IP Address for Testing: c0a80390

Case 2 : This one prints all zero's for some reason.

#define SL_IPV4_BYTE(val,index)                  ( (val >> (index*8)) & 0xFF )

IPDeviceScanP Device_Hooter;
UART_PRINT("Test Simulation of Device Health Check and Scan Software\n\r");

Device_Hooter->SecureIP = 0xC0A80390;

UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);
UART_PRINT("IP Address of SECURE: %d.%d.%d.%d\r\n",     SL_IPV4_BYTE(Device_Hooter->SecureIP, 3),
                                                        SL_IPV4_BYTE(Device_Hooter->SecureIP, 2),
                                                        SL_IPV4_BYTE(Device_Hooter->SecureIP, 1),
                                                        SL_IPV4_BYTE(Device_Hooter->SecureIP, 0));

Output

Test Simulation of Device Health Check and Scan Software
Simulating Secure IP Address for Testing: 0
IP Address of SECURE: 0.0.0.0

Case 3 : This one prints something that doesn't make sense to me.

IPDeviceScanP Device_Hooter;

int ip3, ip2, ip1, ip0;
UART_PRINT("Test Simulation of Device Health Check and Scan Software\n\r");
Device_Hooter->SecureIP = 0xC0A80390;
UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);

ip3 = SL_IPV4_BYTE(Device_Hooter->SecureIP, 3);
ip2 = SL_IPV4_BYTE(Device_Hooter->SecureIP, 2);
ip1 = SL_IPV4_BYTE(Device_Hooter->SecureIP, 1);
ip0 = SL_IPV4_BYTE(Device_Hooter->SecureIP, 0);

UART_PRINT("IP Address matches Gateway or SECURE: %d.%d.%d.%d\r\n",ip3, ip2, ip1,ip0);
UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);

Output:

Test Simulation of Device Health Check and Scan Software
Simulating Secure IP Address for Testing: a0dd00
IP Address matches Gateway or SECURE: 0.160.221.0
Simulating Secure IP Address for Testing: a0dd00

What is the difference in the three cases in terms of the code and content?. And why each of them print results are different?

\$\endgroup\$
  • \$\begingroup\$ I believe this question belongs to software development, i.e. stackoverflow.com and should be moved there. \$\endgroup\$ – user59864 Sep 17 '16 at 8:28
2
\$\begingroup\$

You are de-referencing an uninitialized pointer, which invokes undefined behavior.

This allocates space to store a pointer, but it does not point it to any valid location:

IPDeviceScanP Device_Hooter;

This attempts to store something to an undefined location in memory

Device_Hooter->SecureIP = 0xC0A80390;

That could already potentially crash your program or trip a memory protection fault.

But then you try to read from the undefined location

UART_PRINT("Simulating Secure IP Address for Testing: %x\r\n",Device_Hooter->SecureIP);

To you this appears as the wrong value being read back, but the actual issue is far, far more serious

You need to allocate storage for your struct - presumably an IPDeviceScan and then if you wish you can allocate an IPDeviceScanP and initialize it to point to the location of the structure you allocated.

\$\endgroup\$
  • \$\begingroup\$ So, should I just create a struct variable and a struct pointer. Then point the pointer to the address of the struct variable?. Or allocate memory using malloc for the structure variable and then point the pointer to the address of the memory allocated for struct? \$\endgroup\$ – PsychedGuy Sep 17 '16 at 9:22
  • \$\begingroup\$ I am fairly new to data structures. Pardon me, if I asked something silly. I know that using malloc we can allocate memory, does the same apply with creating a variable of the above type struct? \$\endgroup\$ – PsychedGuy Sep 17 '16 at 9:28
  • \$\begingroup\$ @PsychedGuy - IPDeviceScanP is not a structure, it is a pointer which you haven't pointed somewhere! Just use IPDeviceScan. \$\endgroup\$ – brhans Sep 17 '16 at 13:40
  • \$\begingroup\$ @brhans : You mean something like this - IPDeviceScan HooterDevice; and then HooterDevice.SecureIP = 0xC0A80390; ? \$\endgroup\$ – PsychedGuy Sep 17 '16 at 16:46
  • 1
    \$\begingroup\$ Yes. And then if you need to pass a pointer to it to something else, you can use &HooterDevice \$\endgroup\$ – Chris Stratton Sep 17 '16 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.