473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Please explain why sizeof behaves like this?

Hi All

#include <stdio.h>
#include <string.h>

#define SYSTEM_TABLE "SYSIBM"

int main()
{
printf("size is %d %d\n", sizeof(SYSTEM_TABLE - 1), (sizeof(SYSTEM_TABLE) - 1) );
}

Output

size is 4 6

I can understand the reason for the seconds value i.e. 6.

Please explain the reason for the first value i.e. 4. I guess some type conversion happens there. But I am not sure.

Thanks & Regards
Sathish Kumar
Jun 3 '08 #1
5 1162
I guess that this 4 is the size of the pointer to your literal "SYSIBM", minus one memory position (assuming 32-bit arch).
Jun 3 '08 #2
Laharl
849 Expert 512MB
That's correct. On a 32-bit architecture, all memory addresses are 32-bit and thus are 4 bytes. Therefore, sizeof will return 4 for any pointer.
Jun 3 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Actually, Visual Studio.NET gets 7 and 6.

This code:
sizeof(SYSTEM_TABLE - 1)
calculates the size of the object on the stack. In this case SYSTEM_TABLE is 7 (6 for SYSIBM plus 1 for the null terminator. The calculation SYSTEM_TABLE-1 is pointless. It subtracts 1 from the address of "SYSIBM" and this does not affect sizeof.

Now this code:
sizeof(SYSTEM_TABLE) - 1
calculates the size of the array on the stack, which is 7 and then subtracts 1 to get 6.

If anyone is getting 4, then your compiler is DOA.

True, sizeof any pointer is 4 but sizeof always returns the size of the variable on the stack. So, when SYSTEM_TABLE is passed to a function, then the address is passed and a sizeof the function argument from inside the function will return 4.
Jun 3 '08 #4
If anyone is getting 4, then your compiler is DOA.
GCC 3.4.4 with defaults gives 4

What does DOA mean?
Jun 3 '08 #5
Laharl
849 Expert 512MB
It means "Dead on arrival".
Jun 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: REH | last post by:
Someone more articulate than me please explain to Ioannis Vranos why the following two programs both exhibit undefined behavior: > #include <iostream> > #include <cstring> > > class SomeClass...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
11
by: Sathyaish | last post by:
I had recieved an email sometime ago wherein I was asked to write a function that parsed a given string for the longest pallindrome it contained and replaced the pallindrome with another string,...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
3
by: Aarti | last post by:
Hi, Can some one please explain why the output of this program is 15 #include <iostream> using namespace std; class A {
12
by: raghukumar | last post by:
# include <iostream> class A { public: A() : i(1) {} int i; } ; class B: public A { public :
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a));...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.