Homebrew C question: creating an array of NULL pointers?

MasterFeizz

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
1,098
Trophies
1
Age
28
XP
3,698
Country
United States
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.
 

MasterFeizz

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
1,098
Trophies
1
Age
28
XP
3,698
Country
United States
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
 

mashers

Stubborn ape
OP
Member
Joined
Jun 10, 2015
Messages
3,837
Trophies
0
Age
39
Location
Kongo Jungle
XP
5,054
Country
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.
 

MasterFeizz

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
1,098
Trophies
1
Age
28
XP
3,698
Country
United States
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.
 

bkifft

avowed Cuthwaldian
Member
Joined
Jun 10, 2010
Messages
613
Trophies
0
XP
624
Country
Gambia, The
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.
 

bkifft

avowed Cuthwaldian
Member
Joined
Jun 10, 2010
Messages
613
Trophies
0
XP
624
Country
Gambia, The
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,
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys