Three more questions

Posted By: 3run

Three more questions - 08/19/14 16:09

As I continue developing my small project farther, I've faced some questions answers for which I couldn't find at all.

FIRST QUESTION is about mouse events... EVENT_TOUCH, EVENT_RELEASE and EVENT_CLICK. Well everything works and almost everything is clear, but I would like to know, how to make mouse ignore specific entities? I couldn't find anything about this in the manual (maybe I've searched bad/wrong), and I really hope that this is possible. The thing that I need this for (as an example). We have a house, that is covered with the roof. Player walks inside of the house, so the roof gets translucent and we can click on objects inside of the house, but problem is, that even if the roof model has PASSABLE flag on, mouse can't detect objects below..

SECOND QUESTION is about text and probably about max. size of the string (maybe set by default?). The thing is, that I'm working on some kind of a journal (diary?) so all picked up notes are stored in it. It open ups a .txt file, fins the text (by it's ID and page) that we need to display. But so far I've noticed that only about 300 letters are displayed.. Why? I've set 'size_x' and 'size_y' for the TEXT, and it has only one string to be displayed. Why this happens?

THIRD QUESTION is still about TEXT stuff.. and this time about '\n' thing.. As far as I know thing THING splits the string so the rest of it (till end, or delimiter or next THING) starts from the next line (below). But I've found out, that when I read text from a .txt file into the string, this THING doesn't work.. I wonder why? I've set delimiter to ';', could that cause any trouble?



Greets
Posted By: Uhrwerk

Re: Three more questions - 08/19/14 16:28

For the first question: You can set the entities UNTOUCHABLE flag.

The second question: Please post your code.

Third question: the delimiter is just a character that causes the file_read_string instruction to stop at that specific character. What exactly is the problem? Is it ignored?
Posted By: MasterQ32

Re: Three more questions - 08/19/14 16:29

1: flags2 + UNTOUCHABLE
2: Try using WWRAP to wrap text lines (txt_load loads line by line)
3: \n is an escape code. It is just a "new line" as long long as you are in c-code
The compiler translates \n to 0x0A in the string AT compile time).
If you load \n from a text file, you load the actual \n string which is 0x2F 0x6E
So you need to parse your loaded string for "\\n" and replace every single one with " \n" (space at line end does not do anything at all, but has the same size so you can run the code in-place)
Posted By: 3run

Re: Three more questions - 08/19/14 16:38

Originally Posted By: Uhrwerk
For the first question: You can set the entities UNTOUCHABLE flag.

The second question: Please post your code.

Third question: the delimiter is just a character that causes the file_read_string instruction to stop at that specific character. What exactly is the problem? Is it ignored?
Of man, the answer for the first question was so easy... dumb me :<
About the code, I can't post it right here, cause I don't have access to it from this place.
The problem is, that '\n' doesn't work if you read text from file.
Originally Posted By: MasterQ32
1: flags2 + UNTOUCHABLE
2: Try using WWRAP to wrap text lines (txt_load loads line by line)
3: \n is an escape code. It is just a "new line" as long long as you are in c-code
The compiler translates \n to 0x0A in the string AT compile time).
If you load \n from a text file, you load the actual \n string which is 0x2F 0x6E
So you need to parse your loaded string for "\\n" and replace every single one with " \n" (space at line end does not do anything at all, but has the same size so you can run the code in-place)
2. I do use WWRAP, as I've said above I've set 'size_x' and 'size_y' to make my text feet the background image, and this part works great. But the thing is, that only about 300 character of the whole text are visible.. I still wonder why :<
3. so I need to cycle throw the string, find '\\n' and replace every single one I've found with '\n' ?


Thank you guys, for such a quick replies! laugh

Greets
Posted By: 3run

Re: Three more questions - 08/19/14 17:11

for the THIRD problem I used 'str_replaceall', and replaced all '\\n' with '\n' and it works! But it leaves one '\' right before jumping to the next line... why?

