GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2012, 05:32
sumanish sumanish is offline
New Member
 
Join Date: May 2012
Posts: 3
sumanish is on a distinguished road

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  
Old 11-May-2012, 12:20
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,004
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: If there is no sizeof operator in C


In C we have the sizeof() operator that works same as a function.
See if this example of pointer math sheds some light.
Feel free to ask more questions about what you don't understand about it.
CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
    char c;
    int x;
    int y[4] = { 11, 22, 33, 44 };
    int *int_p = NULL;

    printf("--- 1 --- \nsizeof c= %lu, sizeof x= %lu, sizeof y= %lu, sizeof y[0]= %lu \n",
            sizeof(c), sizeof(x), sizeof(y), sizeof(y[0]) );

    /* pointer arithmetic - 
     * since int_p is delared an "int *" the program "knows" to move int_p
     * sizeof(int) bytes at each ++ or -- and int_p+=1 or int_p-=1 
     */

    int_p = y;    
    printf("--- 2 ---\nvalue at int_p = %d , value at (int_p + 2) = %d, \n", *int_p, *(int_p + 2) );

    printf("--- 3 --- \n");
    for (x = 0; x < (int)(sizeof(y) / sizeof(y[0]) ); x++)
    {
      printf("value at (int_p + %d) = %d \n", x, *int_p );
      int_p++;
    }
    int_p -= (sizeof(y) / sizeof(y[0]));

    printf("--- 4 ---\nvalue at int_p = %d \n", *int_p );

  return 0;
}
output:
Code:
> gcc -Wall -W -pedantic pointermath.c -o pointermath.bin > ./pointermath.bin --- 1 --- sizeof c= 1, sizeof x= 4, sizeof y= 16, sizeof y[0]= 4 --- 2 --- value at int_p = 11 , value at (int_p + 2) = 33, --- 3 --- value at (int_p + 0) = 11 value at (int_p + 1) = 22 value at (int_p + 2) = 33 value at (int_p + 3) = 44 --- 4 --- value at int_p = 11
  #3  
Old 11-May-2012, 21:49
sumanish sumanish is offline
New Member
 
Join Date: May 2012
Posts: 3
sumanish is on a distinguished road

Re: If there is no sizeof operator in C


I can understand the code and pointer arithmetic.......but my question is very specific to the compiler....

As you have mentioned....
Code:
>>/* pointer arithmetic - >> * since int_p is delared an "int *" the program "knows" to move int_p >> * sizeof(int) bytes at each ++ or -- and int_p+=1 or int_p-=1 >> */

but want to know if there is no sizeof operator in C then does it move 'int_p' sizeof(int) bytes ?????

How it will know the size of the 'int' ????
  #4  
Old 12-May-2012, 22:56
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,004
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: If there is no sizeof operator in C


Quote:
How it will know the size of the 'int' ????
The same way it knows how to read sizeof(int) bytes to determine value.
Magic!!!

