using a pointer without allocation

This is a discussion on using a pointer without allocation within the C++ Programming forums, part of the General Programming Boards category; hi there, i use a pointer without allocating a memeory for it and compiler just gives warning. but it works. ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    17

    using a pointer without allocation

    hi there,
    i use a pointer without allocating a memeory for it and compiler just gives warning. but it works. i use this trick?!(i dont know whether it is) a lot. but is it dangerous? when is it dangerous
    [ i could use malloc and realloc also for an infinitly large array but i juste mean this! ]

    for example:
    ------------------------------------------------------------
    //WARNING
    #include<iostream>
    using namespace std;

    //#define SIZE 100

    int main()
    {
    //int ip[SIZE];
    int *ip;
    int size,sum=0;
    double result;
    cin>>size;
    for(int i=0;i<size;i++)
    {
    cin>>ip[i];
    sum+=ip[i];
    }
    result=sum/(double)size;
    cout<<result<<endl;
    return 0;
    }
    ------------------------------------------------------------
    //WORKS NICE
    #include<iostream>
    using namespace std;

    #define SIZE 100

    int main()
    {
    int ip[SIZE];
    int size,sum=0;
    double result;
    cin>>size;
    for(int i=0;i<size;i++)
    {
    cin>>ip[i];
    sum+=ip[i];
    }
    result=sum/(double)size;
    cout<<result<<endl;
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i use a pointer without allocating a memeory for it and compiler just gives warning. but it works. i use this trick?!(i dont know whether it is) a lot. but is it dangerous? when is it dangerous
    C and C++ could care less if you use your pointers uninitialized. It's not their job to make sure you aren't going to crash your program or crash the OS. However, when you declare a pointer, and don't implicitly point it at something, it points to some random block of memory.

    As such, you have no idea what you're pointing at. You are not overwriting whatever is there in memory.

    It's possible that you're now overwriting portions of memory that my program want to use.

    It's possible that that is free memory out there some place, that another program may grab that and allocate it. At which point in time, you'll get a nice little crash when you next try to reference it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    17
    yeha, u are right.
    in my homework projects of course i use "new" and "delete" i mean i allocate. because it is my job to learn program in a best way.
    i just use this method in my little codes for exercising.
    so you say that it will give an error?! or you say that it is a luck to run that.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A few lost bits of memory here and there probably won't kill your computer. Accessing unallocated memory is most definitely dangerous though. You may be writing to memory belonging to the system or to other programs. This will not cause your program to crash, but may cause unexpected behavior or crashes in other programs or the OS.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Zach L.
    You may be writing to memory belonging to the system or to other programs.
    Actually, under modern OS's, you cannot write (by traditional methods) to memory that is owned by another process. Each process "lives" in its own virtual memory space, so address 0x1234, to my process, is not the same as address 0x1234 to yours, and neither is necessarily stored at address 0x1234 in physical RAM. This is NOT the way that 16 bit Windows operating systems worked, but since the beginning of Win32 (Win95 & NT4), it's been the way every operating system of Microsoft's has worked. Linux, at least modern versions, are the same (I don't know the history of their memory managers, though.)

    Under DOS, or 16 bit versions of Windows, though, any program could write anywhere into physical memory that it wanted, which could easily cause one program to write over system/other programs.

    However, you are correct that writing to a pointer that points at unallocated space is a *very bad* idea. You could be writing to anything. Sometimes it will work (when the pointer points at memory that has been defined as having write access), sometimes it won't, and sometimes it will crash.

    Bottom line is, you should ALWAYS make sure your pointers point to *something* before you use them -- either memory you allocate for that purpose, or memory that was already allocated, etc.

    If you don't allocate, it's anyone's guess if it works or not. Sometimes it will, sometimes it won't. The same program could work today but not tomorrow. What happens when that code runs is totally unpredictable, and nondeterministic -- you can get different results from the same code, because you cannot predict where the pointer will point.
    Last edited by Cat; 05-30-2003 at 03:03 PM.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686

    Talking

    I stand corrected.

    I know that UNIX, Linux, and newer Windows OS's use virtual memory. I do remember, though, that in Win95, it was possible to write to system owned memory (there was a weak safeguard, but not difficult to get around).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It's still possible to do, but it requires more than just dereferencing a pointer.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah... The Win95 one was simply dereferencing a pointer. The only thing was, you had to select a high enough memory address, otherwise the OS would 'notice'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    vVv: you implied that most of this doesn't apply to MacOS. How is it different? I'm just curious.

    Also, if each program has its own memory space, and they may map differently, does this mean it's impossible, for example, to send a pointer from one program to another? I.e. Allocate memory with program A and use it with B, by passing its address between them? Thinking more about it, I don't even know how you'd pass the address anyway, but it's still an interesting thought (to me, anyway).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  2. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  5. pointer and memory allocation trouble
    By volk in forum C++ Programming
    Replies: 27
    Last Post: 05-17-2003, 12:14 PM

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