C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 November 19th, 2003, 11:57 AM
Combat Combat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 42 Combat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
getting the size of a dynamic array

okay, i am having problems getting the size of a dynamic array... with a static array i could just do this: sizeof(Array); but when you are working with a dynamic array, it just returns the size of the pointer. Is there a function for this, or should i just write a class that has the array size and index stored in it?

Reply With Quote
  #2  
Old November 19th, 2003, 12:15 PM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 7
There's no (ansi) way to get the (memory) size of an array pointer. The common way is to create a struct around it with e.g. a size_t size field.

Reply With Quote
  #3  
Old November 19th, 2003, 12:38 PM
Kris_A Kris_A is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Manchester, UK
Posts: 58 Kris_A User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 1 m 33 sec
Reputation Power: 6
why not store the size in a variable, and change its value whenever you resize the array?

Y'know, like:

new_size = 500;

...

array = malloc(new_size*sizeof(array_type));
size = new_size;

Reply With Quote
  #4  
Old November 19th, 2003, 03:02 PM
Combat Combat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 42 Combat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
that's what i did at first, but i thought there would be a better way... i guess i will just write a class that automatically sets the size variable everytime the array is resized. thanks anyway guys.

Reply With Quote
  #5  
Old November 20th, 2003, 03:40 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,422 clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 8 h 12 m
Reputation Power: 969
Quote:
Originally posted by Combat
... but i thought there would be a better way... i guess i will just write a class that automatically sets the size variable everytime the array is resized. thanks anyway guys.


If you created an array you must already have known its size!

The problem that dynamic allocation returns a pointer. A pointer is not the same as an array. An array, as you know retains its size information, a pointer knows only the size of itself, not the data it points to. An array, like a reference, cannot be moved to reference different data, whereas a pointer may be reassigned. When an array is passed as a function parameter, it is converted to a pointer, so within that function, you loose the array behaviour such as size information.

However, you are right that having two separate variables (a size and a pointer) is inconvenient and potentially error prone.

However, you have exactly described the better way that you are seeking - write your own class to encapsulate the functionality you require. If you make it a template class, you can create arrays of any desired type, have functions to obtain the size in bytes and the also the size in terms of number of elements. By overloading the [] operator, you can make it behave syntactically just line an array.

Clifford

Reply With Quote
  #6  
Old November 20th, 2003, 09:56 AM
Combat Combat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 42 Combat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
that's what i did. As for me already knowing the size of the array... I am using several different functions for this little program. I am not passing the size of the array to each one. It is much easier than that to just write my own dynamic array class.

Reply With Quote
  #7  
Old November 20th, 2003, 10:28 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,422 clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level)clifford User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 8 h 12 m
Reputation Power: 969
Quote:
Originally posted by Combat
... As for me already knowing the size of the array... I am using several different functions for this little program. I am not passing the size of the array to each one...


Unfortunately that is what you have to do, (even for arrays, because they are converted to pointers when passed into a function). Take a look at the standard C library for example, functions like memcpy() and memset() require a size parameter.

Hiding all the detail in a class is as elegant as it gets - which is actually pretty elegant, and if well designed need only be done once.

You might also consider using the STL classes, such as vector, which does exactly more or less what I described earlier.

Clifford.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > getting the size of a dynamic array


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



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT