Thread: Memory management

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Memory management

    Hi,

    I got an assignment at school asking for a program that can implement functions that store, get and deletes text/binary data to a given memory area. We are supposed to make kind of like a tiny-mini OS.. Do you guys have any links to some tutorials or explanations hon ow to understand this and be able to make a program like this on my own?
    Last edited by norskjente; 09-19-2013 at 06:44 AM. Reason: bad english :P

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your description of the project is not very clear. what have you tried so far?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Quote Originally Posted by Elkvis View Post
    your description of the project is not very clear. what have you tried so far?
    Well, thats the problem. I really do not know where to start.

    We have received a precode with 4 functions and a struct, we have to implement the code.

    The struct:
    The struct is supposed to enable separate storing of text/binary data, you will not have the opportunity to use pointers to other memory areas.

    Function 1:
    return: struct text_t*
    arguments: void *memory, size_t size
    Receives a pointer to the memory area and the size of it. In the function you have to make a struct and return it, that enables you to store data in the given memory area. You can only use the memory area received.

    Function 2:
    return: int
    arguments: sruct text_t *txt, const char *input, size_t input_sz
    Here you have to copy text/binary data in to the memory area given in function one, it will not be enough to just store the reference(pointer). You still can not use external memory areas.

    Function 3:
    return: ssize_t
    arguments: struct text_t *txt, int index, char *output, size_t output_sz
    Her you have to copy the text/binary data that you in Function 2 stored in the memory struct, and you can still not change the reference. The only external memory area you can use here, is the one given in with the parameter output and then you can not exceed the size of this(output_sz). The return value is the amount of memory you overwrote in the parameter output or -1 if an error occur.

    Function 4:
    return: void
    arguments: struct text_t *txt, int index
    Here you have to free the space that is identified by the parameter index, so that new calls on Function 2 can use this.

    Sorry if the english is poor, this is a translation of the assignment we got.

    So, does this help? I really do not know where to look for information - and when I asked my teachers, they didnt know of any good material. I think it is really weird to give us an assignment that we have not learned enough to do - but it is probably to force us to learn a lot on our own.
    Last edited by norskjente; 09-19-2013 at 07:42 AM. Reason: english correction

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Corrections to problem statement:

    Function 3 and 4 do not need an index. There's no index in struct text_t and Function 1 does not return an index.

    Function 4 frees the memory allocated by Function 1 (not Function 2).

    The program should allocate a large amount of memory, perhaps 100 MB or more to use for this memory management program. In addition to the structures used by the functions, you'll need some type of internal structure for the memory. The internal structure would be used to keep track of the "free" memory. This "free memory" structure will contain a pointer to the next "free memory" structure, and the size of the "free memory" for the current structure, followed by the block of free memory. The last "free memory" structure pointer to the next structure will be zero (NULL). The initial state of the free memory will be just one "free memory" structure for all of the memory that the program initially allocates.

    See if you can figure out what Function 1 and Function 4 need to do in order to keep track of the allocated and free memory.
    Last edited by rcgldr; 09-19-2013 at 10:49 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    what fields are in the struct text_t?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elkvis View Post
    what fields are in the struct text_t?
    Based on the problem statement, a pointer to memory and the size. Might as well have the structure text_t immediately followed by the memory it points to, so that the structure text_t and the memory it points to are part of one continuous block.

    A similar structure / memory block arrangement could be used internally for free memory, but the pointer would be to the next structure, creating a linked list of free memory blocks (structure + free memory).
    Last edited by rcgldr; 09-19-2013 at 03:10 PM.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The struct:The struct is supposed to enable separate storing of text/binary data, you will not have the opportunity to use pointers to other memory areas.
    So if anything, it sounds we have a char array or unsigned fields, considering we can't use a pointer.

    Or at least, I interpreted that to mean there's no pointers in the structure.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by MutantJohn View Post
    considering we can't use a pointer.
    The structure has a pointer, void * memory;

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Thank you guys! I think I have managed to make a useful code now

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by rcgldr View Post
    The structure has a pointer, void * memory;
    My apologies, I assume then I misinterpreted the line, "you will not have the opportunity to use pointers to other memory".

    I took that to mean you couldn't use pointers XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory management
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-26-2009, 01:11 PM
  2. Memory Management
    By black_spot1984 in forum C++ Programming
    Replies: 9
    Last Post: 10-08-2008, 11:25 AM
  3. Memory Management
    By rasdfasfd in forum C Programming
    Replies: 2
    Last Post: 12-03-2002, 08:57 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM
  5. memory management>?
    By razrektah in forum C++ Programming
    Replies: 12
    Last Post: 11-30-2001, 09:37 PM

Tags for this Thread