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

how to find length of array in visual c++ ?

like if we have an array
int [] a = new int [20]
int []b;
b= new int [a.length];
FOR(INT I=0; I<a.length ; i++)
{
b[i]=a[i];
}
this program is not working in visual c++ it keep saying me that there is no such method as (a.length) in visual c++
Feb 5 '12 #1
7 18142
weaknessforcats
9,208 Expert Mod 8TB
There is no a.length because a is the address of an int. Addresses don't have methods.

There is no way to find the length of an array in C or C++. The most you can do is to find the sizeof a variable on the stack. In your case you used the new operator which creates the array on the heap. A sizeof(a) will get you the size of the address of the array.

Read this: http://bytes.com/topic/c/insights/77...rrays-revealed
Feb 5 '12 #2
thanks weaknessforcats :) i am new to c++ but a.length method works in java so i was trying the same with c ++
so there is no way to find length of array in c++ ? because sizeof (a) gives the total size of array it dont give any information about length or we can do like if it is char array then (sizeof(a) /8 )is it good idea?
Feb 6 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
sizeof returns the size of the local variable.

If your array is a local variable (on the stack) sizeof(array) will return hw big the array is in bytes. But if you create your array on the heap using new or malloc then all that's local is the address of the array so sizeof(array) now returns the size of the address of the array and not the array itself. Further, if you pass your array to a function as an argument, then all that's passed is the address of the array. So inside the function sizeof(array) again returns the sizeof the address.

You need to maintain a variable that contains the number of elements and pass it around with the array address. This is the only method that works in all cases.

Further, arrays are consdered very low level. C++ users are supposed to use vectors. The vector container is an array but all of the code you need to write to manage your own personal array is duplicated by the vector code. You should just use a vector and save yurself a lot of time writing code that's alreayd been written and debugged.
Feb 6 '12 #4
I figured this worked pretty well:

in the header define this: const int ARRAYEMPTY = -858993460;

then

int arrayLengt(a[])
{
bool end = false;
int i = 0;
while(!end)
{
if (a[i] == ARRAYEMPTY)
{
end = true;
}
else
{
i++;
}
}
return i;
}
Aug 19 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
You want to be careful of that solution. It prevents -858993460 from being in the array and this is a valid int value.

What is passed to the function is the address of the array. As far as the function knows, this is an int*. That is, a pointer to a single int. Incrementing from that address is an assumption. There is no such thing as a pointer to an array. This is because the name of the array is defined as the address of element 0. There is no way to figure out the number of elements from this address.

If you use sizeof on the array to determine the number of elements in the array, the array must be on the stack. The function's stack in this example does not contain the array so the function can't use sizeof to figure out the number of elements.

The only foolproof method is to use two arguments for the function. One for the address of the array and one for the number of elements. Whoever created the array knew the number of elements so it's important to pass that along.
Aug 19 '14 #6
Finding the length of any non-dynamically allocated array is easy. You simply write:

int Array[ ] = { ... };

unsigned int Array_Length( sizeof( Array ) / sizeof( Array[ 0 ] ) );
Aug 20 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
Just keep in mind that only works on the stack in the function where the array is defined.

Everywhere else you will need the number of elements since all you will have is the address of the array. This is called decay of array.
Aug 20 '14 #8

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

Similar topics

18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
8
by: Fernando Barsoba | last post by:
Hi, I decided to start a new topic about my memory allocation question. One of the answers was that variable length arrays are possible using C99. I found this topic that also mentions it. ...
1
by: moonriver | last post by:
I intend to generate a variable-length array, similar to link lists in plain C. For example, I define the following array int a Initially I assign 5 elements to the array as a = new int...
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
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)...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.