Thread: About variables

  1. #1
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    Question About variables

    I'm trying to understand how c variables are stored and deleted...
    I tought that a variable created in a function was erased after the function stop to work.
    But I think i'm wrong because of a code I'm studing like this:
    Code:
    ...
    item *function(int a, int b, int c){
        item *y;
        ...
        return y;
    }
    
    int main(){
        item *x;
        ...
        x=function(a,b,c);
        ...
        return 0;
    }
    Now "x" points to "y", but wasn't "y" erased after function execution?
    I don't know if I explained well what I don't understand... I hope someone could help me.
    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ZaC
    Now "x" points to "y", but wasn't "y" erased after function execution?
    After the call of the function, x does not point to y. Rather, x points to what y pointed to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    y is just a pointer to a location in memory. So the label y is gone after the function finishes, but x will be pointing to the same place as y was

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ZaC View Post
    I'm trying to understand how c variables are stored and deleted...
    Now "x" points to "y", but wasn't "y" erased after function execution?
    I don't know if I explained well what I don't understand... I hope someone could help me.
    Thanks in advance
    If you are coming from some language that does not use either pointers or references, you have a leap to make.

    ps. is that avatar an oil well with its head about to explode??
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Uhm... I see. Thank you very much!
    I got rusty a lot in C programmin, it was so easy... but I can't remember this thing!
    Thanks again

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    As a side note (just mentioning this because it's what came to mind as I read the first part of you questions), variables do go out of scope, yes, but the actual value that was stored in memory is still there until something else overwrites it. So it's entirely possible that if a function is called many times, unless you properly initialize the variables - you're gonna get old data.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, in the eyes of the language, that is not so. It is just undefined--anything can and will happen.
    What you say is probably true of most modern operating systems such as Windows or Linux, but may not be true in other systems...
    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.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It also depends a lot on what code you have in the ... sections.
    If you have:
    Code:
    y = malloc( 10 );
    then the memory that y holds should never get released until you call free() on that memory; but if you have:
    Code:
    item it;
    y = ⁢
    then y is pointing to memory on the stack, which is released once the function returns. Although there's no guarantee that the stack memory will be erased (overwritten) unless you start putting more data onto the stack.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Now I'm confused a little...
    Reading the original code I posted, can I be sure x contains the data y pointed to? Or not? Or does it depends on... what?
    Thanks again

    PS: @ sean, nice note!
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In your original code, x will get a copy of the value of the pointer y, not what was pointed to by y.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    function returns a pointer. In this case it returns a pointer to y, so x becomes a pointer to the same thing that y pointed to. y is gone, but whatever it pointed to is still in the program since x points to it instead.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can be certain that x has the same value as y.
    Whatever data was stored in y will now be stored in x.
    This is different from what it points to.
    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.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    edit: wow - lots of posts since the one I was answering!

    x and y are both pointers. Be careful wherever you see the * and & operators - you might be talking about value, you might be talking about location.

    Since the function returns y, and that is assigned to x, yes, they both contain the same data. But don't forget that pointers contain the location of another value. So we say that x points to the same place as y (like laserlight said).

    In this case it returns a pointer to y
    No, no, no... y is a pointer. The value of y is a memory address, but that value is return to x. A pointer to y does not exist in this code.

  14. #14
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by MK27 View Post
    ps. is that avatar an oil well with its head about to explode??
    It's a person
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  15. #15
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Ok, now, it's more clear! I should repeat a little bit more about pointers
    The last question just to make it clear at all.
    I understood that: variables are destroyed, but not their datas. So I can use the addresses of theese datas to treat them, isn't it? But how can I be sure those addresses will not be used in the rest of the program?

    O.T.: @MK27: it's a person, sorry but i didn't read your ps before. I think oil is f*g all of us. And it's not the only one :/
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM