Pointer fear

This is a discussion on Pointer fear within the C Programming forums, part of the General Programming Boards category; Okay I'm not confused about any of the syntax of pointers, or related items such as '->' and '&'. What ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Unhappy Pointer fear

    Okay I'm not confused about any of the syntax of pointers, or related items such as '->' and '&'. What worries me is the reference to the heap. In fact it scares the hell out of me. I just need someone to explain what's going on so I can rest at ease. Here's my fear. Say I initialize a char* and store some 256 character string. I write the string, then I create an int* and place who cares at the address its pointing to. What guarantee do I have that the int* isn't going to just randomly point somewhere in the middle of my previous string? How does the compiler decide where to get addresses for these pointers. I know there's malloc, calloc, new delete and free amongst in c/c++, but I frequently see pointers used without. Is it all just on faith that with such a large number of addresses in the heap its "unlikely" you'll piont at or near the same one twice?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    What's to fear about pointers? Are the popular Operating Systems so primitive that they would actually let some simple C++ program effect it's memory in a negative way? The second something goes wrong, the program crashes. You can be sure of it.

    Anyway, some of what you're saying is backwards. You don't create a pointer and place something at the address it's pointing to. You create data in memory, whether standard or dynamic, and then you point to it. Your OS isn't going to place data over data unless you intentionally try to do that. Now, after it's pointing to an address, you can change the value of that memory, at will.

    Code:
    int main() {
       int num = 10;    // This is allocated when you run the program.
       char let = 'a';  // So is this. The OS is not going to overwrite the 10 with this.
    
       int *ptr1 = #  // There, now this is pointing to the address of num.
    
       printf("%d\n", *ptr1); // Your pointer deferenced. Should print 10
       num = 5;
       printf("%d", *ptr1); // Same deal. Now it prints out 5.
    
       return 0;
    }
    Last edited by SlyMaelstrom; 02-27-2006 at 09:10 PM.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by zerbitx
    Say I initialize a char* and store some 256 character string. I write the string, then I create an int* and place who cares at the address its pointing to. What guarantee do I have that the int* isn't going to just randomly point somewhere in the middle of my previous string? How does the compiler decide where to get addresses for these pointers.
    Generally, the programmer takes it out of the compiler's hands and points to somewhere valid or nowhere valid (NULL) instead of throwing caution to the wind.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Working with a pointer without pointing it to something (or nothing) is like working with a data variable that you didn't initalize. It's like walking though the house with the lights off.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Thanks for the quick replys, but I'm still a bit worried... I see things like

    Code:
    char* line = "some arbitrary string";
    Now the integer that is the address of the first character is on the stack. The string its pointing to is on the heap. With the above string (if I counted correctly) there are 24 addresses past the first character (including the null terminator) where another pointer could point to and corrupt the string. I couldn't find the scheme behind the C/C++ compilers that told me how it will choose its next address when you haven't initialized you're pointer. I know using malloc is safer, and I do. My concern is when I see this in other people's code. Are they safe from what I'm worried about or should they worry too?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Show me this code where you see people working blindly with pointers that aren't assigned an address and I'll show you bad code.

    Code:
    char *foo = "Hello World!";
    char *bar;  // You don't know where this is pointing. Like any variable, it's garbage. 
                // It's whatever it was the last time it was used.
    Sent from my iPadŽ

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by zerbitx
    Code:
    char* line = "some arbitrary string";
    This declares a string literal. Nothing wrong with that as long as you don't try to modify the string (you'll likely segfault if you do). If you don't want a string literal, you can assign the string like this

    Code:
      char line[] = "you can modify this string";
    I couldn't find the scheme behind the C/C++ compilers that told me how it will choose its next address when you haven't initialized you're pointer.
    What's not initialized in the example you gave? The string is stored and you have a pointer to access it. The compiler keeps track of what data is stored and what space is available.


    I know using malloc is safer, and I do.
    How is malloc() safer? If you don't trust the compiler to keep track of data storage, then why trust the system to keep track of memory? One offers as much cause to fear as the other.

    My concern is when I see this in other people's code. Are they safe from what I'm worried about or should they worry too?
    Take my advice: stop worrying. You're fretting over nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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