Game of life: Initializing a struct with two 2D arrays

This is a discussion on Game of life: Initializing a struct with two 2D arrays within the C Programming forums, part of the General Programming Boards category; If i use "if (cells->states == NULL)" it doesn't do anything, thus the malloc function should succeed. The problem is ...

  1. #16
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    If i use "if (cells->states == NULL)" it doesn't do anything, thus the malloc function should succeed.
    The problem is in Array_Get: It can't return the value pointed to by the pointer
    Last edited by Brafil; 12-03-2008 at 10:42 AM.

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > memset(cells, 0, sizeof(*cells));
    And this trashes the pointers to the memory you just allocated in the previous lines.

    Just as we thought we were getting somewhere explaining arrays, you suddenly decide to enter a whole new race on a whole new horse.

    Only this horse is a real buckaroo and you're in for a rough ride!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Yes I know, but I'm starting to feel better with pointers than with arrays. So memset is the bad guy? How can I undergo this?

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    memset the result of malloc, for the amount you asked for.

    Or use calloc(), which achieves the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Wow! Now it works! Thanks for helping a newb!

  6. #21
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476

    New problem

    Sorry, but Now, when I calculate and draw two times, it crashes at the very first Array_Get(prev_row, prev_item) position. What might it be?

    PS: It doesn't really crash, but it says return value 3, and I don't have any exit(3) specified.
    Last edited by Brafil; 12-05-2008 at 09:14 AM.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Most compilers come with a debugger.
    The Visual Studio Express debugger is very nice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    But I remarked that AllocConsole etc. seems to be undefined?

  9. #24
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    EDIT: Solved. I wrote it all again (And tested at every step. It seemed as when I replace some pointers with real variables, it works. What's that? How can I prevent that?)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. 2D Game Programming Tutorials?
    By Zeusbwr in forum Game Programming
    Replies: 9
    Last Post: 11-04-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21