Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C Array Question

Posted on 2002-05-20
6
Medium Priority
?
210 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

916 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