int a= 256;
printf("%d \n", a;

wow!!!
  #5  
Old 13-May-2012, 11:35
davekw7x davekw7x is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: If there is no sizeof operator in C


Quote:
Originally Posted by sumanish
...but want to know if there is no sizeof operator in C...

Why do you keep saying that there is no sizeof operator in C? Of course there is a sizeof operator in C. There has always been a sizeof operator in C. Howard posted a C program that uses the sizeof operator. (Did you try it?) What the heck are you talking about?

But wait a minute! Are you saying that your C compiler won't compile a program that uses the sizeof operator?

If so, then please tell us what compiler you are using, and please post the code. I gotta see it to believe it! (See Footnote.)

Also, I would be interested in knowing what reference material you are learning from:
  • Class notes? Tell us the title of the course, and tell us what are the prerequisites (if any).
  • Textbook? Tell us the name of the book.
  • On-line tutorial? Give a link.
  • What?


Regards,

Dave

Footnote:
All standard C compilers have the sizeof operator. If there are any (non-standard) C compilers out that that don't have or recognize the sizeof operator, I would like to hear about them. Really.

However...

It is not true that all compilers have the same size of int variables. For some implementations, sizeof(int) may yield 4. For some it may yield 2. For some it may yield 8. (There may even be compilers that yield values other than these. Don't know; don't care.)

In fact, that's one of the really good reasons that all C compilers need (and have) a sizeof operator. (That is: Sometimes a program must know the size of an int and act accordingly.)

That may not seem important as you are reading introductory material, but you may find that it can come in handy time after time (after time) if/when you get to the Good Stuff of actually writing programs that are a little beyond Hello World!.

I mean, I know that, in the beginning of the learning process, there is a certain amount of terminology and technical information that is introduced without much user motivation. (Like hinting at where you would ever use such a thing.) Pedagogically good material will show some meaningful usage as new concepts are revealed, but sometimes it is still not apparent to the beginner why such a thing is there. That's why I asked about the source of your learning material.
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 13-May-2012 at 13:00.
  #6  
Old 13-May-2012, 22:13
sumanish sumanish is offline
New Member
 
Join Date: May 2012
Posts: 3
sumanish is on a distinguished road

Re: If there is no sizeof operator in C


I don't think the discussion is going in correct direction. I know all C compilers have sizeof operator.
I have asked if there is no sizeof operator then what will happen.

Think about a question..... If there is no sizeof operator how will you compute the size of int or any other datatype ?

One simple answer is just take a pointer to that datatype and increment it once and take the difference with the previous value.

But I want to know if there is no sizeof operator how the system will know how much to increment ???????
  #7  
Old 14-May-2012, 09:08
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,004
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: If there is no sizeof operator in C


Also
Quote:
I want to know if there is no sizeof operator how the system will know how much to increment ?
oh, a hypothetical question.
First I must say that I don't really know exactly but my perception is this:

The sizeof operator is something "WE" use in our C code to get the size of an object for use in our program.
I BELIEVE
The compiler doesn't "use" the sizeof() operator other than when it comes to it in our code it is replaced by a value.
Where does this value come from?
It is determined earlier in our code at declaration.
When we say "int x;" an address is set up to store the int.
The label "x" goes on a "list" along with that address and size of "int" on the target machine.
When we write sizeof(x) that value is used in it's place.

Quote:
One simple answer is just take a pointer to that datatype and increment it once and take the difference with the previous value.
Maybe so but the pointer would have to be defined to the proper size like
int x;
int * px = &x;
Last edited by Howard_L : 14-May-2012 at 09:48.
  #8  
Old 14-May-2012, 09:22
davekw7x davekw7x is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: If there is no sizeof operator in C


Quote:
Originally Posted by sumanish
I don't think the discussion is going in correct direction.
Well, I certainly did not understand the question. I am sorry that I misread the question and its intent.

First of all, the sizeof operator is there to let a program know the size of "something." The C compiler knows the size of everything, regardless of the existence of any user operator. It has to. Obviously. I mean, it does calculations and storage access (and everything else that it needs to do) with whatever internal representation the compiler implementers decided to use. It does not require the existence of any particular user-accessible operator to do its work internally.

On the other hand...

Now, there are many situations for which a program has to be able to know the size of "something," and that is what the sizeof operator is for.
Quote:
Originally Posted by sumanish
One simple answer is just take a pointer to that datatype and increment it once and take the difference with the previous value.
I am assuming that you want the program to be able to determine what the size is, not just a human reading out the numerical byte address values from a print statement. Maybe that's another incorrect assumption on my part, but I think the subject is worth a little more investigation.

Well...

Incrementing the pointer and attempting to determine the size by calculating the difference between new and old values won't work unless you cast the pointer to an integer data type. This is not portable, since there is no guarantee that casting a pointer to an integer won't lose information.

I mean, if you take a pointer value and increment that value (using pointer arithmetic), and then you subtract the old value from the new value (using pointer arithmetic), you always get 1.

On the other hand...

If you cast the new and old pointer values to integer data types the program may be able to tell the difference (in bytes) by subtracting the integer values (using integer arithmetic). (However: See Footnote.)


Here is an example that may work, but there are no guarantees:

CPP / C++ / C Code:
/* Non-portable example of a way to try to determine
   the size of "something" without using the sizeof
   operator.
*/

typedef struct {
    char cx;
    int  iy;
    int iz;
} s_t;

#include <stdio.h>
    
void printinfo(void);
int main()
{

    /* A struct of type s_t and two pointers */
    s_t s;
    s_t *sp1;
    s_t *sp2;

    /* An int variable and two pointers */
    int x;
    int *ip1;
    int *ip2;

    /* Variables to hold results of the arithmetic */
    int pdiff, idiff;
    unsigned long i1, i2;

    printinfo();


    /*
       Use pointer arithmetic and integer arithmetic on
       pointers cast to ints
    */
    ip1 = &x;
    ip2 = ip1 + 1;

    pdiff = ip2-ip1;

    i1 = (unsigned long)ip1;
    i2 = (unsigned long)ip2;
    idiff = i2-i1;
    
    printf("Look at size of an int variable:\n");
    printf("  Using pointer arithmetic\n");
    printf("      ip1 = %p, ip2 = %p, difference = %d\n\n", (void *)ip1, (void *)ip2, pdiff);
    printf("  Casting pointers to ints and using integer arithmetic\n");
    printf("      i1  = 0x%lx, i2  = 0x%lx, difference = %d\n\n", i1, i2, idiff);

    /*
       Use pointer arithmetic and integer arithmetic on
       struct pointers cast to ints
    */
    sp1 = &s;
    sp2 = sp1 + 1;
    i1 = (unsigned long)sp1;
    i2 = (unsigned long)sp2;
    pdiff = sp2-sp1;
    idiff = i2-i1;
    printf("Look at size of an struct of type s_t:\n");
    printf("  Using pointer arithmetic\n");
    printf("      sp1 = %p, sp2 = %p, difference = %d\n\n", (void *)sp1, (void *)sp2, pdiff);
    printf("  Casting pointers to ints and using integer arithmetic\n");
    printf("      i1  = 0x%lx, i2  = 0x%lx, difference = %d\n\n", i1, i2, idiff);

    return 0;
}

void printinfo()
{
    printf("Trying to calculate the size of stuff without using the sizeof operator\n");
    printf("requires casting pointers to integer data types.  This is not\n");
    printf("portable, since there is no guarantee that an arbitrary pointer\n");
    printf("can be cast to any particular kind of integer data type without\n");
    printf("losing information.  I'll use long integers, and it may seem to\n");
    printf("\"work\", but there is still no guarantee...\n\n");
    printf("  davekw7x\n\n\n");
}

Output (Compiled with 32-bit GNU gcc):
Code:
Trying to find size of stuff without using the sizeof operator requires casting pointers to integer data types. This is not portable, since there is no guarantee that an arbitrary pointer can be cast to any particular kind of integer data type without losing information. I'll use long integers, and it may seem to "work", but there is still no guarantee... davekw7x Look at size of an int variable: Using pointer arithmetic ip1 = 0xbfdbb244, ip2 = 0xbfdbb248, difference = 1 Casting pointers to ints and using integer arithmetic i1 = 0xbfdbb244, i2 = 0xbfdbb248, difference = 4 Look at size of an struct of type s_t: Using pointer arithmetic sp1 = 0xbfdbb248, sp2 = 0xbfdbb254, difference = 1 Casting pointers to ints and using integer arithmetic i1 = 0xbfdbb248, i2 = 0xbfdbb254, difference = 12

Note that instead of casting, you could, conceivably, use sprintf-scanf to convert the pointer values to ints, but there is still no guarantee that an arbitrary pointer value can be converted to any particular integer data type without losing information.

A final comment: Instead of casting pointers to integer data types, you might try casting them to pointers to char. It is guaranteed that sizeof(char) is equal to 1, so that doing pointer arithmetic with pointers to char might seem to be more portable. I'm not sure how C compilers are implemented on machines for which there is no byte-addressable memory (or even if it's worth trying such a thing) and how it relates to pointers to different data types. "Probably more portable" seems to be not particularly useful in the grand scheme of things.

Bottom line: The sizeof operator is a portable way that is guaranteed always to return the correct size of "stuff" in C. It is easy to use and it is efficient, and, most importantly, it is portable.


Regards,

Dave

Footnote:
Beware:
There is a lot (a lot) of legacy code out there that assumes a pointer has the same length as an int, but that is simply not always true. For 64-bit GNU compilers, pointers occupy 8 bytes but ints occupy 4 bytes. I have run across a lot (a lot) of legacy code that behaved "properly" on 32-bit systems but failed miserably when the code was recompiled with a 64-bit compiler.
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 14-May-2012 at 10:48.
  #9  
Old 14-May-2012, 10:52
davekw7x davekw7x is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,148
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: If there is no sizeof operator in C


Quote:
Originally Posted by Howard_L
...
Quote:
Originally Posted by sumanish
if there is no sizeof operator
First I must say that I don't really know exactly


Like me, maybe you are not sure what the meaning of 'is' is. (See Footnote.)

The answer is, "It depends."


Regards,

Dave

"It depends on what the meaning of the words 'is' is."
---U.S. President William Jefferson (Bill) Clinton, during his 1998 grand jury testimony on the Monica Lewinsky affair.
__________________
Sometimes I just can't help myself...
 
 

Recent GIDBlogConfiguring iptables for Webmin Servers Index Module by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:13.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.