LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, Debian64 stable, LFS 7.1
Posts: 490

Rep: Reputation: 161Reputation: 161
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,494

Rep: Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789Reputation: 1789
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: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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, Debian64 stable, LFS 7.1
Posts: 490

Original Poster
Rep: Reputation: 161Reputation: 161
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 Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
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, Debian64 stable, LFS 7.1
Posts: 490

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:26 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration