delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/14/10:34:08

From: "DeHackEd" <Not DOT given AT out>
References: <352EDA1E DOT 7B0C55AF AT teccart DOT qc DOT ca>
Subject: Re: Dynamic memory allocation
Date: Sun, 12 Apr 1998 09:50:41 -0400
Lines: 29
Message-ID: <OrlJVshZ9GA.211@uppubnews03>
Newsgroups: comp.os.msdos.djgpp
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

You have 4 options:

1) Just don't let the user put in more than 10
2) Remove unused objects from the list if it gets to big
3) Use a linked list as already mentioned.
4) Malloc, realloc, free.

C++ doesn't have a realloc equivalant (oh no...) to new and delete. Use C
routines instead.

Object **objects;

objects = malloc(sizeof(Object*) * objectsneeded);
for (int i = 0; i < objectsneeded;i++)
    objects[i] = new Object;


// If you need more, realloc it.
objects = realloc(objects, sizeof(Object*)*newobjectsneeded);

You will need to do some casting to get rid of C++ warnings, but I left them out
so you can read this.
--
"DeHackEd"

EMail address not given out due to low-life spammers.



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019