LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2019, 08:14 PM   #1
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Rep: Reputation: 167Reputation: 167
Why the disparity with sizeof result?


I see the sizeof operator in a lot of examples, and the concept seems simple enough and I use it sometimes. I might use it more if I understood this:

Code:
  const int BUF = 40;
  char *string1 = calloc (BUF, sizeof (char));
  /* calloc error checking */
  char string2[BUF];
size of string1 is 8, and the sizeof string2 is 40. Even after I copy a 10 character string into string1, sizeof still reports 8. So basically I resort to strlen() most of the time for error checking.
 
Old 07-30-2019, 08:33 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
sizeof is a compile-time operator; if you pass it a pointer, it will dutifully tell you the size of a pointer. That is 8 bytes if compiling a 64 bit program, 4 bytes for a 32 bit one. If you want to check on the string being pointed-to, strlen is the correct function to use.
 
2 members found this post helpful.
Old 07-30-2019, 10:45 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: sizeof is evaluated compile-time, except for VLAs (variable length arrays). (Explanation: https://tvtropes.org/pmwiki/pmwiki.p...inkThisThrough ).
 
1 members found this post helpful.
Old 07-31-2019, 12:17 AM   #4
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Original Poster
Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by NevemTeve View Post
Note: sizeof is evaluated compile-time, except for VLAs (variable length arrays). (Explanation: https://tvtropes.org/pmwiki/pmwiki.p...inkThisThrough ).
I think that link might need to be changed... but I found some info about VLAs, thanks.
 
Old 07-31-2019, 08:21 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
I believe the C standard say that sizeof (char) is always 1, so this particular use of sizeof isn't that useful.

char *my_pointer = calloc(40, sizeof *my_pointer); is probably better as it will still work if you change the type to something other than char. i.e. you get enough storage allocated for 40 of whatever type *my_pointer is declared as. (comes into its own when used for arrays of structs)

Last edited by GazL; 07-31-2019 at 08:23 AM.
 
2 members found this post helpful.
Old 07-31-2019, 02:08 PM   #6
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Original Poster
Rep: Reputation: 167Reputation: 167
Thanks for the additional tips
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
openoffice page disparity - windows v/s centos bittus Linux - Software 2 06-26-2011 11:03 PM
Htop vs 'top' vs 'free' disparity schachwizard Ubuntu 2 09-22-2010 04:33 PM
UN reveals global disparity in broadband access Jeebizz Linux - News 0 09-02-2010 10:16 AM
Debugging slow tape: Big disparity between iostat and tape speed wsanders Linux - Server 2 05-23-2009 09:02 AM
Huge speed disparity between windows/linux (bad for linux) Errsta_Fonzarelli Linux - Networking 13 03-05-2006 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration