Iterate a Dynamically Allocated Array

I need to dynamically allocate an array of type int with size 12MB. I did it as follows:

unsigned int *memory;
memory = (int *) malloc( 3072 * sizeof(int));

How would I iterate through the array without having to use 3072? Is there a way to get the length of the array? Or should I just do for (int i = 0; i < 3072; i++)?

Thanks

Answers


The is no portable or convenient way to find out how large the allocate chunk is. After malloc is done all that is visible is a pointer to an indeterminate amount of memory (*). You must do the bookkeeping yourself.


(*) Actually malloc and friends do know how large the chunk is but there is no standard way for a client to access this information.


Need Your Help

android monkey script for multiple devices

android robotium monkeyrunner monkey

Can some body can help me for to put android monkey test for multiple devices ?

Simulate ant copy in rake

ruby copy rake

I want to copy some files with Rake, but compared to Ant this is kind of clumsy and my implementation is a bit stupid.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.