Solved

C Array Question

Posted on 2002-05-20
6
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

627 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