Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Compiler/Runtime uses of sizeof

Programming


Tags
solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 01-22-2016   -   Original Discussion by GSalisbury
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline
Registered User
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 Posts

Code:
char a[8];
c = sizeof(a);
strncpy( a, b, c );

If optimization is enabled, GCC will optimize away the second line of this code.
Sponsored Links
    #9  
Old Unix and Linux 01-27-2016   -   Original Discussion by GSalisbury
GSalisbury's Unix or Linux Image
GSalisbury GSalisbury is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 14 December 2017, 12:57 PM EST
Location: New Jersey
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks - that's pretty much what the takeaway has been.

In the code blocks here that gave rise to the conversation the sizeof was expressed in the using statements and not set as separate variables.

It seems that, for all practical purposes, the compiler treats sizeof as a placeholder for substituting the literal value of the size of the referenced item. That's good enough and is as far we need take it

Thanks again.
Geo. Salisbury
Long Valley, NJ
Sponsored Links
    #10  
Old Unix and Linux 02-01-2016   -   Original Discussion by GSalisbury
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 14 December 2017, 4:58 PM EST
Location: Saskatchewan
Posts: 22,547
Thanks: 1,159
Thanked 4,287 Times in 3,955 Posts
Quote:
Originally Posted by GSalisbury View Post
It seems that, for all practical purposes, the compiler treats sizeof as a placeholder for substituting the literal value of the size of the referenced item. That's good enough and is as far we need take it
It hardwires it at compile time. As such, it gives a completely literal, unchanging result.
    #11  
Old Unix and Linux 02-01-2016   -   Original Discussion by GSalisbury
GSalisbury's Unix or Linux Image
GSalisbury GSalisbury is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 14 December 2017, 12:57 PM EST
Location: New Jersey
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for re-stating it is a literal value.
That's what had been indicated thoughout the last couple of posts.
Done (again)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sizeof(object) in C++ ramkrix Programming 2 10-20-2010 05:04 PM
Doubts regarding sizeof() operator royalibrahim Programming 5 07-21-2010 05:58 PM
How to get the sizeof char pointer SamRoj Programming 3 06-04-2009 03:58 AM
sizeof an array of structure without using 'sizeof' operator rvan Programming 18 04-01-2009 05:55 PM
sizeof ramneek Programming 7 10-13-2005 08:22 AM



All times are GMT -4. The time now is 08:07 AM.