Thread: segmentation fault on the last printf

  1. #16
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    I don't really understand dynamically assigning memory or what the stack is or where it comes from and even if that is where dynamically assigned memory comes from. because of this and the fact i still trip up on arrays and pointers im afraid to try anything in case i start writing somewhere important and over write the address of my hd or something equally nasty.
    coop

  2. #17
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    861
    Quote Originally Posted by cooper1200 View Post
    I don't really understand dynamically assigning memory or what the stack is or where it comes from and even if that is where dynamically assigned memory comes from. because of this and the fact i still trip up on arrays and pointers im afraid to try anything in case i start writing somewhere important and over write the address of my hd or something equally nasty.
    coop
    You can't write to HDD unless your using system functions to declare where you want to write, in particular driver interface functions since they're what actually write to the HDD if at all (entirely possible to not write at all when running software), but simply put the guys before me have a point about keeping the initialization code of carda out of deal(), in particular I suggest using calloc() to create your card list instead of [52] and put the result of that in a variable called possible_cards, then seperatly reference those cards with a pointer list (also initialized with calloc()), example of what I mean:
    Code:
    card_t InitCardList(size_t num) {
      return calloc( num, sizeof(card_t) );
     }
    void FreeCardList( card_t *cards ) {
      if ( !cards ) return;
      free(cards);
    }
    ...
    int main(...) {
      size_t num = 52, pnum = 4, *phand = NULL;
      card_t *cards = InitCardList(num);
      card_t ***players = InitPlayerList( pnum, &phand, cards, num );
      do { 
        deal( players,  pnum, phand );
      }
      while ( play( players, pnum, phand ) > 1 );
      FreePlayerList( players, pnum, phand );
      FreeCardList( cards );
      return 0;
    }

  3. #18
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Code:
     
    card_t InitCardList(size_t num)
    InitCardList should be returning a pointer, but I am sure that is just a typo - Easy/peezy to fix.
    Code:
     
    card_t *InitCardList(size_t num)
    This is probably the perfect project to learn dynamic memory allocation, because you can use the analogy of taking more cards out of the box and making sure you put all the cards back in the box at the end!

    And if you are using an OS like Microsoft or Linux, it will stop the programme if you accidentally do something wrong.

    If you want to programme C as a job, you will need to be comfortable with dynamically allocated memory

  4. #19
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    im still asking "silly" questions about pointers. as said above i don't fully understand what malloc is and where the memory allocated by it comes from.

    the reason i am learning c is i want to eventually learn embedded systems (after learning c++ and classes etc) as i have a classic car and want to do certain things like cutrosy light dimming wiper delay etc etc while i can do that electonicaly with a timer chip for each system the timing is hard coded where as if it was on an ardino/pi i can do it all from one thing and be able to change it easily

  5. #20
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    861
    Quote Originally Posted by Click_here View Post
    InitCardList should be returning a pointer, but I am sure that is just a typo - Easy/peezy to fix.
    Whoops, yeah a typo

  6. #21
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    861
    Quote Originally Posted by cooper1200 View Post
    im still asking "silly" questions about pointers. as said above i don't fully understand what malloc is and where the memory allocated by it comes from.

    the reason i am learning c is i want to eventually learn embedded systems (after learning c++ and classes etc) as i have a classic car and want to do certain things like cutrosy light dimming wiper delay etc etc while i can do that electonicaly with a timer chip for each system the timing is hard coded where as if it was on an ardino/pi i can do it all from one thing and be able to change it easily
    Well malloc is just as its name implies, a Memory ALLOCator, calloc is similar but it 0 initializes the memory before returning the pointer - equivalent would be memset( malloc( num * Tsize ), 0, num * Tsize ); - as for where malloc is getting the memory from, from my understanding systems put process memory in a file or files when switching out and vice versa when switching back in, malloc I believe adds to this/these file/s using a system defined value then finds a spot in active memory that is available (using the same size) maps that to the aformentioned file/s and then builds a size/next/block style list within this memory and then returns the first available block where size is 0 even if next is not 0, take this with a pinch of salt though since I've only glanced at this sorta stuff while looking for other stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. commenting out printf causes segmentation fault
    By zahid990170 in forum C Programming
    Replies: 7
    Last Post: 10-04-2011, 08:38 AM
  3. Segmentation fault on printf() [NOOB ALERT]
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-26-2011, 06:44 PM
  4. segmentation fault due to "printf" ?
    By MarkZWEERS in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2009, 12:12 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread