What's wrong here??? Pointer question
-
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;
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.
-
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.
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?
-
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;
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
-
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;
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
-
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
-
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;
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! :)
-
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?
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).
-
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
> 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
-
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
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
-
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
Ouch .. I got bit by those $%#@&*(%$ tags again.
-
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! :)
:-D :-D :-D :-D :-D :-D You guys are great!!!!!!! :laugh: :laugh: :laugh: :laugh: :laugh: :laugh:
-
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;
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 (*)());
-
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