Working with dynamic memory in C++

This is a discussion on Working with dynamic memory in C++ within the C++ Programming forums, part of the General Programming Boards category; hi! I'm working on a project which I can't use vectors, lists, etc... but only dynamic memory :-( Being new ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Working with dynamic memory in C++

    hi!

    I'm working on a project which I can't use vectors, lists, etc... but only dynamic memory :-(
    Being new to C++, i don't know how to "save" blocks of data in dynamic memory.

    Theres what i need to do:
    I have to make a program for a grocery store. In this store the clients can order products and what i need to keep in dynamic memory are each clients orders.

    Order Data:
    - Client Name
    - The products the client wants
    - Total price

    But i don't know how to do this without using vectors or lists...
    Can anyone help me?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by IndioDoido View Post
    I'm working on a project which I can't use vectors, lists, etc... but only dynamic memory :-(
    Sounds like a class assignment, right?

    But i don't know how to do this without using vectors or lists...
    A basic vector is pretty easy to implement. Look for tutorials. You should be able to find one.

    Once you have that, just solve it how you would have done with std::vector. After all, they can't fault you for properly encapsulating your dynamic memory management, can they?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey CornedBee, thanks for the fast reply ;-)

    yap! it's a class assignment :-S

    i don't really know if they mean i can't use the std::vector, or both (basic vector and std::vector). I have to ask them.

    Anyway, can std::set be used as an alternative to std::vector? Or am i saying something really stupid? :-S
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    std::set is a map, from what I understand, but std::vector is a vector, or an array. Two different things.
    A map can be a good thing to find a certain item, so to speak. Say, you lookup your client's name and find the total price, etc.
    A vector is simply a class to store information, much like an array. Good when you don't need to lookup something by things as name. In vectors, indexes are used.
    Although a vector is much faster than a map if you wish to retrieve data.
    Last edited by Elysia; 10-29-2007 at 07:00 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I doubt you can use any standard container if they specifically say don't use vector or list, which means that set and map are likely out.

    Also, vector is the same as std::vector. Those are just two different ways to refer to it.

    As far as what to do, do you know how to create a dynamic array? You should have a class that stores the data in a dynamic array. It should be able increase the size of the array and delete the array in the destructor. If you're smart, you'll actually make the class that handles the resizing and construction and deletion separate from the classes that handle the grocery stuff as CornedBee suggested, but I doubt that is necessarily expected for the assignment.

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    Thanks for the explanation Elysia, i was really saying something stupid :-)

    hi Daved, i have already created a class orders, example:

    Code:
    class order
    {
    	string clientName;
    	unsigned int numProducts;
    	unsigned int orderID;
    	unsigned int productID;
    unsigned int totalPrice;
    
    public:
    	...
    
    };
    now, is this what you mean with dynamic array?
    Code:
    order *arrayOrder;
    Last edited by IndioDoido; 10-29-2007 at 07:32 PM.
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sounds like your teacher wants you to create your own vector or list structure...
    Do some reading about Linked Lists. They're pretty simple once you see it.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> is this what you mean with dynamic array?
    That's a start. It's a declaration of a pointer that can point to a dynamic array if you make it. Now how would you actually create the dynamic array and have arrayOrder point to it?

  9. #9
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Daved

    yes! that's it...
    "Artificial Intelligence usually beats natural stupidity."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory alloction for my array of struct
    By pears0 in forum C Programming
    Replies: 13
    Last Post: 03-11-2005, 10:53 AM
  4. dynamic memory allocating error
    By valhall in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 09:49 AM
  5. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 07:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21