Thread: very simple

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    18

    very simple

    hi all...

    just getting into c. trying to understand pointers. reading the C programming language book. i have a few very simple lines of code that produce:

    Bus error (core dumped)

    here is the code:
    Code:
    int main () {
            int k, *p, *m,l;
            k = 10;
            p = &k;
            *p = 23;  //   line 5 
             l = *p;
             *m = l;  // problem line
         
            printf("%d\n",*m);
    
            return 0;
    }
    the cc -Wall doesnt complain. but if i run it i get the above bus error.
    if i coment out the 'problem line' it works fine. don't understand since 'line 5' is pretty much the same statement except instead of actually assigning a number i assign the 'l' variable that holds that number.
    i know that if i change 'problem line' to m = &l; it works fine. i just dont understand why it wouldn't work the other way around. like in line 5.

    is it because *m doesnt even have an address yet?

    i'd appreciate an explanation. thanks....

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Pointers are not storage units.
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > is it because *m doesnt even have an address yet?
    Yes, you need to do m = something, before you do *m = somethingElse

    Just like you need to set var to something before adding to it, say
    var = 0;
    var = var + 1;

    The only difference with pointers is that it's more likely to blow up with a bus error / seg fault if you do it wrong. Most uninitialised values just produce garbage results.
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by toshog View Post
    the cc -Wall doesnt complain.
    That is interesting. I would expect that the compiler can see that m is being used while not initialized.

    I just verified: gcc 3.4.3 indeed doesn't complain, Visual Studio 2005 gives a warning: "uninitialized local variable 'm' used".

    Does anyone have a gcc 4 version around to test this? I'd be interested to know whether that one warns about this.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Detection of uninitialized variable use in gcc is relies on the flow-graph-analyzis, and it's only done if you turn on certain levels/forms of optimization (not sure which level - I know -O2 will do it). I doubt it changes in gcc 4, but rather gcc 3.x can do that already, but not without enabling optimization.

    --
    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 gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Which address "m" should get?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typically you use malloc and free to get an allocated memory block to keep in your 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM