473,396 Members | 1,891 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.

How to find size of a dynamically allocated array

Hello.
I want to find size of a dynamically allocated array in my following code :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.         int *Set = NULL;
  10.         int element;
  11.         char Choice;
  12.         int n = 0;
  13.  
  14.         do
  15.         {       
  16.                 cout << "  Enter an element : ";
  17.                 cin >> element;
  18.  
  19.                 n++;
  20.                 Set = (int *)realloc(Set, n * sizeof(int));
  21.                 Set[n - 1] = element;
  22.  
  23.                 cout << "  Do you want to enter another element? (y/n) : ";
  24.                 cin >> Choice;
  25.         } while (Choice == 'y' || Choice == 'Y');
  26.  
  27.         cout << "  Size of the array : " << sizeof(Set)/sizeof(int) << endl;
  28.         return 0;
  29. }
  30.  
Here is the output :
tapas@My-Child:~/Programming/Set Intersection$ ./test.o
Enter an element : 1
Do you want to enter another element? (y/n) : y
Enter an element : 2
Do you want to enter another element? (y/n) : y
Enter an element : 3
Do you want to enter another element? (y/n) : n
Size of the array : 1 <-- But the size should be 3!!
The reason behind this behavior is, Set is a integer pointer which points to a chunk of contiguous memory block and using sizeof(Set)/sizeof(int) I am getting the size of the pointer (am I wrong?). But how can I can the size of allocated memory, I also try it sizeof(*Set)/sizeof(int), but the result is same. And
Expand|Select|Wrap|Line Numbers
  1.  cout << "  Size of the array : " << n  << endl;
  2.  
will give the answer. But I need some function. Please help.
Mar 13 '10 #1
7 12079
You have declared:

int *Set = NULL;

this means that the variable Set is a pointer to int. On your system pointers are the size of 4 bytes which is why you get the answer 1 when you do sizeof(Set)/sizeof(int). Instead of using dynamic allocated memory why not let the standard template library do all the heavy lifting for you by using a vector.

i.e.

vector<int> Set; // to create it

Set.push_back( element ); // to add a new int to it

Set.size(); // to get the size
Mar 13 '10 #2
Banfa
9,065 Expert Mod 8TB
The actual answer to your question is that there is no way to get the size of a dynamically allocated array except to use a variable, like n, to remember what it was. There is no function that returns it.

Since you are using C++ you should avoid using malloc/realloc/free although C++ admitidy has no equivilent of realloc.

And as colford says you should prefer using vectors and other standard contains in C++ rath than arrays and other home grown containers.
Mar 13 '10 #3
whodgson
542 512MB
I think you are getting the correct answer.
sizeof(Set)=3
sizeof (int) probably = 4 on your machine
int 3/4=1
Mar 14 '10 #4
whodgson
542 512MB
I want to find size of a dynamically allocated array
In a sense your question is redundant when you consider the way in which a dynamic array is typically declared in C++:

double* p = new double [20]; // so the size of p is 160 bytes

As far as I am aware setting the number of elements in the subscript of array p (or equivalent) is mandatory.
Of course you have to call <delete [] p> later to prevent the possibility of dangling pointers and memory leaks.
Mar 14 '10 #5
i think if you don't set the SET = NULL at the beginning it will work. it does in c though!

but you must use an
if n= 0 malloc
in the do while
Mar 14 '10 #6
Banfa
9,065 Expert Mod 8TB
I think you are getting the correct answer.
sizeof(Set)=3
sizeof (int) probably = 4 on your machine
int 3/4=1
Erm, sizeof(Set) is not 3, remember sizeof returns the size in bytes of an object, not the number of elements in an array since sizeof can be called on things that are not arrays.

As has already been said Set is a pointer sizeof does not do any dereferencing, it returns the size of the object passed to it not the size of the object pointed to by the object passed to it so sizeof(Set) is sizeof a pointer which for a 32 bit machine is 4 bytes.

Additionally even if it did return 3 then

int 3/4 = 0 not 1
Mar 14 '10 #7
whodgson
542 512MB
Yes #4 is very bad ...i wonder where I was when that was written. Please ignore thread #4
......thanks Banfa
Mar 15 '10 #8

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

Similar topics

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
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
6
by: finerrecliner | last post by:
hey everyone i'm trying to make a function that will return the length and width of a dynamically allocated 2D array. here's what i came up with to find width: int findWidth(int** matrix)...
5
by: Gerrit | last post by:
Hi all, I'm getting an OutOfMemoryException when I initialize a byte array in C# like this: Byte test = new Byte; I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
8
by: The Cool Giraffe | last post by:
I have a dynamically declared array of integers and i'd like to check the number of elements. For some reason, the expected sizeof(mtx)/sizeof(mtx) doesn't work. I've been googling and i got a...
10
by: linq936 | last post by:
Hi, If the function signature is like this: int func( int arr ); Or this: int func ( int* arr ); Then inside the function I have no way to know the size of the
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
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
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: 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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.