Thread: Character array initialization problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    23

    Character array initialization problem

    Hello,

    I have a character array with 14 elements in my newest version of my program. The same character array is on the same spot in my old version of the program.
    The differences are: On my old version the array gets initialized with 0s. On my new one it has some random negative numbers in it.

    This is ALWAYS the case. Not matter how often I restart both programs.
    I have the feeling that some evil pointer is overriding the array element values. But that's just a feeling. I ran the program several times now and it executed like expected.

    Any idea what this could be?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So your guess is that it has something to do with a pointer, and now you want us to guess?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Where is the array defined? Inside a function or outside up top in "global" space?
    My guess is it's inside a function. If you don't explicitly initialize it, it will have garbage (unpredictable crap) in it.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please post your code in [code] tags

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    No,

    I want to know the known reasons why an array is initialized with 0s in one program and the same one is initialized with numbers in the same program, but newer version.

    I am pretty sure now that it is no pointer problem, as I commented out all new pointers and had the same problem.

    The array is in a function (local). But why the difference between the two program versions?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Unless you're going to help us by showing us relevant portions of the old and new program, then all I can say is perhaps the storage class of the variable changed (static to automatic). You can read the C standard on variable initialization. There is a sticky post here with info on the C standard: C Draft Standards. I suggest using the top link and looking through section 6.7.8. You can also find information about static variables and automatic variables on wikipedia.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by ThatDudeMan View Post
    I have the feeling that some evil pointer is overriding the array element values. But that's just a feeling.
    You need to post some code or we can only guess.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If it's in a local function, an uninitialized array (and I'm just guessing that's where your bug is) results in any undefined values residing there. It's stack. So it is left over stuff from previous stack use. Any change in program flow or the values of previous parameter passing from unrelated calls would affect which values are put there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  4. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM