By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Memory allocation error under Windows

P: 2
Well, this is my first topic at this forum and I'm a newbie on C programming too.

I'm coding a little program and I've used some "dynamic arrays" on it. Compiling and running the program on Linux worked fine for me. But I was very curious to see how the program runs on Windows, so I decided to test it. Compilation was fine too, but when I run the program, I get a memorry allocation error.

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. int *tspos, *tepos = NULL;
  2. int tcc = 0;
  3. .
  4. .
  5. .
  6. // the following loop loops about 30000 times
  7. // and saves the beggining and the ending position
  8. // of some texts
  9. loop() {
  10.     if((tspos=(int *)realloc(tspos,(tcc+1)*sizeof(int)))==NULL)
  11.         error("Memory error while saving temp text data!\n");
  12.     if((tepos=(int *)realloc(tepos,(tcc+1)*sizeof(int)))==NULL)
  13.         error("Memory error while saving temp text data2!\n");
  14.     tspos[tcc]=spos;
  15.     tepos[tcc]=epos;
  16.     tcc++;
  17. }

That is the part. I compiled it and when I run it I get the message "Memory error while saving temp text data!"

On Linux I was using the gcc compiler. On Windows, I'm using Dev-C++ with Mingw/GCC 3.4.2.

Ss I said, it is compiled on Windows, but I get the memory error when try to run.

I think that it's because of the differences between Windows/Linux memory allocation and need to say that my program uses "a lot of memory" (more especificaly, something about 25MiB).

So, why did it occurs? It's because of the "malloc/realloc" inside a big loop?

Thanks for the answers.
Feb 11 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 10K+
P: 11,448
Maybe you do actually run out of memory when running on MS Windows; one little thing: why don't you initialize tspos to NULL when you start your loop?

kind regards,

Feb 11 '09 #2

P: 2
thank you

but the problem was here:

Expand|Select|Wrap|Line Numbers
  1. int *tspos, *tepos = NULL;
according to another answer that i received, "tepos has a value of NULL, tspos has an undefined value. GCC might handle that more gracefully by default than Dev-C++" (ref:

so, all I have to do is:

Expand|Select|Wrap|Line Numbers
  1. int *tspos, *tepos;
  2. .
  3. .
  4. .
  5. tspos=NULL;
  6. tepos=NULL;
  7. ...
Feb 11 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.