Results 1 to 4 of 4

Thread: Character arrays and pointers

  1. #1
    tbarnette
    Guest

    Character arrays and pointers

    I have a few questions here...

    char *something
    char otherthing[50];

    something allocates a pointer to a character. I've seen strings/character arrays passed around using these. How much storage does this really offer? Is it safe? I can actually point to a string that I haven't allocated storage for with this?

    otherthing allocates a character array, I'm comfortable with the storage here. However, I can't return a function to a character array. I seem to have to use a character pointer (*something).

    One final question in regards to character pointers:
    char *SomeFun(){
    char *something
    return something
    }

    If I call this function several times to return a character pointer will my storage be ok? I'm not eating memory, right?


    (I know..basic ?'s. Thanx for your patience (and answers..<G>))

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay, using your two example lines of code:

    Code:
    char *something;
    char otherthing[50];
    'something' here will be, as far as the computer is concerned, a 32-bit variable on the stack. What's stored in this is the address of (the same as 'a pointer to') any char variable you specify.

    So if we take this line:
    Code:
    char *something;
    At this point, you have reserved space for a 32-bit pointer on the stack. That's it. You haven't reserved any space for whatever this pointer might point to. So if you dereference this pointer and try to do anything with what it's pointing too, chances are you're going to get an access violation, because what's in that pointer variable icould be anything, since you haven't initialised it.

    Now, the second line:
    Code:
    char otherthing[50];
    This is reserving space for 50 8-bit char variables in a row on the stack. 'otherthing' is a pointer to a char variable, just like 'something' is in the first line, but 'otherthing' is pointing to allocated memory on the stack that you can safely use however you like.

    Your SomeFun() function:
    Code:
    char *SomeFun(){
    char *something
    return something
    }
    In this function, a 32-bit pointer will be allocated on the stack, and the value of that pointer (the address of whatever it points to) will be returned by the function. As soon as the function exits, the 32-bits of memory on the stack are deallocated (since 'something' is out of scope now) and not legitimately yours to use any more. There is never any memory allocated for the pointer to point to, so no memory is leaked.


    If that's not clear, or if it missed the point of the question, jsut say
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    tbarnette
    Guest
    Thanks Harry, makes sense... That was my thinking but I just saw some code that receives what appears to be a string into a character pointer - uses, without ever really ever allocating any real memory for it.

    In the previous example, it was like
    Something = function();

    even though Something is only a pointer, and the value returned from function is destroyed after function ends... Something still holds a value afterwards. I didnt think that was possible.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well it will be passed back by value. The variable itself in the return statement goes out of scope, but it's the value that is passed back, not the variable.

    So it sounds like the function you are talking about passes back an address, which is fine. In the function that returns that address, the pointer that holds it will go out of scope when the function ends, but the value of that pointer (the address it holds) can be passed back from the function and assigned to another variable that is still in scope.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width