Thread: Reallocing gives me a segmentation fault...

  1. #1
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259

    Reallocing gives me a segmentation fault...

    I try to dynamacly allocate memory during run time since i don't know how much memory I need when I start the prog since it is up to the user (trying to make a text editor) so I have to dynamacly allocate memory alot, I have made a board search but didn't find anything which made the program work and i googled it. Anyway here is the code which won't work:

    Code:
    if (cheak>=pline)
    /*kolla ifall mer minne behövs*/
     {
     wprintw(win," %d %d",pline,cheak);
     pline++;
     wprintw(win," %d",pline);
     
     store=realloc (buff,pline*sizeof(int));
     if(store == NULL)
      {
      endwin ();
      printf("Not enuf free memory\n");
      exit(0);
      }
    
     buff=store;
     buff [pline]=calloc((pcol+1),sizeof(int));
     if(buff [pline] == NULL)
       {
       endwin();
       printf("not enuf free memory\n");
       exit(0);
       }
     
     free(store);
     store=NULL;
    
     wprintw(win,"allocating ok! %p %p %p",store,buff [pline],buff);
     }
    buff [cheak] [j-1] = ch;
    both buff and point are int **. I have tryed everything I can think of which might work...

  2. #2
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    ah okey thx a ton for the help Should I change it when i malloc the memory for buff the first time so it is sizeof(int*) there aswell?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  3. #3
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    it still gets segmentation fault
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  4. #4
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I've done some small changes now (added a new variable to keep track of somthing instad of using an other one ment for somthing else (and set cheak back to 0 (it keeps track of where I should write)) and now I can print 3 rows and then it my wprintw statement comes that it allocated ok and when I am at the end of the 4:th row it crashes, although I try to print somtihng on that line I get segment fault. I guess I get it when I try to do the buff [cheak] [cheak2] = ch; so...is there any better way to cheak if I got the memory then the one I'm using?
    Last edited by Shogun; 08-08-2003 at 08:54 AM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
     store=realloc (buff,pline*sizeof(int));
    
     ...
    
     buff=store;
     buff [pline]=calloc((pcol+1),sizeof(int));
    Let's say pline equals 5. Five elements means you can use elements 0 to 4. You use element five. Bad idea.

  6. #6
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    ah okey forgot that! thx a ton for the help!
    Last edited by Shogun; 08-08-2003 at 01:19 PM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM