Solved

C Array Question

Posted on 2002-05-20
6
182 Views
Last Modified: 2010-04-15
Is there a good way to calculate the number of elements in a malloc'd array?

I need to implement a function that accepts a pointer which is the malloc'd array and returns the number of elements in the array.
0
Comment
Question by:Dargie
6 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 50 total points
ID: 7022476
There are two ways this is done:

1. Accept an additional parameter containing the number of elements in the array so that you don't have to figure it out.

2. Define a convention that the last element in the array has a NULL value. You can count the elements by looping through the elements and stopping when you hit a NULL.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7028602
Dargie, how are you going to malloc the array?
Aren't you going to base it on the number of elements you need to put in the array?
An easy way is to just keep track of how much you malloc and/or put in.

Are you using structures or something of a consistent size?
If so, you can divide the size of the malloc'd array by the size of one data element.  That will tell you how much you have in the array.

Yoren also has two workable ideas.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7029853
If you use malloc(), you can use _msize() to get the size in bytes of the originally allocated block. Is this what you're searching for?

BTW: I cannot check right now, but i suppose this is not portable :)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 6

Expert Comment

by:snoegler
ID: 7029855
BTW again: I supposed you use MSC/MSVC ...
0
 

Author Comment

by:Dargie
ID: 7030319
I can't keep track of the malloc because this needs to be a function that returns the size of an array based upon the array passed.  Yoren's 2nd idea seems to be the only workable solution.  I will malloc 1+ the size of the array needed and append a sentinal value and then loop through until this value is encountered.
0
 

Author Comment

by:Dargie
ID: 7030321
They only workable solution.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now