Checking if variable is unitialised

This is a discussion on Checking if variable is unitialised within the C Programming forums, part of the General Programming Boards category; Say I have the following code: Code: char *msg = malloc(sizeof(char*)); msg = NULL; msg = decode_arc4(something here, something here); ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Checking if variable is unitialised

    Say I have the following code:

    Code:
    char *msg = malloc(sizeof(char*));
    msg = NULL;
    msg = decode_arc4(something here, something here);
    
    char* decode_arc4(int* arg1, int arg2)
    {
      // nothing here
    }
    How would I check if msg was uninitialised or not? I'm not meant to modify the function to return anything its just meant to be as-is, I thought about comparing msg with NULL but to no luck since calling the function apparently changes the value of msg. Any ideas?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Mein gott in himmel!

    Code:
    char *msg = malloc(sizeof(char*));
    Inits msg to point to an area of allocated memory the size of a char pointer, which on a 32-bit compiler would be 4 bytes... (msg currently contains an address in memory)
    Code:
    msg = NULL;
    ...Promptly destroys that address, meaning that allocated memory is now forever lost.

    What is it you're trying to achieve here? Do you want space for a string? In that case, you need to be doing:-
    Code:
    char *msg = malloc(64 * sizeof(char));
    Where 64 is a number you specify for the maximum length of your string, one character being reserved for the terminator. Multiplying it by sizeof(char) ensures compatibility with Unicode and other crazy encoding schemes.

    Once it has been successfully allocated (And you should check by comparing it with NULL), the memory pointed to by msg will probably be uninitialised, but there's no way of checking (Compiler/platform specific). Best do:-
    Code:
    memset(msg, 64, 0);
    After allocation to ensure that any string functions you use won't crash.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where 64 is a number you specify for the maximum length of your string, one character being reserved for the terminator. Multiplying it by sizeof(char) ensures compatibility with Unicode and other crazy encoding schemes.
    Except that sizeof( char ) will always give you 1. It's standard. Perhaps you're thinking along the lines of wchar_t ?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Hmm I'm looking for something to check if my variable is initialised, the function call shouldn't return anything - ie. if the function wasn't completed (doesn't return 0 or NULL or anything) I would print something back.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by 0rion
    Hmm I'm looking for something to check if my variable is initialised, the function call shouldn't return anything - ie. if the function wasn't completed (doesn't return 0 or NULL or anything) I would print something back.

    When you declare a function like
    Code:
    char* decode_arc4(int* arg1, int arg2);
    The function will return a pointer to char. It must return a pointer to char.

    Now some C compilers will give a warning if the such a function has a barefoot return statement. (That is a statement return; with no value indicated.) Some C compilers won't even give a warning. Nevertheless, the calling program will always get a value from wherever it expects to get the value from that function. The calling program has no way of knowing whether the called program actually created a valid return value.

    Note that C++ compilers will give an error message at compile time, and the compilation will not be successful.

    Now, quite often, functions that return a pointer to char are designed so that a return value of 0 is used to indicate something special (like some kind of problem, for example). Since 0 can not be a pointer to anything (that is, it is absolutely guaranteed that C programs can not have access to address 0, this is a reasonable way to give a special message back to the calling program.

    Now, here's an example of how such a thing might be put together. I am sure that you can get lots of advice about why it might be better to do something else, But here's an example that you should be able to run.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char* decode_arc4(int* arg1, int arg2);
    
    int main() {
    
      int int1, int2;
      char *save_pointer;
      char *msg;
    
      msg = malloc(100);
      save_pointer = msg;
    
      int1 = 9999;
      for (int2 = 0; int2 < 3; int2++) {
        msg = decode_arc4(&int1, int2);
        if (msg) {
          printf("decode_arc4(&int1, %d) returned: <%s>", int2, msg);
          printf("  and int1 = %d\n", int1);
        }
        else {
          printf("decode_arc4(&int1, %d) returned: NULL", int2);
          printf("  and int1 = %d\n", int1);
        }
      }
    
      free(save_pointer);
      return 0;
    
    }
    
    char *global1 = "This is message 1";
    char *global2 = "This is message 2";
    
    char* decode_arc4(int* arg1, int arg2)
    {
      char *retval;
      if (arg2 == 1) {
        *arg1 = 1234;
        retval = global1;
      }
    
      else if (arg2 == 2) {
        *arg1 = 5678;
        retval = global2;
      }
    
      else {
        retval = NULL;
      }
    
      return retval;
    }
    Notice how I retained the value of the pointer that malloc() gave me so that I could free() it before bailing out.

    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I think this is what you're looking for

    Code:
    #define BUFFER_SIZE 100
    //...
    char *msg = malloc(BUFFER_SIZE);
    memset(msg,0,BUFFER_SIZE);
    decode_arc4(msg);
    
    if(*msg)
      printf("The msg buffer has been changed");
    else
      printf("The msg buffer has not been changed");
    
    void decode_arc4(char *msgbuffer)
    {
      // nothing here
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 01:30 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 09:46 AM

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