Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is pointer1=pointer2 legitimate assignment?

Status
Not open for further replies.

SashaBuilder3

Programmer
Jan 13, 2002
129
0
0
CA
Hi everybody!


Here is a simple sample program, it works OK, but I am not sure if such pointer assignment could be used freely (both in DOS and Windows). Can anyone clarify this issue?

#include <stdio.h>
#include <conio.h>

//Global pointer
int *PtrGlob;


void ArraySetting(int n)
{
//local pointer
int *ptrLoc;

//memory allocation using the local pointer
ptrLoc=new int[n];

//the pointer assignment
PtrGlob=ptrLoc;

}


void main()
{
int i,n=1000;

ArraySetting(n);

//sample usage of the global array
for(i=0;i<n;i++)
PtrGlob=i+10;

for(i=0;i<n;i++)
printf(&quot;i=%4d Value=%4d\n&quot;,i+1,PtrGlob);

getch();

}


And who can expalain why in posted text in some lines the square brackets disappear?

 
And who can expalain why in posted text in some lines the square brackets disappear?

The TGML (Tecumseh Group Group Markup Language) is interepreting your code as part of the markup language. To not have your code interpreted put it in a code marks. To start your code, type a square bracket ([), the word code, then a closing square bracket (]). At the end of you code, put a square braket ([), a backslash (\), the word code, then a closing square bracket (]).

Alternately, you can disable the TGML by unchecking the Process TGML when you post a question or answer. This is located in the Step 2 Options next to the Emoticons/Smileys.

For more infomation, click the link on the Process TGML. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Just a quick look at your code:
Code:
//memory allocation using the local pointer
ptrLoc=new int[n];
For every new there should be also be a delete. For a small program it may not matter but when you start doing larger programs, this is a common cause of memory leaks. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Thanks James,

Well, in that sample program I cannot use the operator delete in the ArraySetting() function because it'll spoil the whole idea. But what about to apply this operator to the global pointer PtrGlob in the main function?

Alexandre.
 
Since you are not &quot;creating&quot; the pointer with a new method, delete won't work. Some compilers won't even allow you to use delete if new isn't used to create the variable. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Hi James,

Well, I re-wrote the program putting the delete operator in it. Here you are:

Code:
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

char *PtrGlob;

int ArraySetting(unsigned long howManyBytes)
 {
  //the function returns 1 if memory allocation is successful and 0 otherwise
 
  char *ptrLoc;

  if(!(ptrLoc=new char[howManyBytes]))
   return 0;
  else
   {
    //successful allocation 
    PtrGlob=ptrLoc;
    return 1;
   }
 }


void main()
 {
  int i,n=5;
  unsigned long mult=64535,totalBytes;

  for(i=0;i<n;i++)
   {
    //how many bytes to allocate
    totalBytes=(i+1)*mult;

    //attempt to allocate an array
    if(!ArraySetting(totalBytes))
     {
      //if falls through - end the program
      printf(&quot;Failed to allocate %lu bytes\n&quot;,totalBytes);
      exit(1);
     }
    else
     { 
      //successful allocaton
      printf(&quot;allocated %lu bytes\n&quot;,totalBytes);
      getch();

      //free the allocated memory 
      delete PtrGlob;
      printf(&quot;freed %lu bytes\n&quot;,totalBytes);
      getch();
     }
   }//for(i=0;i<n;i++)

 }//main()

As you see I apply new to the local pointer in the ArraySetting() function and use delete to a different one -- global PtrGlob. It works so far but still I'm not sure if this is correct.

What do you think?

Thanks,

Alexandre.
 
This seems right to me X-) . The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Muppeteer,

Just maybe a little addition (from your response about delete operator.).

Shouldn't it be like this in the main function:

Code:
delete[] PtrGlob;


Thanks,

Alexandre.
 
Haha, whoepsie :) The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
In the case that you are confused about pointers, just draw yourself a picture.

For example, if you allocate a chunk of memory on the heap, such as an array referenced by a pointer, and then you assign it to another pointer variable, all you really have, you know, is two pointers referencing the same thing. So all you do is draw a box (lots of adjacent ones if it's an array) that resembles the allocated memory, and two little arrows pointing to it. In your case, one that says Local and another that says Global, perhaps.

Speaking of which, you should always look carefully for an alternative to using global variables, for it is good programming practice. They can cause many problems.

Lastly, If you dynamically allocate memory, you know that you must ALWAYS free it up immediately when the program doesn't need it anymore. However you do not want to orphan any arrays or leave any dangling pointers, so make sure you keep careful track of what you have sitting in memory, exactly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top