for the FIRST problem UNTOUCHABLE flag2 was used and it's works perfectly fine.

and SECOND problem still there, waiting for a mighty community member to fix it grin


edit: I've found out that actually changing '\\n' to (f.e.) '\m' still leaves '\' right before splitting the string to next line..
Posted By: Quad

Re: Three more questions - 08/20/14 00:19

\ is also an escape character needs to be escaped so in your context \\ means single slash so "\\n" in fact matches \n leaving the first slash, to match them both you have to replace "\\\\n".
Posted By: 3run

Re: Three more questions - 08/20/14 07:14

Salam Aleikum, Quad laugh
Thank you for responce, it actually worked with your help!


SECOND PROBLEM is still there.. but I've found out that problem is in the code (where I read the string into the text).
I'm trying to figure out, but still no luck.. I can't upload the whole thing here, I could share it with someone via PMs.

Greets
Posted By: 3run

Re: Three more questions - 08/20/14 16:43

So, I've found out that SECOND problem comes from my script, but yet I can't find the problem.

I've ripped the text loading part from my diary, and here it is:
Code:
// include lib:
#include <acknex.h>
#include <default.c>
#include <strio.c>

// an empty string:
STRING* str_file = "#999";				// used to read the stuff from the file
// document strings:
STRING* document_str = "notes.txt";			// the name of the document file
STRING* document_page = "#999";				// same as above
STRING* text_str = "#999";				// used to display text

// documents stuff:
var document_id = 0;					// used to read document ID from external file

// background for the text:
PANEL* documentPan = {
	// set it's layer:
	layer = 1;
	// set it's bmap:
	bmap = "#340x450x24";
	// flags:
	flags = SHOW | UNTOUCHABLE;
}

// document description text:
TEXT* doc_desc_txt = {
	// no strings:
	strings = 0;
	// set layer:
	layer = 2;
	// set font:
	font = "Arial#18i";
	// set flags:
	flags = SHOW | LIGHT | WWRAP;
}

// document description text:
TEXT* doc_debug_txt = {
	// no strings:
	strings = 0;
	// set layer:
	layer = 2;
	// set font:
	font = "Arial#18i";
	// set flags:
	flags = SHOW | LIGHT | WWRAP;
}

// reads a string from file and puts it to global pointer:
function file_for_str(var fhandle){
	// read string from file, don't have to be comma separrated:
	var result = file_str_read(fhandle, str_file);
	// copy string into the debug text:
	str_cpy((doc_debug_txt.pstring)[0], str_file);
	// convert the string to right format:
	str_replaceall((doc_debug_txt.pstring)[0], "_", " ");
	str_replaceall((doc_debug_txt.pstring)[0], "\\\\n", "\n");
	// if we succeeded:
	if(result != -1){
		// if we were able to read data from the file, use var_for_name to pass the value to global pointer:
		var d = var_for_name(str_file);
		// if we weren't able to read data:
		if(d == 0){
			// write it down in diag:
			diag("\nUnkwon string or var given:");
			// write it down in diag:
			diag(_chr(str_file));
		}
	}
	// return result:
	return(result);
}

// laod document title, pages and maximum pages by ID
function loadDoc(var id){
	// open the document file:
	var fhandle = file_open_read(document_str);
	// if the file was null, safe return this function:
	if(fhandle == NULL){ return(0); }
	// variables:
	var i, a;
	// cycle throw all documents:
	for(a = 0; a < 1; a++){
		// get global string/variable from the file:
		i = file_for_str(fhandle);
		// if result was -1, we're at the end of the file:
		if(i == -1){ break; }
		// if the id fetched from the file matched the ID we're searching for:
		if(document_id == id){
			// load pages with maximum apge variable:
			for(i = 0; i < 1; i++){
				// load page of the document:
				file_for_str(fhandle);
				// convert the string to right format:
				str_replaceall(document_page, "_", " ");
				// convert the string to right format:
				str_replaceall(document_page, "\\\\n", "\n");
				// copy the currect page to the corresponding page array:
				str_cpy(text_str, document_page);
			}
			// break the loop:
			break;
		}
	}
	// close the document file:
	file_close(fhandle);
	// if we succeeded here, return 1:
	return(1);
}

// toggle the currect document:
function loadText(){
	// copy currect page:
	str_cpy((doc_desc_txt.pstring)[0], text_str);
	// make description visible:
	set(doc_desc_txt, SHOW);
}

// document's title is clicked in choice panel:
function titleClicked(){
	// load document with real value:
	loadDoc(1);
	// load text into the text (lol):
	loadText();
}

// main function:
void main(){
	// limit fps:
	fps_max = 60;
	// load empty level:
	level_load(NULL);
	// wait for while:
	wait(3);
	// set delemeter:
	str_cpy(delimit_str, ";");
	// load text from the .txt file:
	titleClicked();
	// loop:
	while(!key_esc){
		// show current documents ID:
		DEBUG_VAR(document_id, 10);
		// set notebook at the center of the screen:
		documentPan.pos_x = (screen_size.x / 2) - (bmap_width(documentPan.bmap) / 2) + 150;
		documentPan.pos_y = (screen_size.y / 2) - (bmap_height(documentPan.bmap) / 2);
		// set proper position for text itself:
		doc_desc_txt.pos_x = documentPan.pos_x + 15;
		doc_desc_txt.pos_y = documentPan.pos_y + 15;
		// set the size for the text (description) - used by WWRAP:
		doc_desc_txt.size_x = bmap_width(documentPan.bmap) - 25;
		doc_desc_txt.size_y = bmap_height(documentPan.bmap) - 25;
		// set debug text:
		doc_debug_txt.pos_x = 45;
		doc_debug_txt.pos_y = doc_desc_txt.pos_y;
		// set the size for the text (description) - used by WWRAP:
		doc_debug_txt.size_x = 300;
		doc_debug_txt.size_y = 500;
		// wait one frame:
		wait(1);
	}
	// exit:
	sys_exit("");
}


