Skip to main content
gamedev.net
🔒 Locked

malloc

Started by webspynner_99 8 replies 600+ views
Original Post
webspynner_99
webspynner_99
Okay, be blunt. Why do I need to allocate memory? Or, in what situation would I want to allocate memory? Sorry I''m so stupid. I have a cannon, and it launches potatoes. You will bow down to me, or you will eat mashed potatoes at 60mph. EAT AT WAFFLE HOUSE! The Spyders Web JavaScript Games. Check out the JavaScript RPG!
Muzzafarath
Muzzafarath
You need it when you don't know how much memory you need. For example, you can get the user to type in the number of bytes of memory to allocate. You don't know what the user will type in, so you must dynamically allocate it (using m/calloc or new). That's one use of malloc'd memory. Dynamically allocated memory is also useful when doing things like linked lists too.

Edited by - Muzzafarath on September 8, 2000 2:28:12 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
furby100
furby100
quote:
Original post by webspynner_99

Sorry I''m so stupid.




Right you are.


------------------------------
#pragma twice


sharewaregames.20m.com

Poltras
Poltras
quote:
Original post by furby100

[quote] Original post by webspynner_99

Sorry I''m so stupid.




Right you are.


------------------------------
#pragma twice


sharewaregames.20m.com

You not nice, furby! He isn''t, we all asked the question. Maybe differently, but there was a time for all of us who wanted to know the use of dynamic allocation.

Programming is:
A.The art of debugging a blank sheet of paper (or an empty file).
B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.
C.The most fun you can have with your clothes on (although clothes are not mandatory).

Now I know what I'm made of, and I'm afraid of it...
webspynner_99
webspynner_99
quote:
Original post by furby100

[quote] Original post by webspynner_99

Sorry I''m so stupid.




Right you are.


------------------------------
#pragma twice


sharewaregames.20m.com
*raises cannon*
*searches for target*
Ooh.. a furby
*primes cannon*
Eat potato, furby
*fires cannon*
*no more furby*



I have a cannon, and it launches potatoes.
You will bow down to me, or you will eat mashed potatoes at 60mph.

EAT AT WAFFLE HOUSE!
The Spyders Web JavaScript Games.
Check out the JavaScript RPG!

webspynner_99
webspynner_99
Okay, so I would allocate memory for, say, a pointer that would store the name of the user.
So, If I have a 2d array of strings that are meant to be room descriptions for a text based adventure game then I don''t need to allocate memory because it isn''t dynamic. but if I want the player to be able to enter there name or enter commands I will need to allocate memory. But how do I know how much memory I will need to allocate? The only way I know how to find the length of a string is strlen(string). But then I would need to store the string first. But I would need to allocate memory to store the string. But I would need to know the length of the string to allocate memory. But I would need to store the string first.

I have a cannon, and it launches potatoes.
You will bow down to me, or you will eat mashed potatoes at 60mph.

EAT AT WAFFLE HOUSE!
The Spyders Web JavaScript Games.
Check out the JavaScript RPG!
benjamin bunny
benjamin bunny
quote:
Original post by Poltras

You not nice, furby! He isn''t, we all asked the question. Maybe differently, but there was a time for all of us who wanted to know the use of dynamic allocation.




I think furby''s statement was not based on webspynner''s lack of knowledge of memory management, but on the views which he expressed in the "Downright sick" thread.

http://www.geocities.com/ben32768
Ironblayde
Ironblayde
Webspynner: You could allocate space for a user's name, but it's just as easy to use a string with a maximum length on it. I think malloc() is best used when you want to allocate memory you're going to be using as an array, but you don't know how many array elements you're going to need. Or maybe for loading an image, or other things of that nature.

Suppose you were writing an action game or something that reads data for the enemies out of a file, and the data for each enemy fits into a structure you made up called ENEMY. Instead of hard-coding the value for how many enemies are in the game, you could make it a dynamic allocation. That way, you can add or remove enemies from the game without having to change your code or recompile. Let's say the enemy file is called enemies.dat, and is comprised of a single byte representing the number of enemies stored in the file, followed by the enemy structures themselves. You might read it in like this:

    
int LoadEnemies(ENEMY *enemies) // parameter will become your enemy array

{
FILE *file_ptr; // file pointer

int num_enemies; // number of enemies


// open the file

if ((file_ptr = fopen("enemies.dat", "rb")) == NULL)
return(FALSE);


// read the number of enemies

num_enemies = (int)getc(file_ptr);


// now allocate space for the ENEMY structures

enemies = (ENEMY*)malloc(num_enemies * sizeof(ENEMY));
if (enemies == NULL)
return(FALSE);


// read in the enemy data

fread(enemies, sizeof(ENEMY), num_enemies, file_ptr);
fclose(file_ptr);
return(TRUE);
}


Then just don't forget to free() the memory when you're done with it. Hope that helps-

-Ironblayde
 Aeon Software

Edited by - Ironblayde on September 9, 2000 1:10:21 AM
"Your superior intellect is no match for our puny weapons!"
webspynner_99
webspynner_99
enemies = (ENEMY*)malloc(num_enemies * sizeof(ENEMY));
alright, I think I''m starting to get it. One thing, in the code sample, what is this: (ENEMY*). I know that other data types can be used in there, like char and int, but why is that put there with the asteriks after it?

I have a cannon, and it launches potatoes.
You will bow down to me, or you will eat mashed potatoes at 60mph.

EAT AT WAFFLE HOUSE!
The Spyders Web JavaScript Games.
Check out the JavaScript RPG!
Staffan
Staffan
quote:
Original post by webspynner_99

enemies = (ENEMY*)malloc(num_enemies * sizeof(ENEMY));
alright, I think I''m starting to get it. One thing, in the code sample, what is this: (ENEMY*). I know that other data types can be used in there, like char and int, but why is that put there with the asteriks after it?

I have a cannon, and it launches potatoes.
You will bow down to me, or you will eat mashed potatoes at 60mph.

EAT AT WAFFLE HOUSE!
The Spyders Web java-script Games.
Check out the java-script RPG!


It''s a typecast. malloc returns void*. We''re assigning a ENEMY*, so it must be of the type ENEMY* - we need to cast it to the correct type. The * means it''s a pointer. ENEMY* means it''s a pointer to a ENEMY, long* means it''s a pointer to a long etc.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.