Homebrew C question: creating an array of NULL pointers?

  • Thread starter Thread starter mashers
  • Start date Start date
  • Views Views 3,322
  • Replies Replies 29
But would that be any faster than memcpy'ing the actual struct which will be used? Remember as I said it seems to take just as long to create and add empty structs as it does to create, populate and add them. Would memalloc'ing the space for them be faster than this?

I don't get what you're trying to do. You want to avoid preallocating, but don't want to load items when changing pages.
 
Right, but if I malloc the array, how do I access its contents? Can I still use square bracket notation or do I have to add sizeof(menuEntry_s)*indexOfElement to the address of the pointer to the beginning of the array?
Square brackets will work
 
Square brackets will work
Perfect, thank you. So my plan now is:
  1. Remove the 'next' variable from menuEntry_s
  2. When creating menu_s structs, malloc its entries variable to sizeof(menuEntry_s)*numEntries
  3. Replace iteration code which traverses the linked list with for loops, accessing the contents of the entries array using square bracket notation
By this point I should be able to leave the entries array empty and only fill its contents page by page when each page is displayed.

Let's say I do this, and try to access menu.entries[0] after it was malloc'ed but before it was populated. Can I predict what will be in menu.entries[0]? I need to be able to test whether it has already been populated or not.
 
Perfect, thank you. So my plan now is:
  1. Remove the 'next' variable from menuEntry_s
  2. When creating menu_s structs, malloc its entries variable to sizeof(menuEntry_s)*numEntries
  3. Replace iteration code which traverses the linked list with for loops, accessing the contents of the entries array using square bracket notation
By this point I should be able to leave the entries array empty and only fill its contents page by page when each page is displayed.

Let's say I do this, and try to access menu.entries[0] after it was malloc'ed but before it was populated. Can I predict what will be in menu.entries[0]? I need to be able to test whether it has already been populated or not.

Always assume junk data, use memset(entries, 0, sizeof(menuEntry_s)*numEntries); to clear everything.
 
Let's say I do this, and try to access menu.entries[0] after it was malloc'ed but before it was populated. Can I predict what will be in menu.entries[0]? I need to be able to test whether it has already been populated or not.
Depends on the used C library. AS per spec malloc() doesn't need to init the memory, calloc() does but needs more runtime to do it.

Also bear in mind that it's a pain in the ass to remove or insert new elements inside or to resize an array.
 
Any ideas if there would be any difference in performance between malloc() + memset() and calloc() ?

At least on linux calloc is allways faster than malloc+memset, you'll have to try if it's the case on the 3DS

<edit>(to be more precise, malloc + memset is never faster than calloc on linux when using a current glibc)</edit>
 
Last edited by bkifft,

Site & Scene News

Popular threads in this forum