Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What's wrong here??? Pointer question

What's wrong here??? Pointer question

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tim Deveaux

    If you have only 10 items in the array, use ; i < 10 ; in the loop (0 to 9 are valid indices). A common gotcha, to be sure.

    R Offline
    R Offline
    Richard Cheng
    wrote on last edited by
    #3

    Actually, I'm looking for a infinite size of "array", so I want to see if i can use pointer to do that.......sometimes i need 10....but sometimes i need 40......that's why i create pointer..... Do you know how?

    T 1 Reply Last reply
    0
    • L Lost User

      I can display that 0-10 numbers....but then get a memory error message.....what's wrong? int *pPtr; pPtr = new int; CString a; for (int i=0; i<=10; i++) { *(pPtr+i) = i; Display Pointer here....... } delete pPtr;

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #4

      You've allocated space for one integer with 'new int'. You're trying to write 11 integers using *(pPtr + i). If you want this to work OK, you should use 'new int[11]' Tomasz Sowinski -- http://www.shooltz.com.pl

      1 Reply Last reply
      0
      • L Lost User

        I can display that 0-10 numbers....but then get a memory error message.....what's wrong? int *pPtr; pPtr = new int; CString a; for (int i=0; i<=10; i++) { *(pPtr+i) = i; Display Pointer here....... } delete pPtr;

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #5

        Unless I'm mistaken here, the line of code causing the memory error is *(pPtr+i) = i; You are trying to cause the pointer to point to memory that you have not allocated. Too boot, you then attempt to change the contents of that location by doing the assignment operation. Chris

        L 1 Reply Last reply
        0
        • C Chris Meech

          Unless I'm mistaken here, the line of code causing the memory error is *(pPtr+i) = i; You are trying to cause the pointer to point to memory that you have not allocated. Too boot, you then attempt to change the contents of that location by doing the assignment operation. Chris

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          But I don't want to fix the number of elements......I want to create an infinite "array"......I can insert elements in this list unlimited........ Do you know how to do that????? THANK YOU

          T C M 3 Replies Last reply
          0
          • L Lost User

            I can display that 0-10 numbers....but then get a memory error message.....what's wrong? int *pPtr; pPtr = new int; CString a; for (int i=0; i<=10; i++) { *(pPtr+i) = i; Display Pointer here....... } delete pPtr;

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            The prolbem is that you are allocating enough memory to hold one int item only!! so your code is accessing invalid memory! the statement : *(pPtr+i) = i; //this is going beyond the memory pointed to by pPtr!! To create a dynamic array, you need to know the number of elements in it and then allocate it: void MakeArray(int nCount) { int * pPtr = new int[nCount]; if (pPtr) { for (int i=0; i < nCount; ++i) { *(pPtr+i) = i; //This is ok pPtr[i] = i; //is same as above! ..... } //Free the memory delete [] pPtr; } } Mh2! :)

            R 1 Reply Last reply
            0
            • R Richard Cheng

              Actually, I'm looking for a infinite size of "array", so I want to see if i can use pointer to do that.......sometimes i need 10....but sometimes i need 40......that's why i create pointer..... Do you know how?

              T Offline
              T Offline
              Tim Deveaux
              wrote on last edited by
              #8

              Ah - ok - didn't look at your code carefully enough. What you are doing is going to act differently in debug, release, and where the pointer is declared (in a function (on the stack) or global (on the heap). You will be able to store to the pointer as long as the increment doesn't take it outside of a valid memory location (which would be an access violation). But you will probably end up overwriting some other important data, which can corrupt the heap or the stack. What you are looking for is, I think, a dynamic array - one which you can assign values to and have the array allocate more memory 'on the fly' to accomodate if necessary. The STL's vector would be useful in this case - very handy. If you'd like to roll your own, you might want to look at MSPutils.h (in the platform SDK), which implements a simple placement new based container for Plain Old Data (POD) types - A related class is CSimpleArray, which extends this to classes with constructors and destructors. But I think you should look at CArray (MFC) or vector (STL).

              1 Reply Last reply
              0
              • L Lost User

                But I don't want to fix the number of elements......I want to create an infinite "array"......I can insert elements in this list unlimited........ Do you know how to do that????? THANK YOU

                T Offline
                T Offline
                Tomasz Sowinski
                wrote on last edited by
                #9

                > I want to create an infinite "array" You'll need a computer with infinite memory. Not all users of your application will be able to buy one. Tomasz Sowinski -- http://www.shooltz.com.pl

                1 Reply Last reply
                0
                • L Lost User

                  But I don't want to fix the number of elements......I want to create an infinite "array"......I can insert elements in this list unlimited........ Do you know how to do that????? THANK YOU

                  C Offline
                  C Offline
                  Chris Meech
                  wrote on last edited by
                  #10

                  The best that I think you can do is use some program variable that is set. It is difficult to contemplate what an infinite size array might be ;) int* pNumber = new int [SomeProgramVariable]; for (inti=0;i

                  C 1 Reply Last reply
                  0
                  • C Chris Meech

                    The best that I think you can do is use some program variable that is set. It is difficult to contemplate what an infinite size array might be ;) int* pNumber = new int [SomeProgramVariable]; for (inti=0;i

                    C Offline
                    C Offline
                    Chris Meech
                    wrote on last edited by
                    #11

                    Ouch .. I got bit by those $%#@&*(%$ tags again.

                    1 Reply Last reply
                    0
                    • L Lost User

                      The prolbem is that you are allocating enough memory to hold one int item only!! so your code is accessing invalid memory! the statement : *(pPtr+i) = i; //this is going beyond the memory pointed to by pPtr!! To create a dynamic array, you need to know the number of elements in it and then allocate it: void MakeArray(int nCount) { int * pPtr = new int[nCount]; if (pPtr) { for (int i=0; i < nCount; ++i) { *(pPtr+i) = i; //This is ok pPtr[i] = i; //is same as above! ..... } //Free the memory delete [] pPtr; } } Mh2! :)

                      R Offline
                      R Offline
                      Richard Cheng
                      wrote on last edited by
                      #12

                      :-D :-D :-D :-D :-D :-D You guys are great!!!!!!! :laugh: :laugh: :laugh: :laugh: :laugh: :laugh:

                      1 Reply Last reply
                      0
                      • L Lost User

                        I can display that 0-10 numbers....but then get a memory error message.....what's wrong? int *pPtr; pPtr = new int; CString a; for (int i=0; i<=10; i++) { *(pPtr+i) = i; Display Pointer here....... } delete pPtr;

                        R Offline
                        R Offline
                        Rejeesh
                        wrote on last edited by
                        #13

                        The correct code is as follows. int *pPtr; pPtr = new int[10]; //U were allocating for only a single integer for (int i=0; i<10; i++)//u were doing 11 iterations 0 to 10 { *(pPtr+i) = i; } delete[] pPtr; //u were deleting only a single element :) void (*p[10]) (void (*)());

                        1 Reply Last reply
                        0
                        • L Lost User

                          But I don't want to fix the number of elements......I want to create an infinite "array"......I can insert elements in this list unlimited........ Do you know how to do that????? THANK YOU

                          M Offline
                          M Offline
                          markkuk
                          wrote on last edited by
                          #14

                          Use a MFC CArray or a STL vector.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups