C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 16th, 2005, 04:39 PM
marizion marizion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 180 marizion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 m 43 sec
Reputation Power: 8
newbie,determine the size of an array

lets say you have a dynamically allocated array called darray
how do u determine the size of the array?

i tried:
int size = sizeof(darray) * sizeof(arraytype)
but the sizeof(darray) dosen't check the whole array. it only checks the sizeof of the pointer to the first element.

i also tried
int size = sizeof(*darray) * sizeof(arraytype)
but i guess that was a stupid move cuz it would give me the data at the location of the pointer which is not an array just one element.

i think i also tried something else but i can't remember what lol.

can someone tell me the formula for finding the size of the array, both dynamic and static?
well i think the first thing i tried would work for static arrays so.

Reply With Quote
  #2  
Old August 16th, 2005, 04:43 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Quote:
Originally Posted by marizion
lets say you have a dynamically allocated array called darray
how do u determine the size of the array?
Generally you use a variable to keep track of how much memory you ask for.
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Reply With Quote
  #3  
Old August 16th, 2005, 06:18 PM
Randolpho's Avatar
Randolpho Randolpho is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2004
Location: Middle TN
Posts: 504 Randolpho User rank is Sergeant (500 - 2000 Reputation Level)Randolpho User rank is Sergeant (500 - 2000 Reputation Level)Randolpho User rank is Sergeant (500 - 2000 Reputation Level)Randolpho User rank is Sergeant (500 - 2000 Reputation Level)Randolpho User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 5 m 47 sec
Reputation Power: 17
Just to expand on Dave's suggestion:

If you created the array yourself, you already know how large the array is, because you malloc'ed or new'ed the memory. If you need that information elsewhere in your program store it as a variable or a #define (depending on your design).

If you called some API call that returned a pointer to a dynamically created array, I sincerely hope it also gives you the size of the array. If it does not, either a) shoot the person who wrote the API, or b) get yourself a new API.

On second thought, you should probably do both.
__________________
Bad code monkey! No banana!

Reply With Quote
  #4  
Old August 16th, 2005, 09:22 PM
marizion marizion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 180 marizion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 m 43 sec
Reputation Power: 8
lol, thanks for ur advice that would get me in jail, then i'll never be able to program again

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > newbie,determine the size of an array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap