c - pointer confusion with malloc -


i have working implementation of code ton of mallocs. has

struct* node mylist; struct node { ... } // contains stuff struct* node_constructor(...) {     node* tempnode = (node*) malloc(sizeof(node));     node* next = ...;     ... // other info } 

and in main function call

mylist = node_constructor(mylist,otherarguments...); 

i'm thinking there's way like

temp = (node*) malloc(sizeof(node)*numberofnodes); for(i=0; i<numberofnodes;i++) { temp[i].next = temp[i-1]; ... } // wrong? temp[numberofnodes-1]; // set mylist equal last 1 

and rid constructor function , individual mallocs, seem getting confused malloced space. temp (node*), i'm pretty sure temp[i].next = temp[i-1] thing wrong...and maybe of other ideas too. if has advice/input, i'd appreciate it.

ok, sure, that, kinda. long temp big enough , know size beforehand. , temp more void* or generic container hang onto unspecified memory rather node. (because it's not node, it's nodes go. temp[x] when temp malloced kind of hack).

but want add node list. how do that?

the individual mallocs let add single item list. , if in situation want add number of items list, can call individual functions multiple times.

the idea of allocating large pool of memory, , divvying piecemeal sections later valid idea , has performance benefits, it's of limited niche use.

allocating enough memory 1 more item lot more handy , common. you're not wrong, trust me, learn how normal way first.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -