c++ - Best way of using malloc and realloc -


i'm pritty new using malloc , realloc , have few questions.

question 1.

the game making allows create own spaceschip, knowing wondering better realloc twice ammount of current memory standard. or along lines of reallocing 100 spaces needed vertexes? self think seccond option better maybe there i'm not aware of.

question 2

if have malloced array of squares[20] [4] how realloc size of squares[100][4]? realloc 1st array 100 , malloc 20-99 4 new spots?

question 3

right strange happens when realloc data. if put down break point goes fine if let code work @ once crashes. i'm guessing there memory leak. right realloc code.

unsigned char *temp =( unsigned char* ) realloc(realspaceship, gridsize*sizeof( unsigned char )); if(temp != null) {     realspaceship = temp;     //free(temp); line deleted @ advice of helpfull people here. } else {     //error     printf("realloc error\n");     free(realspaceship); } 

i figgured freeing temp idea couses code crash @ different location.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

i realised problem not realloc somewhere else.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

a litle information. i'm using c++ , i'm using glm

honestly, avoid using realloc multiple times. malloc once , stick until run extreme case, realloc (but then, avoid it!)

the main reason debugging , testing. if program fails @ realloc, can multiple reasons... , of comes os. (i.e. not enough memory, trying use memory freed, etc.) tough bugs reproduce , debug.

do little math, calculate max need use case , malloc that. move onto coding other stuff. debugging realloc errors no fun.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -