Uninitialized pointer usage: one OK, other NOK?

This is a discussion on Uninitialized pointer usage: one OK, other NOK? within the C Programming forums, part of the General Programming Boards category; Hello to everyone! My first post, so be gentle. Here I have two snippets: first should be avoided (unpredictable behavior), ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Question Uninitialized pointer usage: one OK, other NOK?

    Hello to everyone! My first post, so be gentle.

    Here I have two snippets: first should be avoided (unpredictable behavior), second should be fine. I do not understand why is it so. (I have some knowledge of assembly (language) and computer architecture.)

    First snippet:
    Code:
    char *k;
    strcpy(k, "ABCD");
    Second snippet:
    Code:
    char *k;
    k = "ABCD";
    Last edited by courteous; 04-20-2010 at 02:25 AM. Reason: [CODE]

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    In the first you are copying string ABCD to the memory location pointed to by k. However k is not pointing to a valid memory location because it is not initialized to point to one, nor is there enough space at that location to accept a string.

    The second snippet pointer k points to a constant string. In this case it is not the string that is transferred to the memory location pointed to by k, but rather k made to point at the memory location where ABCD resides. That is fine, however k is 'read-only' in this case. You cannot change for example k[1].

    Also when you post make sure to use CODE and /CODE tags between [] instead of PHP tags.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Quote Originally Posted by claudiu View Post
    In the first you are copying string ABCD to the memory location pointed to by k. However k is not pointing to a valid memory location because it is not initialized to point to one, nor is there enough space at that location to accept a string.
    Doesn't char *k; make k point to some "random" memory location? What does this statement really do behind the scenes? And how do you know that "there is not enough space"?

    What is valid memory location, or for that matter, invalid? Is it, for example, memory range from [0] to [2^31-1] (for a 32-bit OS) and invalid is thus any location out of this range?

    Quote Originally Posted by claudiu View Post
    The second snippet pointer k points to a constant string. In this case it is not the string that is transferred to the memory location pointed to by k, but rather k made to point at the memory location where ABCD resides. That is fine, however k is 'read-only' in this case. You cannot change for example k[1].
    Got it: k is made to point somewhere (not vice-versa). Again, what char *k; (physically) really does?

    Quote Originally Posted by claudiu View Post
    Also when you post make sure to use CODE and /CODE tags between [] instead of PHP tags.
    Corrected. Though [CODE] doesn't color-highlight.
    Last edited by courteous; 04-20-2010 at 02:40 AM. Reason: typo

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by courteous View Post
    Doesn't char *k; make k point to some "random" memory location? What does this statement really do behind the scenes? And how do you know that "there is not enough space"?
    That's the point. k points to who-knows-where. There is a 99% chance that there isn't any allocated memory there. If there isn't, the application will crash.
    But sometimes there can be. That's why it's called undefined behavior. The standard doesn't dictate what will happen.

    What is valid memory location, or for that matter, invalid? Is it, for example, memory range from [0] to [2^31-1] (for a 32-bit OS) and invalid is thus any location out of this range?
    A valid memory location is any location of allocated memory. We have about 2 GB to use in a 32-bit system, but remember that we're dealing with virtual memory. We could theoretically write anywhere in physical memory, but in virtual memory, the OS controls where we can and cannot write.
    So wherever you have allocated memory is valid and everywhere else is not. That's basically it.

    Got it: k is made to point somewhere (not vice-versa). Again, what char *k; (physically) really does?
    The string literal you try to assign is stored somewhere. In a read-only memory section of the file. So what the compiler does is take the address where that is stored and stores it inside the pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Elysia basically explained everything more thoroughly. Thanks Elysia!

    Saying char *k; literally means here is a pointer to char. If you wanted k to point to VALID memory location then you could allocate memory using malloc and make k point to that location like so:

    Code:
    /* allocate a chunk of memory of 100 chars and make k point to it */
    k = malloc(sizeof(char) * 100);

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    You can also set it to point to a local variable.
    So long as it points to some valid storage location, either via malloc or by some other means, it's fine to use it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Is char size == 2, that is, 'a string literal' + '\0'? Or is there no universal definiton of its size?

    Thank you claudiu and Elysia ... there really should be a "Thank you!" button.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by courteous
    Is char size == 2, that is, 'a string literal' + '\0'? Or is there no universal definiton of its size?
    sizeof(char) == 1

    It is when you deal with string literals that you +1 for '\0'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer usage problem.
    By Swerve in forum C++ Programming
    Replies: 11
    Last Post: 10-17-2009, 07:11 PM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. deferencing uninitialized pointer
    By Bontrey in forum C Programming
    Replies: 7
    Last Post: 02-21-2008, 12:48 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 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