Thread: Valgrind Invalid Read/Write In C Program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Valgrind Invalid Read/Write In C Program

    Greetings,

    I have recently been programming a small Cell Simulation to be used as the background of a menu in a game.

    I was under the impression that the Updating/Drawing function "MenuAnimation()" worked correctly, however when testing for a memory leak in another function I discovered that Valgrind reports many Invalid Read/Writes throughout the duration of the function.

    I am somewhat perplexed as to what could be causing these, as they seem to occur for no reason what so ever, lines of code that bear no relation to one another will both throw the same Invalid Read Or Write.

    I have attached a small test program which contains only the Resetting/Updating and Drawing functions from the game and the Valgrind log.

    Any help you can provide on the matter would be greatly appreciated.

    -Alex Richman.

    log.txt
    test.c

    P.S: I am compiling using GCC version 4.5.2 on Ubuntu 11.04.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your array (on the stack) is huge - 800×680×3×4 = 6,528,000.

    > for(z = 0; z <= 3; z++)
    And you overflow it by a huge margin as well.

    Since it isn't recursive, you could just make the array static.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Thank you very much Salem,

    I feel rather silly now ^^

    Reguards,

    -Alex Richman.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid Read from Valgrind
    By jduro in forum C Programming
    Replies: 1
    Last Post: 10-05-2010, 11:28 AM
  2. Read from txt and write in an external program
    By sombrancelha in forum C Programming
    Replies: 8
    Last Post: 06-01-2009, 09:04 PM
  3. C program file read/write help
    By BobDole11 in forum C Programming
    Replies: 5
    Last Post: 10-13-2008, 09:55 AM
  4. Can I write & read file in same program?
    By chad03 in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 08:39 AM
  5. Read/Write a excel file using C program & OS is DOS
    By akhitesh in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 11:07 PM