• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: allocate a 2d array of floats?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: allocate a 2d array of floats?


  • Subject: Re: allocate a 2d array of floats?
  • From: Alastair Houghton <email@hidden>
  • Date: Sat, 22 Nov 2003 00:32:05 +0000

On 22 Nov 2003, at 00:18, Jay Rimalrick wrote:

> Thanks for everyone's advise on the dynamic 2d array i ended up with
> this
> code to do the job, however how do I assign values without doing it
> cell by
> cell? The last assignment with 'a' does not work in this dynamic
> state but
> does work when you define a 2d array statically ... any suggestions?

You can't. You have to assign to individual elements. C doesn't
really support 2D dynamically allocated arrays (you have to code them
yourself, as you've already discovered).

> a = (float **) malloc (sizeof (float *) * 7);
> int n;
>
> for (n = 0; n < 7; ++n)
> a[n] = (float *) malloc (sizeof (float) * 5);

Do you really need to malloc() the rows separately? If not, then the
example I posted shows you how to do it with two malloc() calls rather
than the eight that your code does. (What's more, the example I posted
will *always* do two malloc()'s, whereas your way you do 1 + H malloc()
calls, where H is the number of rows in your array.)

Another advantage of the code I posted is that memory locality is
potentially much better, because all of the data will be held in a
single block of memory. With the above code, the small malloc()
requests might be spread about in memory, reducing performance.

Kind regards,

Alastair.

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Waiting for a Cocoa app to die...
  • Next by Date: Re: Waiting for a Cocoa app to die...
  • Previous by thread: Re: allocate a 2d array of floats?
  • Next by thread: Image Capture with iSight?
  • Index(es):
    • Date
    • Thread