global varibales being owerwritten

This is a discussion on global varibales being owerwritten within the C Programming forums, part of the General Programming Boards category; hello i have defined global variables like this Code: int ntests; int concepttable[numconcepts][TEST_SZ]; int intenttable[numconcepts][predsize]; then i have function initializeintentable ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    5

    Arrow global varibales being owerwritten

    hello

    i have defined global variables like this
    Code:
    int ntests;			
    int concepttable[numconcepts][TEST_SZ];	
    int intenttable[numconcepts][predsize];
    then i have function initializeintentable
    Code:
    void
    initializeintenttable ()
    {
    int i,j,;
    for (i = 0; i < numconcepts; i++)
        for (j = 0; j < TEST_SZ; j++)
          {
    	intenttable[i][j] = 0;
          }
    }
    from another function when i call initializeintentable
    value of ntests before and after call is not same
    i dont know why waiting for a prompt reply

    I am sorry for above stupid question
    as I declare intentable to be of size lesser
    but any how why it didnt gave me segmentation fault
    Last edited by B.S.F.; 12-05-2006 at 10:42 AM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    int intenttable[numconcepts][predsize]; <- predsize

    for (j = 0; j < TEST_SZ; j++) <- TEST_SZ

    unless predsize = TEST_SZ change that

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    because you didn't write to memory you weren't allowed to.
    hence ntests changed. its' your processes memory, it can write to it.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well, stop using global variables. Its not a good idea

    ssharish2005

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    What's with the extra comma?
    Code:
    int i,j,;
    Buffer overruns don't always result in segmentation faults. But they can, and do.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 01:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 08:37 AM
  4. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 11:06 AM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM

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