uninitialized local variable 'XXXXXX' used

This is a discussion on uninitialized local variable 'XXXXXX' used within the C Programming forums, part of the General Programming Boards category; Hi Guys, This is proabably some stupid error on my part, but I can't seem to see why the following ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22

    uninitialized local variable 'XXXXXX' used

    Hi Guys,

    This is proabably some stupid error on my part, but I can't seem to see why the following piece of code isn't working:

    Code:
    char *starthour, *startmin;
    
    		ifstream starttime;
    		starttime.open ("starttime.txt", ios::in);
    		if (starttime.is_open()) 
    		{
    			starttime >> starthour;
    			starttime >> startmin;
    			starttime.close();
    		}
    The warning I am getting is the following:

    Code:
    warning C4700: uninitialized local variable 'starthour' used
    The same for the majority of the variables in the code.

    Any help is greatly appreciated.
    Thanks

    Brownie

    ### Novice programmer since forever ###

    ### Currently using Microsoft Visual C++ 2008 Express Edition on Windows Vista ###

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Well you declared a pointer, but you didn't make it point at any actual memory (that's uninitialised).
    Then you try and cin a string to wherever it happens to be pointing.

    Some random memory location, but it won't be a good idea to do so.
    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. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you just want a variable to store a string in, you could declare an array of an arbitrary size -- say
    Code:
    char starthour[80];
    But of course, such buffers can always overflow, no matter how large you make them . . . so an alternative solution is to use std::string. You may want to look into it.

    [edit] BTW: that is definitely C++ code, so consider posting in the C++ forum next time, not the C forum. [/edit]
    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.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    Hi,

    I needed to use char * for some of the other functions I was using. For instance strcat was giving out if it didn't have it.

    What you said Salem got me thinking and adding in the following two lines of code fixed the problem.

    Code:
    starthour=(char*)(malloc(sizeof(char)));
    startmin=(char*)malloc(sizeof(char));
    Thanks for the help anyway. Sorry for posting on the wrong forum. I thought I was in the C++.
    Thanks

    Brownie

    ### Novice programmer since forever ###

    ### Currently using Microsoft Visual C++ 2008 Express Edition on Windows Vista ###

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brownie View Post
    Hi,

    I needed to use char * for some of the other functions I was using. For instance strcat was giving out if it didn't have it.

    What you said Salem got me thinking and adding in the following two lines of code fixed the problem.

    Code:
    starthour=(char*)(malloc(sizeof(char)));
    startmin=(char*)malloc(sizeof(char));
    Thanks for the help anyway. Sorry for posting on the wrong forum. I thought I was in the C++.
    Do you think that ONE char is sufficient for the data being entered?

    Also, you seem to be using C++ style input, so you probably should be using new instead of malloc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    Hi mats,

    So you are suggesting something like the following:

    Code:
    starthour = new char[10];
    From the small tests I have been doing on the code, the one char seems to be sufficient, but thanks for the suggestion. Could cause problems later on.
    Thanks

    Brownie

    ### Novice programmer since forever ###

    ### Currently using Microsoft Visual C++ 2008 Express Edition on Windows Vista ###

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brownie View Post
    Hi mats,

    So you are suggesting something like the following:

    Code:
    starthour = new char[10];
    Yes, that would be fine for up to 9 characters entered.

    From the small tests I have been doing on the code, the one char seems to be sufficient, but thanks for the suggestion. Could cause problems later on.
    So, you are not actually entering ANYTHING in the string (as the string termination zero would be taking up the ONE character space you have), or are you relying on the fact that whatever comes AFTER the single character, when it gets overwritten, is not harmful to your program at this time?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    I am entering stuff into the string. It's nothing big, so the 9 characters will handle it.

    Previously I was relying on the fact that what was being overwritten was not harming the computer.
    Thanks

    Brownie

    ### Novice programmer since forever ###

    ### Currently using Microsoft Visual C++ 2008 Express Edition on Windows Vista ###

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    For instance strcat was giving
    you do not need strcat when using std::string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should "static" variable always be local to a file?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2008, 11:51 PM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  3. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Replies: 5
    Last Post: 06-01-2002, 11:24 PM

Tags for this Thread


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