×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Is pointer1=pointer2 legitimate assignment?

Is pointer1=pointer2 legitimate assignment?

Is pointer1=pointer2 legitimate assignment?

(OP)
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("i=%4d Value=%4d\n",i+1,PtrGlob);

  getch();

 }


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

RE: Is pointer1=pointer2 legitimate assignment?

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.

RE: Is pointer1=pointer2 legitimate assignment?

Just a quick look at your 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.

RE: Is pointer1=pointer2 legitimate assignment?

(OP)
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.

RE: Is pointer1=pointer2 legitimate assignment?

Since you are not "creating" 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.

RE: Is pointer1=pointer2 legitimate assignment?

(OP)
Hi James,

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


#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("Failed to allocate %lu bytes\n",totalBytes);
      exit(1);
     }
    else
     {
      //successful allocaton
      printf("allocated %lu bytes\n",totalBytes);
      getch();

      //free the allocated memory
      delete PtrGlob;
      printf("freed %lu bytes\n",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.

RE: Is pointer1=pointer2 legitimate assignment?

(OP)
Muppeteer,

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

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


delete[] PtrGlob;



Thanks,

Alexandre.

RE: Is pointer1=pointer2 legitimate assignment?

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.

RE: Is pointer1=pointer2 legitimate assignment?

(OP)
Thanks for the tip, CINC!

Alexandre.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close