Finding the length of an int pointer array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 36
Reputation: DaBunBun is an unknown quantity at this point 
Solved Threads: 4
DaBunBun DaBunBun is offline Offline
Light Poster

Finding the length of an int pointer array

 
0
  #1
Jul 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Finding the length of an int pointer array

 
0
  #2
Jul 28th, 2009
I always advocate using an std::vector when possible.. can you do this:
  1. std::vector<int*> yourVar(8);
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: DaBunBun is an unknown quantity at this point 
Solved Threads: 4
DaBunBun DaBunBun is offline Offline
Light Poster

Re: Finding the length of an int pointer array

 
0
  #3
Jul 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Finding the length of an int pointer array

 
0
  #4
Jul 28th, 2009
Originally Posted by DaBunBun View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: DaBunBun is an unknown quantity at this point 
Solved Threads: 4
DaBunBun DaBunBun is offline Offline
Light Poster

Re: Finding the length of an int pointer array

 
0
  #5
Jul 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Finding the length of an int pointer array

 
0
  #6
Jul 28th, 2009
You always have to pay attention when you create a vector of pointers, consider this:
  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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 548 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC