Hi again. I'm here to wreck your world.
Just kidding.
How's your new year, all of you? Going on swell? Eid Mubarak to all Muslims.
Bypassing the small talk, I'm here to query you on a doubt I have in dynamic initialization.
As I have learnt in school (correct me if I'm wrong), dynamic memory is allocated from a heap or a free memory. I'm running a project code (a C++ simulation), and I require to determine how much free space I already have, and that C++ can use, so that my program can initialize 'creatures' and their 'food', the progress of which I'm charting with a linked list.
I.E. How do I determine how much free space the heap has remaining?
If anyone has the book Turbo C++ Techniques and Applications by Scott Robert Ladd (c. 1991) from which this program is *derived*, it would be even more helpful. Hopefully I'm not asking too much.
I work in Turbo C++, version 3.0, Windows XP (mismatch, yes, I know). Would it help if I paste all 1100 lines of code here? :rolleyes: :D
Thanks for any assistance,
Me. :o :eek: :mrgreen:

>I'm here to query you on a doubt I have in dynamic initialization.
You sound like less of an idiot when you speak clearly. "I'm here to ask you a question I have about dynamic memory" is much better than trying to sound smart by using uncommon and inappropriate words.

>dynamic memory is allocated from a heap or a free memory.
Heap, pool, free store, it's all just a big block of memory that you can grab chunks from.

>I require to determine how much free space I already have
Require is an awkward choice of words, what's wrong with "need" or "want"? Though "want" would be better because I very much doubt that you "need" to know. If you do "need" to know then your design is probably flawed.

>How do I determine how much free space the heap has remaining?
When new throws a bad_alloc exception, you can assume that you're out of dynamic memory. However, virtual memory goes a long way toward making such exceptions nearly impossible. For that reason, it's safe to assume that the amount of available dynamic memory is limited only by storage space and your tolerance for thrashing.

>mismatch, yes, I know
If you know, why do you continue to use a relic compiler on a modern operating system? Are you just a masochist?

>Would it help if I paste all 1100 lines of code here?
Sure, why not? Everyone will ignore you anyway, so it doesn't matter how much code you paste into your thread.

Well. Gee. Thanks. Lol.
I didn't mean to sound smart, I was just gabbing.
I am not a masochist, blame it on my educational system. They're the ones forcing FoxPro, C++, Q-Basic, V-Basic on us, on modern operating systems.
I am not going to paste all 1100 lines of code. I am, however, going to reiterate my question, and add another one:
Something's wrong with my randomization. I'm using the srand((unsigned) &t) statement for a time_t time variable, for randomizing the seed, but it ain't workin' 'cuz I get the same creatures at the same places, which is so dayum weird.
Thank you for any help. Again. Thank you bearing me.
Me. :cry: :-| ;)

>I am, however, going to reiterate my question
My answer probably wasn't clear enough. In general, if you need to know, your design is probably flawed. Since this appears to be an assignment, none of the exceptions apply to you and your design is definitely flawed.

>I'm using the srand((unsigned) &t) statement for a time_t time variable
If t is the time_t variable then you're use its address as the argument to srand, not its value. The cast is unsafe anyway because pointers aren't guaranteed to be representable by an unsigned int. Consider this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  time_t now;
  int i;

  now = time(NULL);
  if (now == (time_t)-1) {
    fprintf(stderr, "Cannot calculate a random seed\n");
    return EXIT_FAILURE;
  }

  srand((unsigned int)now);

  for (i = 0; i < 10; i++)
    printf("%d ", (int)((double)rand() / RAND_MAX * 100));
  printf("\n");

  return EXIT_SUCCESS;
}

But it's still not perfect because time_t isn't guaranteed to be representable by unsigned int either. time_t is a restricted arithmetic type, and you can only assume that comparison with (time_t)-1 will work. As such, a more thorough approach is needed for robust code:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Hash the bytes of a time_t value */
unsigned int rand_seed(time_t t)
{
  unsigned char *p = (unsigned char *)&t;
  unsigned seed = 0;
  size_t i;

  for (i = 0; i < sizeof t; i++)
    seed = seed * (UCHAR_MAX + 2U) + p[i];

  return seed;
}

int main(void)
{
  time_t now;
  int i;

  now = time(NULL);
  if (now == (time_t)-1) {
    fprintf(stderr, "Cannot calculate a random seed\n");
    return EXIT_FAILURE;
  }

  srand(rand_seed(now));

  for (i = 0; i < 10; i++)
    printf("%d ", (int)((double)rand() / RAND_MAX * 100));
  printf("\n");

  return EXIT_SUCCESS;
}

Of course, the first program will almost always work, it just doesn't have to according to the language definition. Anyway, your problem was that t's virtual address is always the same, so the seed passed to srand never changes.

This article has been dead for over six months. Start a new discussion instead.