Here how the 'notes.txt' file looks like (can't be displayed here correctly):
Quote:
document_id = 1;
document_page = This_is_just_an_example_of_my_diary_system_I've_created_so_far!\\n\\nIt_works_pretty_nice_and_I'm_happy_with_the_results.\\n\\nBut_I've_found_out_that_only_about_300_characters_of_this_string_will_be_visible...\\n\\nHopefully_this_example_will_show_you_what_kind_of_a_problem_I'm_currently_facing_and_you'll_be_able_to_help_me_out_guys.;


Take a look at this screen:


If you want to give it a try, you may download ready to use demo:
Download link

What could cause the problem? It seems (at least to me) that everything should work fine!
The text on the left, displayed right after it was read from the .txt file by file_for_str.
Second one (the one with the background) is displayed after the string receives the text and get's edited.

Greets
Posted By: 3run

Re: Three more questions - 08/21/14 17:48

Really? No one can help me out with this? Even after I've posted a code?? frown
Posted By: lemming

Re: Three more questions - 08/21/14 20:25

I was seriously disappointed when in the main you only commented every second line.

I was quite unable to understand what you where trying to do, mainly because your comments killed your code. My favorite lines where:
Code:
// set it's layer:
layer = 1;

// no strings:
strings = 0;

// wait one frame:
wait(1);



So I came up with my own approach. Read the file until delimeter "#" then append it to TEXT, read next til "#" and so on. Then the order they are saved in represent ids.

I dunno what problems you had with \n and stuff.

Click to reveal..

Code:
// include lib:
#include <acknex.h>
#include <default.c>
#include <strio.c>

STRING* document_str = "notes2.txt";			// the name of the document file



STRING* strText = "";
STRING* strFile = "";



// document description text:
TEXT* doc_desc_txt = {
	strings = 0;
	layer = 2;
	font = "Arial#18i";
	flags = SHOW | LIGHT | WWRAP;
}



//STRING* strText = "";
//STRING* strFile = "";

STRING* strNumber = "";
/**
 * reads a Note from a File. Modifies doc_desc_txt as it appends strings to it.
 */
var read_notes(STRING* file)
{
	var fhandle = file_open_read(file);
	if (fhandle)
	{
		var num = 0;
		var eof = 0;
		do
		{
			eof = file_str_readto(fhandle, strFile, "#", 10000);
			if (eof != -1)
			{
				error(strFile);
				if (str_len(strFile) > 0)
				{
					txt_addstring(doc_desc_txt, strFile);
					num++;
				}
			}
		} while (eof != -1);
		return num;
	}
	return 0;
}

void cycle_notes(var from, var to)
{
	var waitTime = 0;
	var curr = from;
	while(1)
	{
		txt_setinvisible(doc_desc_txt, -1);
		txt_setvisible(doc_desc_txt, curr);
		waitTime += time_step;
		if (waitTime > 10)
		{
			waitTime = 0;
			curr = cycle(curr+1, from, to+1);
		}
		wait(1);
	}
}


// main function:
void main(){
	fps_max = 60;
	level_load(NULL);

	var msgs = read_notes("notes2.txt");
	cycle_notes(0, msgs-1);
	
	while(!key_esc){
		DEBUG_VAR(1, 0);
		DEBUG_VAR(msgs, 20);

		doc_desc_txt.pos_x = 15;
		doc_desc_txt.pos_y = 15;

		doc_desc_txt.size_x = 200;
		doc_desc_txt.size_y = 400;
		
		wait(1);
	}
	// exit:
	sys_exit("");
}


Code:
This is just an example of my diary system I've created so far!

It works pretty nice and I'm happy with the results.

But I've found out that only about 300 characters of this string will be visible...

Hopefully this example will show you what kind of a problem I'm currently facing and you'll be able to help me out guys.#
THIS IS MADNESS!#
Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet Lorem Ipsum Dolor sit amet


Posted By: 3run

Re: Three more questions - 08/21/14 21:47

Originally Posted By: lemming
I was seriously disappointed when in the main you only commented every second line.

I was quite unable to understand what you where trying to do, mainly because your comments killed your code. My favorite lines where:
Code:
// set it's layer:
layer = 1;

// no strings:
strings = 0;

// wait one frame:
wait(1);

Yes, I have to agree with you man.. And you aren't actually a first person who told me about my useless comments for each line...
And even more, I have to say that it's became some kind of a paranoia, so I can't code anything without simply commenting each line frown
This comments aren't just useless.. but they are ridiculous in most cases and they ruined the whole idea of what are they were made for.

About the problem I have (SECOND PROBLEM - still not fixed, all other ones are fixed already).
In order to describe the problem, I probably need to explain how the code works (the one I've posted above).

It's pretty simple so far:
Quote:
1. 'file_for_str' reads .txt file line by line till delimiter
2. data which was read from the .txt file is sent into global pointers (vars, strings).
(f.e. you see that 'document_page' in the .txt file, and I have same string in the code, this function will fill up the string with the text from .txt file automatically! this helps me to read such things like 'document title' or 'amount of max pages per document' for each document separately from one .txt file).
This is very simple approach yet it allows me to read unlimited amount of pages and documents from single .txt file and organize them well.
Current problem is, that string wasn't read completely from the file, maybe the loop was broken before the function finished reading it, I'm not sure.
The function that actually reads document text into 'document_page' string is - 'loadDoc(id)', you can see it in the for loop (line 95).
Also right there, before that 'for' loop you can see that I run 'file_for_str' again, that will read the very first line, to get the id for document (document_id).
Thats just a basic reason that I see in my head for this not being viewed correctly, but still if I read the string from the 'file_for_str' function it's correct...

I really hope that you could help me out to track this bug and fix it, or at least give me some ideas where could this bug come from..
I'll look into your example man, thank you for it and for your time as well, but I would like to try fixing my code first, cause I don't want to give up on it.


friendly greets
Posted By: lemming

Re: Three more questions - 08/21/14 22:12

Originally Posted By: 3run
1. 'file_for_str' reads .txt file line by line till delimiter
2. data which was read from the .txt file is sent into global pointers (vars, strings).
(f.e. you see that 'document_page' in the .txt file, and I have same string in the code, this function will fill up the string with the text from .txt file automatically! this helps me to read such things like 'document title' or 'amount of max pages per document' for each document separately from one .txt file).


Why do you write it to global vars/strings? If you want to "read unlimited amount of pages and documents from single .txt file" you'd need unlimited global vars. I think it's the wrong approach. Read them one by one and save them to a list or something.
I just to read your code again and honestly I don't get it. frown I think it is indeed related to your approaches to read a file several times (though I don't exactly know how and why you actually do it) and recommend to start from scratch again and don't use global vars, but a global dynamic list.
Posted By: 3run

Re: Three more questions - 08/22/14 05:43

The thing is, that to check the ID and to read the string we don't need to use array and using global stuff for this is perfectly fine. Cause as soon as I read data (into global var or string), I save it into the array (in original code). I use list to save text's title, id, max page amount and each page (one by one), but I've ripped everything from this example to make it simplier. So it has nothing to do with the problem. Which part exactly you can't understand, I'll try to explain it more with some deeper details.

Greets
Posted By: 3run

Re: Three more questions - 08/22/14 16:17

Maybe this is some kind of an engine limitation? Cause I've found out one very interesting thing... If I change 'document_page' to (let's say) 'doc_page' (which is shorter for 5 characters) this will make 5 more characters of the text visible.. So if before it was visible till 'hopeful' now it will be visible till 'hopefully th' grin so maybe 'var_for_name' can only read a limited amount of characters?

Greets
Posted By: rayp

Re: Three more questions - 08/22/14 16:32

I know, from other languages, theres a string limitation ... let me lie ... around 256 chars, not sure atm. Maybe the display, the text -> string is the problem? If this is bullshit, sry. But Manual says, "strings = no limits". mhhh. Dont know, never done this with lite-c. hope someone takes a look into.

greets
Posted By: 3run

Re: Three more questions - 08/22/14 16:45

Originally Posted By: rayp
I know, from other languages, theres a string limitation ... let me lie ... around 256 chars, not sure atm. Maybe the display, the text -> string is the problem? If this is bullshit, sry. But Manual says, "strings = no limits". mhhh. Dont know, never done this with lite-c. hope someone takes a look into.

greets
Problem is that 'var_for_name' can't read the whole text into the string (that's my guess)..
Maybe I'll need to create an alternative for this thing... frown

Greets
Posted By: 3run

Re: Three more questions - 08/22/14 19:27

Thanks God I've fixed this thing. I've created three small functions to read text titles and pages, but yet I have a small question. I create string pointers inside of this functions (f.e. STRING* textStr = str_cut), this strings are returned, but do I need to remove them at the end? Something tells me that I have to, and if so, what would be the best way to do that? Simple 'ptr_remove' - will be enough?

Greets
Posted By: sivan

Re: Three more questions - 08/22/14 19:33

you can create it runtime like
str = str_create("#4000");
then you can check its real length by str_len(str);

unfortunately I don't know too much about texts I use only txt_for_dir and for file name string inputs... but I would use txt_create() where you can set the number of strings stored e.g. in a separate info file. that could help to avoid length problems, and can save some memory.
Posted By: lemming

Re: Three more questions - 08/22/14 21:58

Originally Posted By: 3run
I've created three small functions to read text titles and pages

So you now have like 8 or 10 functions to read text files to strings? 4 where already in your snippet where you read them to local strings, 1 or 2 you mentioned to read the global variables in a list (I still don't understand why you just don't skip that step) and another 3 for fixing reading the files? Well, at least it works.

