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

size of dynamic array

Expand|Select|Wrap|Line Numbers
  1. int* x=new int[10];
  2. cout << (sizeof(x)/sizeof(int)) << endl;
  3. x[0]=10;
  4. x[1]=20;
  5. x[2]=30;
  6. x[3]=40;
  7. cout << x[0] << endl;
  8. cout << x[1] << endl;
  9. cout << x[2] << endl;
  10. cout << x[3] << endl;
  11. cout << x[4] << endl;
  12. cout << (sizeof(x)/sizeof(int)) << endl;
Why can't i get the number of elements in this array?
Mar 30 '08 #1
8 2335
Laharl
849 Expert 512MB
sizeof, when used on a pointer, returns the size of the pointer. The sizeof of a pointer is the number of bytes in one 'word' of the computer, 4 for a 32bit machine. An int is also 4 bytes on a 32bit machine, so that will print out 1 everytime on nearly every computer.
Mar 30 '08 #2
Ganon11
3,652 Expert 2GB
The convenient, simple, and most often used method for figuring out the size of an array is...not to figure it out. Make it a separate variable, or if it's a function, require an additional argument indicating the size. Especially for the function, if someone gives you the wrong size, well, that's their own fault.
Mar 30 '08 #3
Expand|Select|Wrap|Line Numbers
  1. cout << x[4] << endl;      // within the max boundary of array, but not initialized
  2. cout << x[10] << endl;     // beyond the max boundary of array
  3. cout << x[11] << endl;
  4. cout << x[12] << endl;
if i print the above three lines in the above situations, what numbers will be printed out? are the numbers meaningful?
Mar 30 '08 #4
Ganon11
3,652 Expert 2GB
No one knows. When you create an array, all it is is a block of memory. So when you say:

Expand|Select|Wrap|Line Numbers
  1. int myArray = new int[10];
somewhere in memory, there is a space 10 * sizeof(int) long (probably 40 bytes):

Expand|Select|Wrap|Line Numbers
  1.  [memory][myArray[0]][myArray[1]]...[myArray[9]][more memory]...
So if you use myArray[10] or anything else past the bounds, you are looking into memory which may or may not be allocated for your program. You have no idea what you might be printing out. You'll be lucky not to get a segmentation fault for accessing illegal memory locations.
Mar 30 '08 #5
Laharl
849 Expert 512MB
You'll get garbage, whatever happened to reside in memory at those locations. If you're lucky, that is. If you're not, it's possible to get a segfault from that.
Mar 30 '08 #6
If you use myArray[10] or anything else past the bounds, you are looking into memory which may or may not be allocated for your program. You have no idea what you might be printing out. You'll be lucky not to get a segmentation fault for accessing illegal memory locations.
Thanks! I don't know about the hardware memory. May I know if it is possible to print out either 0, 1, 2 or 3 in the output?
Mar 30 '08 #7
The convenient, simple, and most often used method for figuring out the size of an array is...not to figure it out. Make it a separate variable, or if it's a function, require an additional argument indicating the size. Especially for the function, if someone gives you the wrong size, well, that's their own fault.
Yes, right ! Ganon11. I do agree with you. Avoid figuring it out!!

I tried to solve by adding a tracking variable!!

this is the most simple solution
Mar 30 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
OK. Here's how it works:

1) sizeof returns the s1ze of the object on the stack.

That makes:

Expand|Select|Wrap|Line Numbers
  1. char arr[5];
  2. cout << sizeof(arr) << endl;
  3.  
display a value of 5. That is, arr occupies 5 bytes.

This:
Expand|Select|Wrap|Line Numbers
  1. char* arr  = new char[5];
  2. cout << sizeof(arr) << endl;
  3.  
displays 4. All that's on the stack is the pointer to the array.

As a rule, do not use sizeof to find the number of elements in an array.
1) It does not work for heap arrays
2) It does not work inside functions where the array is passed in.

Instead, use a const int var for the number of elements and pass that variable around alogn with the array address.
Mar 30 '08 #9

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

Similar topics

11
by: eggie2486 | last post by:
What is the maximum size of an array? I tried to edit an extremely large array for a magic square, for example, array, and when I ran the program, it would not display the array. I changed the...
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
1
by: Vijay Balki | last post by:
I have a array who's size I will know only at runtime. How do I initialize the size of the array at runtime? Is there a equivalent of ReDim in .NET?? VJ
4
by: usenet | last post by:
Hello, I am running my code on an embedded platform without OS. I have defined some data in a section called .eeprom. The section is defined by the linker script and starts at address zero. The...
17
by: yinglcs | last post by:
Hi, In STL, can I create a variable size but non-growable array? In Java, I can do this: int void f (int size) { int array = new int; // do something with array return array;
2
by: abdulsamad | last post by:
Hello there i wanna get the the size of a dynamic array of integer type which i have created like that......... int *arr=0; arr = createTempArray(); arr = ...
4
by: Edward Jensen | last post by:
Hi, I have the following static arrays of different size in a class: in header: static double w2, x2; static double w3, x3; static double w4, x4; in GaussLegendre.cpp:
2
by: Alien | last post by:
Hi Guys, I am currently making a serial algorithm for clustering. I start off with a dynamic array using "ALLOCATABLE" and prompt user for a size then use the ALLOCATE() function to allocate the...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.