Click here to Skip to main content
12,241,564 members (55,298 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C VisualC++
I have custom type called cell and when i try to convert from int to cell i got this strange results:
typedef long int  int32_t;
typedef int32_t   cell;
 
cell foo()
{
 int xz;
 xz = 150;
 return xz;
}

Expected: 150

Output: 32736 <- why ??? and not 150 as expected.

I hope you will understand this.
Posted 11-Apr-13 10:20am
Edited 11-Apr-13 10:23am
v2
Comments
CPallini 11-Apr-13 15:32pm
   
Your code does not produce output, how do you check it?
125Azazelo 11-Apr-13 15:39pm
   
Outside this code. I use print(). And i know it shod be conversion between type something like
return (cell)xz; but i do not know why this bug heaped. Long int shod be large inapt for int to be stored in whit out conversion.
Sergey Alexandrovich Kryukov 11-Apr-13 16:04pm
   
Please, exact code sample, up to the point you got 32736. Use "improve question".
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

No, it should return 150 (unless you also redefined int and long int in some weird way Smile | :) ).

(By the way, hot did you obtain this output? You did not show the code calling foo(), the problem could be there…)

—SA
  Permalink  
Comments
125Azazelo 11-Apr-13 16:01pm
   
Yes i was made a strange define whit int. Thank you for point this.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The following code

typedef long int  int32_t;
typedef int32_t   cell;
#include <stdio.h>
cell foo()
{
 int xz;
 xz = 150;
 return xz;
}
 
int main()
{
  printf("%ld\n", (long)foo());  /* long int ! */
  return 0;
}

outputs:
150
  Permalink  
v2
Comments
125Azazelo 11-Apr-13 16:02pm
   
Thank you for POC i think i got out of my mind.
Sergey Alexandrovich Kryukov 11-Apr-13 16:04pm
   
Sure, a 5, for more complete and definitive sample. However, I have no idea what could OP do to obtain 32736, do you (I see that you asked OP)?
—SA
CPallini 11-Apr-13 16:26pm
   
I have no idea (though 32736 is interesting).
BTW Thank you.
Sergey Alexandrovich Kryukov 11-Apr-13 17:21pm
   
:-)
CPallini 12-Apr-13 4:07am
   
@merano: thank you for fixing the mistake.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100