943,624 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1279
  • C++ RSS
Jul 28th, 2009
0

Finding the length of an int pointer array

Expand Post »
I'm working on a little code snippet and my c++ seems a bit rusty. How do i go about getting the length of an array? My eclipse is giving me weird values when it should be segfaulting, so I'm trying to figure out whats wrong. Thanks in advance.

edit: forgot to say what i meant by an int array.

int *x = new int[8];
Last edited by DaBunBun; Jul 28th, 2009 at 2:17 pm.
Similar Threads
Reputation Points: 28
Solved Threads: 5
Light Poster
DaBunBun is offline Offline
36 posts
since Jul 2009
Jul 28th, 2009
0

Re: Finding the length of an int pointer array

I always advocate using an std::vector when possible.. can you do this:
C++ Syntax (Toggle Plain Text)
  1. std::vector<int*> yourVar(8);
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jul 28th, 2009
0

Re: Finding the length of an int pointer array

Are you saying to use a different variable type?
Or are you saying to type cast it?

If its the first, I want to get the length of a pointer thats being passed.

Edit: Forgot to say, thanks for the help. Normally I'd use a vector like that, except in this case I'm being passed an in pointer.
Last edited by DaBunBun; Jul 28th, 2009 at 2:30 pm.
Reputation Points: 28
Solved Threads: 5
Light Poster
DaBunBun is offline Offline
36 posts
since Jul 2009
Jul 28th, 2009
0

Re: Finding the length of an int pointer array

Click to Expand / Collapse  Quote originally posted by DaBunBun ...
Are you saying to use a different variable type?
Or are you saying to type cast it?

If its the first, I want to get the length of a pointer thats being passed.

Edit: Forgot to say, thanks for the help. Normally I'd use a vector like that, except in this case I'm being passed an in pointer.
I'm pretty sure it's impossible to get the length of an array from the address of the base pointer. You the programmer are responsible for keeping track of the length. That's why when you pass an array to a function, you generally pass the length as well. Regarding "length of a pointer", I assume you are referring to "number of elements in the array"? Regardless, I know of no way to get the 8 from x in your original example. Store 8 in a separate variable.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jul 28th, 2009
0

Re: Finding the length of an int pointer array

Yeah, I didn't think so either, but I just wanted to check. I think there might be a way if you know what your being passed and using sizeof(), but thats not what I was looking for. Thanks for your help.
Reputation Points: 28
Solved Threads: 5
Light Poster
DaBunBun is offline Offline
36 posts
since Jul 2009
Jul 28th, 2009
0

Re: Finding the length of an int pointer array

You always have to pay attention when you create a vector of pointers, consider this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. vector<int*> myvec(5);
  8.  
  9. if( 5 > 3 )
  10. {
  11. int a = 35;
  12. myvec[0] = &a;
  13. cout << *myvec[0] << endl;
  14. }
  15.  
  16. cout << *myvec[0] << endl; // not guaranteed to print 35 on the screen
  17.  
  18. return 0;
  19. }
When variable a goes out-of-scope, then the first pointer in vector myvec (myvec[0]) will point to some memory location which doesn't belong to your program anymore.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Most popular graphics library
Next Thread in C++ Forum Timeline: what is produced in an inflie function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC