LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading a file

Hi Everybody,
I want to change some lines in the text file. For this, I use malloc.
But when I run the program, it gives me below Fatal error. 
I use cvi 7.0.  
 
 
 
 
 FATAL RUN-TIME ERROR:   "DEGER_DEGISTIR.c", line 102, col 1, thread id 0xFAA56147:   The program has caused a 'Stack overflow.' fault at 0167:00408509.
 
 
 
 
 
 
 
int CVICALLBACK FileScan (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int counter, recount, result, fileindex, i, Textboxnumcounter;
 int firstcomma, bas, son, bul, orta;
 char buf[300];
 char *first_str, *third_str;
 char comp_type[20], comp_name[20], comp_NS1[20], comp_NG[20] ;
 char *main_str;
 char *p[10000];

 
 third_str = "),"; 
 SetMouseCursor (VAL_HOUR_GLASS_CURSOR);               // change the cursor to the hour glass cursor while sorting
    ProcessSystemEvents ();                                                     // Call ProcessSystemEvents so that the new cursor gets displayed
 
 GetNumTextBoxLines (panel, PANEL_FILES, &Textboxnumcounter);  
 for (fileindex=0; fileindex<Textboxnumcounter-1; fileindex++)
 {
  for(i=0; i<10000; i++)
   {
    p[i] = (char *) malloc(200);
    if (p[i] == NULL)
     {
      printf("Not enough memory!..\n");
      return -1;
     }
   }  
   ..............
...................
0 Kudos
Message 1 of 2
(2,927 Views)

Taking a guess, it looks like you may be calling the FileScan callback more than you think.

FileScan has a very large variable declared on the stack (char *p[10000]; ).  You do not have anything limiting your callback to handling only EVENT_COMMIT or the event that you want to use this callback.  When you call ProcessSystemEvents() any queued events will be allowed to run and you may call FileScan again before it has completed the first call.  This would allocate another 10000 pointers on the stack, so I could see where you could get a stack overflow.

A couple of things to try, put if (event==EVENT_COMMIT), or the event you want to use, around the executable part of you code so that it will not be called for any event from the control.

Change the declaration of p to static

static char *p[10000];

By the way, 10000 is a lot of character pointers.  If you are going to allocate 10000 strings of 200 char each, that itself is a chunk of memory off the heap, close to 2GB I think (early in the morning, so my math may be faulty)

 

Message Edited by mvr on 04-10-2006 08:49 AM

Message 2 of 2
(2,919 Views)