473,378 Members | 1,368 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,378 software developers and data experts.

need clarification regarding the array

Let's say the memory starts allocating from 1000.

main()
{
int a[]={1,2,3,4,5};

printf("sizeof(&a)=%d sizeof(a)=%d sizeof(*a)=%d sizeof(a[0])=%d\n",sizeof(&a),sizeof(a),sizeof(*a),sizeof(a[0]));

printf("a=%u &a+1 =%u a+1=%u *a+1=%u \n",a,(&a)+1,a+1,*a+0);
}

the output of this program is

sizeof(&a)=4 sizeof(a)=20 sizeof(*a)=4 sizeof(a[0])=4
a=1000 &a+1 =1020 a+1=1004 *a+1=1

This program was compiled and run under Linux.

I am a little confused with this output and my question is:
if sizeof(a)= 20 then a+1 should have been 1020.but program is giving me 1004.
similarly if the sizeof(&a)=4 then &a+1should have been 1004 but the program is giving me 1020.

because incrementing any pointer increments exactly with the size of that pointer.

Can anyone please help me out for understanding this situation or am I making any wrong assumption here.



shouldn't it?
Jun 26 '07 #1
3 1216
weaknessforcats
9,208 Expert Mod 8TB
Yes, the sizeopf a struct is not necessarily the sum of the sizes of its members. This is due to alignment. The C/C++ standard requires int to be aligned on a word boundary and a double on a double word boundary. Word here means processor word and is usually the size of the processor register.

So, for a 32-bit OS, the int has to be on a 4-byte boudary and a double on an 8-byte boundary. This all has to do with how many operations are required to get the int into a register. The answer is one if the int is aligned on a 4-byte boudary. An it is two if it isn't. Thanslate that to way slower.

If your struct member are:
Expand|Select|Wrap|Line Numbers
  1. struct MyStuff
  2. {
  3.    int a;
  4.    char b;
  5.    int c;
  6. };
  7.  
the compiler will add 3 pad bytes after b so that c is on a 4-byte boundary.

Unless you have real issues, use sizeof the struct.

The compiler factors in the pad bytes when computing addresses so you normally don't have to worry about this.
Jun 26 '07 #2
Hi,

Just to remind you, when you write like
int a[] = {1,2,3,4,5}
then a is no longer an integer, a is a pointer to integer and in this case you allocated space for it (through initialization) which is 20 bytes (4 bytes for each number and 4*5=20). So, sizeof(a) will always return you 20 bytes, with 5 integers in this space. Where as sizeof(a[0]) returns 4 (size of an integer).

Now, if tyou increment a (a+1), then you are mistaken here assuming that it will increment by its own size. "a" is an integer pointer (it can point to or store integer(s)) and hence it always will increment by the size of integer (thats 4 bytes), not by the size of itself. Hence, a+1 is (1000+size of integer that is 4) = 1004.

Hope it answers your question.

Thanks,
Sorower
Jun 26 '07 #3
Thanls for your reply

So what about the &a and &a+1?What type of variable these are and why incrementing one it increases to 20.

Thanks & Regards,
Parveen
Jun 27 '07 #4

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
1
by: Roy J | last post by:
Hello everyone:) My name is Roy, I am new to Java and I have problem regarding to arrays. if you have time and like to help, please help. I am trying to make a program to deal with prime...
1
by: Anantha Narayanan Vijayaraghavan | last post by:
Hi Guys, I need a clarification regarding Global.asax file and webconfig file. When I was working in ASP I used to give my database connection in Global.asa. Now in .Net we have two file...
7
by: Rakesh | last post by:
Hi, I was writing this C++ program wherein I used an include statement like - #include <iostream.h> I was told by my co-worker that this form of including a file is deprecated and should...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
11
by: Newbie | last post by:
Can u tell me more abt how did the array a got converted into string a...!! To this question some-one answered : No, it won't convert into string object. Here the representation of string is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.