Results 1 to 10 of 10
  1. #1
    Right Off the Assembly Line
    Join Date
    May 2012
    Posts
    1

    Default If there is no sizeof operator in C

    I do not have much knowledge about compilers and all.

    I have one simple question about C programming.

    If there is no sizeof operator in C then does those pointer arithmetic work or not ?

    I am very much confused about it.

    Please somebody help.

  2. #2
    Master KOD3R nbaztec's Avatar
    Join Date
    Sep 2010
    Location
    New Delhi
    Posts
    342

    Default Re: If there is no sizeof operator in C

    sizeof operator is present in C.
    AMD Phenom II X6 1055T|MSI 880GMA-E45|MSI 7770|2x2 Corsair DDR3 1333MHz|CM Elite 310|Corsair CX400W|DELL ST2320L
    Myself @ nbaztec.co.in
    Build Your Rig! (XBlade) @ nbaztec.co.in - Build Your Rig!
    My Inner Artist @ nbaztec.co.in - Designs

  3. #3
    making future better Vyom's Avatar
    Join Date
    May 2009
    Location
    New Delhi
    Posts
    3,177

    Default Re: If there is no sizeof operator in C

    Quote Originally Posted by sumanish View Post
    If there is no sizeof operator in C then does those pointer arithmetic work or not ?
    I think you need to read some books. Some good books. Non-Indian authors.

  4. #4
    Project Halcyon V2.1 tkin's Avatar
    Join Date
    Aug 2008
    Location
    Hyderabad, India
    Posts
    9,160

    Default Re: If there is no sizeof operator in C

    I learned about this operator when learning C, when doing dynamic memory allocation, read up on malloc, calloc.
    Project Halcyon V2.1
    2600k/Z68VPro/Vengeance/U12PSE2/TX750v2/1TB.Black/DRWB5ST/HAF912+A
    M35/PortaPro/E30/Optimus.L9/Siberia/5800XM/FZ150/SH14/FiiO.E6
    Dell.15R/Ci3.3110m/7670m/ALVS4621/Funbook/Ferox


  5. #5
    Sami Hyypiä, LFC legend Liverpool_fan's Avatar
    Join Date
    Jun 2007
    Location
    Нью-Дели
    Posts
    2,219

    Default Re: If there is no sizeof operator in C

    Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

    Spoiler:
    Read before asking / messaging any moderator for any query: FAQ + answers for new members

    Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
    Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.

  6. #6
    Right Off the Assembly Line
    Join Date
    Jun 2012
    Posts
    1

    Default Re: If there is no sizeof operator in C

    yes there is ....if u r having probs using calloc,malloc etc maybe u forgot to use type casting..

  7. #7
    Broken In audiophilic's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    181

    Default Re: If there is no sizeof operator in C

    It will greatly depend on the compiler you have

  8. #8
    In The Zone rijinpk1's Avatar
    Join Date
    Apr 2012
    Location
    God's own contry
    Posts
    261

    Default Re: If there is no sizeof operator in C

    sizeof() is put by the compiler itself when running pointer arithmatic. Without that, pointer arithmatic wont takeplace. That is what I understand as of now.

  9. #9
    poor little me
    Join Date
    Jul 2012
    Location
    internet
    Posts
    77

    Default Re: If there is no sizeof operator in C

    hii there i learned this trick back in my college days to find the size of a variable without the use of sizeof() operator .


    #define find_size(x) (((char *)(&x+1))-((char *)(&x)))

    int main(){
    int i;
    char c;
    double d;
    printf("%d\n",find_size(i));
    printf("%d\n",find_size(c));
    printf("%d",find_size(d));
    return 0;
    }
    It works in almost all cases .Although sizeof () is a operator i think in assembly code it comes down to some piece of more basic code .

    don't ask how to find size() if we don't have # define
    That i don't know

  10. #10
    Master KOD3R nbaztec's Avatar
    Join Date
    Sep 2010
    Location
    New Delhi
    Posts
    342

    Default Re: If there is no sizeof operator in C

    Quote Originally Posted by krazylearner View Post
    hii there i learned this trick back in my college days to find the size of a variable without the use of sizeof() operator .



    It works in almost all cases .Although sizeof () is a operator i think in assembly code it comes down to some piece of more basic code .

    don't ask how to find size() if we don't have # define
    That i don't know
    As cool and hacky as the trick may sound, it's bad code. And not because it does what sizeof() does, but because it is misleading and counter-intuitive. One should take some time out to understand what exactly is happening:

    A pointer is nothing but a location to memory, and on a x86 systems it holds a 32-bit address. So effectively it is of type long (4 bytes) = int on x86 architecture.
    Code:
            int i = 1337;
    	printf("i(%d) is located at %d, next location is %d\n", i, &i, &i+1); 
    	printf("Number of memory locations: %d - %d = %d\n", &i+1, &i, &i+1 - &i);
    	printf("Number of bytes           : %d - %d = %d\n", &i+1, &i, (long)(&i+1) - (long)&i);
    
         /**
            * Output
    	* i(1337) is located at 1834544, next location is 1834548
    	* Number of memory locations: 1834548 - 1834544 = 1
    	* Number of bytes           : 1834548 - 1834544 = 4
            */
    So without typecasting, the difference between 2 pointers yields the no. of elements or memory locations. But once you typecast the pointer to a long, it is treated as a interger type memory location upon which you can perform the usual arithmetic operations. And what better way to get the number of bytes that to get the difference between 2 consecutive addresses.

    So you can ditch that hacky macro, which I'm sure originated from some Indian authored book.
    AMD Phenom II X6 1055T|MSI 880GMA-E45|MSI 7770|2x2 Corsair DDR3 1333MHz|CM Elite 310|Corsair CX400W|DELL ST2320L
    Myself @ nbaztec.co.in
    Build Your Rig! (XBlade) @ nbaztec.co.in - Build Your Rig!
    My Inner Artist @ nbaztec.co.in - Designs

Similar Threads

  1. Which network operator in Delhi?
    By Micheal in forum Mobiles and Tablets
    Replies: 10
    Last Post: 14-04-2012, 06:08 PM
  2. Mobile Operator ... which is yours ???
    By ax3 in forum Mobiles and Tablets
    Replies: 58
    Last Post: 13-04-2012, 11:17 PM
  3. Fwd: What is Operator Overloading!
    By mrintech in forum Chit-Chat
    Replies: 8
    Last Post: 24-04-2009, 05:08 PM
  4. Operator logo
    By vandit in forum Mobiles and Tablets
    Replies: 3
    Last Post: 16-04-2007, 11:29 PM
  5. Operator Logo Help!
    By REY619 in forum Mobiles and Tablets
    Replies: 13
    Last Post: 15-12-2006, 01:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Close