Solved

C Array Question

Posted on 2002-05-20
6
195 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
Technology Partners: 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Why adding test code changes the build ? 11 136
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 2,346
SQL handling single and double quotes 3 108
MySQL recovery 7 59
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

734 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