i´m calling it in each frame.
should i make some kind of counter or something like that?
but i don´t think it can be a lack of mem, because a few minutes ago i was able to load about 30 new stations, with 30 sprites. but they just where created on the same coordinates.. and didn´t move
If you are calling it in every frame then stations will always point to a new array of ownstation structs. You might want to try replacing the line
stations = (ownstation*) malloc(n*sizeof(ownstation));
with stations = (ownstation*) realloc(stations, n*sizeof(ownstation));
glorious!
have much thanks. it´s working very fine.
but for understanding. realloc is reserving memory as malloc is doing. But realloc keeps the given values and new values are simply added behind the already excisting values?
Yes. As I understand it, if you use realloc to enlarge a block of memory, it allocates a larger block of memory, copies the contents of the original memory block to the new memory block and then frees the original memory block.
From what I can tell, malloc() allocates memory and gives you a pointer to it. However, the memory is not released back to the system unless you explicitly call free() on the pointer or close your program. If you call malloc to get memory and you no longer need it, you have to call free to give it back to the OS. If you don't, you're generating what is known as a memory leak, as you're rendering memory unusable. realloc() does this for you. The end result is much like RuleOfNothing said (I think). If you chose to use malloc() instead of realloc(), make sure to call free() on the old pointer when you're done.
But i only have to use free() if i really want to destroy an "object"? So i can create, create and create and only for "n"- 1 (for example) i need to free the memory then?
EDIT:
btw, there´s a new question i have.
i made an option-menu
I choose with my cursor the option "Handelsbildschirm" an it leads me to the new menu, but there the cursor does not move. is it because of the "while()" ?
It's because you don't take in keypad input in your menu loop, so Pad.Newpress.Down, Pad.Newpress.Up and Pad.Newpress.B will never have non-zero values, so the various if statements will never be triggered. Also, you should use free() only in situations where you no longer want to use the memory you pass to free(), because if you use memory after it has been freed, it may have been overwritten.
what do you mean with that i "dont take keypad input in my menu loop"? they´re in each loop as i see. in the first Menu it´s even working, but if i´m pressing the A-Button the new menu appears but with each klick on up or down the "o" will put out on each y-value
EDIT:
Allright, I understood what you mean.
made it now so:
while ((stations[istationen].entfx<30)&&(stations[istationen].entfy<30)){
//PA_OutputText(1,2,9,"'X' zum kaufen");
//
//menu();
if(optionsflag==0)stationsmenu();
if(Pad.Newpress.A)optionsflag=1;
if(optionsflag==1)handelsbildschirm();
break;
}
I´m of course not ready with it, but seeing that it´s working, is bringing me forward. Have much thanks!