Thread: datatype deduction

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    datatype deduction

    Without the prior knowledge that integers store 2 byte in C,or char store 4 byte,float reserves 'so and so bytes',...etc
    Is it possible to deduce directly from a C program which datatype store what amount of free memory?

    I tried this code but it was without any results.
    (As array of any datatype stores consecutive memory locations I declared )
    Code:
    int a[2];
    char b[2];
    float c[2];
    (Then I substracted the adress and printed )

    Code:
    printf("For integer data type : Bytes stored = %d",(&a[1])-(&a[0]));
    The result should come 4 but it was in vain,it was showing 1 as output which is wrong.....

    In case of deduction of other datatype also ,I went on with the same logic ,but the result in each case was erronious

    Please help,
    Refer if some shorter methods available...

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Use sizeof to see the size of a data type
    Code:
    int i;
    float f;
    printf("%d %d", sizeof(i), sizeof(f));
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    printf("Size of integer in bytes = %d\n",sizeof(int));

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by progmateur
    Without the prior knowledge that integers store 2 byte in C,or char store 4 byte,float reserves 'so and so bytes',...etc
    Is it possible to deduce directly from a C program which datatype store what amount of free memory?
    Use the sizeof operator.

    Quote Originally Posted by progmateur
    The result should come 4 but it was in vain,it was showing 1 as output which is wrong.....
    You're subtracting pointers, so of course the difference is 1 since the elements are adjacent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    But in memory loc ,it skips 4 bytes for the adjascent location.
    I was differentiating the location adress !!
    It should show 4!

    When I was printing like this:
    printf("%x %x",&a[1],&a[0]);

    The output was "FFFE FFFA"

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    without using any inbuilt functions .....please!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by progmateur
    But in memory loc ,it skips 4 bytes for the adjascent location.
    I was differentiating the location adress !!
    It should show 4!
    That is how subtraction of addresses work, but not how subtraction of pointers work. That said, there is a way to get your idea to work: you need to subtract pointers of a different type.

    Quote Originally Posted by progmateur
    without using any inbuilt functions .....please!
    Use the sizeof operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    If I store the pointer adress to another variable,yet there was an error.
    Please provide me the soln without sizeof function!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by progmateur
    If I store the pointer adress to another variable,yet there was an error.
    What did you try?

    Quote Originally Posted by progmateur
    Please provide me the soln without sizeof function!
    But sizeof isn't a function!

    By the way, why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Please Hold on Ill be posting the code in a minute!......

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Smells like a (badly written) homework assignment to me....

    I also distinctly remember this exact problem on this board about a month or two ago.

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Quote Originally Posted by laserlight View Post
    What did you try?


    But sizeof isn't a function!

    By the way, why do you want to do this?
    Code:
    #include <stdio.h>
    
    main()
    {
    int *a,d,e;
    char *b;
    float *c;
    d=&a;
    a++;
    e=&a;
    printf("%d",e-d);
    getch();
    return 0;
    }

    There was a nonportable pointer conversion warning!
    Please debug it!
    It was on in one of our assignments,
    Thats why I wanted to do it
    There It wasnt mentioned to deduce the datatype programatically
    but Im trying just the same

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't do this: your pointers do not point to anything, so you end up with undefined behaviour. Note that d and e are ints, not pointers to int.

    If you insist on the idea of using pointer subtraction, then I suggest that you focus on:
    Code:
    printf("For integer data type : Bytes stored = %d",(&a[1])-(&a[0]));
    Think: what type of pointers must you subtract in order for the subtraction to compute the difference as number of bytes rather than number of ints between the two pointers?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    frankly speaking,I have no clue about that
    Thats why I posted it in the forum

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sigh. Cast the pointers to char*. It is guaranteed that sizeof(char) == 1, so when you cast the pointers to char*, the subtraction will give the number of bytes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Test datatype
    By tsmith94 in forum C Programming
    Replies: 2
    Last Post: 11-27-2011, 08:56 PM
  2. Templates: type deduction question
    By Elysia in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2010, 04:28 PM
  3. datatype bit?
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 02-05-2009, 01:56 PM
  4. Datatype
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 08-04-2008, 12:53 PM
  5. Implicit template argument deduction for classes
    By drrngrvy in forum C++ Programming
    Replies: 12
    Last Post: 03-30-2007, 05:31 PM

Tags for this Thread