Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C VC++
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 at 11-Apr-13 15:32pm
   
Your code does not produce output, how do you check it?
125Azazelo at 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 at 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 at 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 at 11-Apr-13 16:02pm
   
Thank you for POC i think i got out of my mind.
Sergey Alexandrovich Kryukov at 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 at 11-Apr-13 16:26pm
   
I have no idea (though 32736 is interesting).
BTW Thank you.
Sergey Alexandrovich Kryukov at 11-Apr-13 17:21pm
   
:-)
CPallini at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
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