Originally Posted By: 3run
, but yet I have a small question. I create string pointers inside of this functions (f.e. STRING* textStr = str_cut), this strings are returned, but do I need to remove them at the end? Something tells me that I have to,

You are not very clear here. Strings (plural intended?) are returned to what? Returned as return value?
And you don't need to delete the string pointers as they are on the stack and get deleted when the function ends, but the strings they are pointing to. But I guess you or something meant that.

Originally Posted By: 3run
and if so, what would be the best way to do that? Simple 'ptr_remove' - will be enough?

Yes.
Posted By: 3run

Re: Three more questions - 08/23/14 02:15

Originally Posted By: lemming
So you now have like 8 or 10 functions to read text files to strings? 4 where already in your snippet where you read them to local strings, 1 or 2 you mentioned to read the global variables in a list (I still don't understand why you just don't skip that step) and another 3 for fixing reading the files? Well, at least it works.
well no, I have 8 functions for the whole thing (updating panels size
/pos, reading titles and creating buttons, adding new documents, reading data and panel events) and only one of them runs in the main game loop, all others run once where they needed. And I don't see any problems here wink plus don't take the example which was posted above to serious, its just an example to show the problem (which has only ONE function that reads data from file, one handles loading process and two others could be even deleted particularly in this example, but I see no reason to care about them either).

Originally Posted By: lemming
You are not very clear here. Strings (plural intended?) are returned to what? Returned as return value? And you don't need to delete the string pointers as they are on the stack and get deleted when the function ends, but the strings they are pointing to. But I guess you or something meant that.
well by saying that they (strings) are returned, I mean that THEY are returned (return(string name)), you are making thing too complicated grin if I would return a value, I would say that they do return a value.. but anyway do I need to remove this string pointers (STRING*) which are created locally in the functions body?

Greets
Posted By: 3run

Re: Three more questions - 08/24/14 18:21

is it just so hard to give me an answer guys?
Posted By: txesmi

Re: Three more questions - 08/24/14 19:12

Hi,
lemming tried to answer you.
Quote:

And you don't need to delete the string pointers as they are on the stack and get deleted when the function ends, but the strings they are pointing to.

The stack is the memory where local variables and function parameters are allocated.

If you create a new engine object and save its memory address in a pointer, there are two actors in the scene: The object and the pointer. All created objects have to be removed, you know. But pointers will probably be local and are automatically managed as simple variables.

Code:
STRING *new_string ( char *text )
{
   STRING *string = str_create ( text );
   return string;
}


'str_create' returns the address of a newly create string and the code saves the address in the pointer. The pointer is local and is allocated into the stack but the string created by 'str_create' is a global object that has to be deleted at the end.

I guess this confusion comes from the literature around liteC. In order to make things easier to understand it is said that the pointer is the object itself. But no, it is not.

Posted By: 3run

Re: Three more questions - 08/24/14 20:29

Thank you a lot txesmi! This is what I call an answer laugh

So from what I understand, I don't need to care about this local pointers (in this situation STRING*), cause they all are saved in the stack (place where all local stuff and func. parameters are saved) and automatically they will be deleted as soon as function ends, but new string (your example) itself will still exist as it was returned (created) as a global object, right?

