Memory allocation error under Windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravishi
    New Member
    • Feb 2009
    • 2

    Memory allocation error under Windows

    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:

    Code:
    int *tspos, *tepos = NULL;
    int tcc = 0;
    .
    .
    .
    // the following loop loops about 30000 times
    // and saves the beggining and the ending position
    // of some texts
    loop() {
    	if((tspos=(int *)realloc(tspos,(tcc+1)*sizeof(int)))==NULL)
    		error("Memory error while saving temp text data!\n");
    	if((tepos=(int *)realloc(tepos,(tcc+1)*sizeof(int)))==NULL)
    		error("Memory error while saving temp text data2!\n");
    	tspos[tcc]=spos;
    	tepos[tcc]=epos;
    	tcc++;
    }

    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++ 4.9.9.2 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.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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,

    Jos

    Comment

    • ravishi
      New Member
      • Feb 2009
      • 2

      #3
      thank you

      but the problem was here:

      Code:
      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: http://www.gidforums.com/showthread....0127#post80127).

      so, all I have to do is:

      Code:
      int *tspos, *tepos;
      .
      .
      .
      tspos=NULL;
      tepos=NULL;
      ...
      thanks.

      Comment

      Working...