F.e. I have an empty global string, I copy results of the function (your example) into it, will it create two objects? The one that returned (created) by the function and that global string that I had from the start? If yes, what would be the best way to delete the one that created by 'new_string'? And do I really need to delete it in order to save some memory?
Code:
str_cpy(globalStr, new_string("Hello?");



greets
Posted By: Ch40zzC0d3r

Re: Three more questions - 08/24/14 20:51

Yeah, just do
Code:
str_cpy(globalStr, "Hello?");


And it wont trash your heap.

But if you need to allocate memory on the heap then you HAVE to delete it. If you wont and call it like every frame it will eat your memory in 2 mins xD
Best is to read up on heap and stack and maybe some asm.

Short explenation:
Heap will keep data
Stack will keep data till it calls a return

If you want some bigger explanation I could write a bit, but I guess Sid could also write one of his nice to read posts =D (Enjoy them every time xD)
Posted By: txesmi

Re: Three more questions - 08/24/14 21:34

Originally Posted By: 3run
I have an empty global string, I copy results of the function (your example) into it, will it create two objects? The one that returned (created) by the function and that global string that I had from the start?

Yes. You have two string objects.

Quote:
Code:
str_cpy ( globalStr, new_string("Hello?") );


This code causes the address of the new string get lost because the return of the 'new_string' is not saved so you will not be able to remove the new string.

I think the better choice is to direcly use the new string instead of copying it to another but it depends on the rest of the code xP

Code:
STRING *strResult = new_string ( "hello" );
...
str_remove ( strResult );



Quote:
And do I really need to delete it in order to save some memory?

Yes. Totalmente. Every object has to be deleted before exiting.

Salud!
Posted By: Wjbender

Re: Three more questions - 08/25/14 09:24

http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

in this example the keyword "new" can be replaced with "malloc" or others ..

I like to think of the stack as privately assigned/managed memory per function , and the heap as global managed/assigned memory ..

if you create something within a function scope that
does not get stored globally (it cannot be accessed outside of this function scope) then it should be taken care of when the function scope runs out ,the local (stack) would be taken care of .

if you can globally access memory created within another function its not on the stack, you can take
care of removing it , however there is no harm in destroying a local created object within the end scope of that function if you feel like it I think.

However using malloc seems to be mentioned by the manual as automaticly taken care of upon exit , but this also does not mean you should ignore such objects unless you need them to exist throughout runtime ..

you should create a function to release memory you
created globally ,so that you may call it whenever you
need to ,sometimes you need to destroy a bunch
of created objects before the application exit e.g when
going to a new level you most likely need to get rid
of some stored data.

I am coming off as confusing most likely but I tried.

what becomes confusing here is the word "create"
because , depending on how the created object gets created makes a difference ,if the called function creates the object on the heap you take care of it , if it is on the stack it wil be taken care of as soon as the function that called the "create function" runs out ,I mention this because not all functions that create objects are written the same eg.

you may find a function that is written to create on the heap but called localy within another function ..
Posted By: 3run

Re: Three more questions - 08/25/14 10:22

Originally Posted By: txesmi
I think the better choice is to direcly use the new string instead of copying it to another but it depends on the rest of the code xP

Code:
STRING *strResult = new_string ( "hello" );
...
str_remove ( strResult );



Quote:
And do I really need to delete it in order to save some memory?

Yes. Totalmente. Every object has to be deleted before exiting.

Salud!
txesmi@ thank you again man! This explains a lot! laugh

Originally Posted By: Wjbender
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/
I am coming off as confusing most likely but I tried.

what becomes confusing here is the word "create"
because , depending on how the created object gets created makes a difference ,if the called function creates the object on the heap you take care of it , if it is on the stack it wil be taken care of as soon as the function that called the "create function" runs out ,I mention this because not all functions that create objects are written the same eg.

you may find a function that is written to create on the heap but called localy within another function ..
Wjbender@ thank you for the useful link, that will be very useful for me to learn from laugh

I have one more question guys, still about memory issues but this time about structures.
What will be the best way to remove a structure once we don't need it?
I've seen in some examples people use 'free()', is that a good way to go?

Thank you all once again laugh

Greets
Posted By: Ch40zzC0d3r

Re: Three more questions - 08/25/14 10:58

if you use new you use delete, if you use malloc you use free.
In lite-c you just use ptr_remove and your fine. Just keep and mind not to use local pointers from stack.
Posted By: 3run

Re: Three more questions - 08/25/14 11:01

Ch40zzC0d3r@ sorry, I've missed your post above.. Thank you for informantion anyway laugh

Greets
© 2024 lite-C Forums