9 Replies - 439 Views - Last Post: 23 April 2014 - 09:28 PM Rate Topic: -----

#1 newbcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-February 14

[pointers] const pointer pass by reference in print function

Posted 21 April 2014 - 10:42 PM

Hi All,

I am trying use a print function to print out data in a struct. My questions are:

1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?

2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr. Please let me know.

Thanks for helping!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;

struct Inventory
{
	string productCode;
	string productName;
	float productPrice;
};

void dmallocation (Inventory*& ptr, int size)
{
    ptr = new Inventory[size];
    for (int i = 0; i < size; i++)
    {
        ptr[i].productCode = "string1";
        ptr[i].productName = "string 2";
        ptr[i].productPrice = 5.655;
    }
}

void deleptr (Inventory*& ptr)
{
    delete [] ptr;
    ptr = 0;
}

void print (Inventory*& ptr, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << ptr[i].productCode << ptr[i].productName << ptr[i].productPrice << endl;
    }
}

int main ()
{
    int size2 = 50;

    Inventory* pmyInventory;
    dmallocation(pmyInventory, size2);
    print(pmyInventory, size2);
    deleptr(pmyInventory);
}



Is This A Good Question/Topic? 0
  • +

Replies To: [pointers] const pointer pass by reference in print function

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 05:58 AM

Look at the const keyword to have the compiler help you enforce not changing values. Since you are using both a pointer and a reference, the declaration will look uglier to make sure that not only the reference is not changed, but the pointer as well.

As for the deleteptr(), if you allocate an array using new, you should deallocate it using delete [].
Was This Post Helpful? 0
  • +
  • -

#3 newbcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-February 14

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 11:20 AM

View PostSkydiver, on 22 April 2014 - 05:58 AM, said:

Look at the const keyword to have the compiler help you enforce not changing values. Since you are using both a pointer and a reference, the declaration will look uglier to make sure that not only the reference is not changed, but the pointer as well.

As for the deleteptr(), if you allocate an array using new, you should deallocate it using delete [].

Can you give some examples as how to do that? I can't find a way to do it, unless I do const Inventory* pmyInventory but that's not what I want. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 11:41 AM

Have you considered using std::vector to hold all the instances of your Inventory? A std::vector will handle all the messiness of memory allocation/deallocation for you. If you must stick with your own allocation/deallocation you should consider a wrapper class that will encapsulate all the memory allocation/deallocations.

Also the why do you need to pass the pointer to the print function by reference? Just passing the pointer to this function should be adequate. Something like:

void print (const Inventory* ptr, int size)



Jim
Was This Post Helpful? 0
  • +
  • -

#5 newbcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-February 14

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 02:59 PM

View Postjimblumberg, on 22 April 2014 - 11:41 AM, said:

Have you considered using std::vector to hold all the instances of your Inventory? A std::vector will handle all the messiness of memory allocation/deallocation for you. If you must stick with your own allocation/deallocation you should consider a wrapper class that will encapsulate all the memory allocation/deallocations.

Also the why do you need to pass the pointer to the print function by reference? Just passing the pointer to this function should be adequate. Something like:

void print (const Inventory* ptr, int size)



Jim

It's a class assignment. I have to use array and pass by value is not allowed. and use const when it's appropriate. I can't think of a way to do it. These requirements in a print function just don't make any sense.....
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 03:05 PM

Quote

I have to use array and pass by value is not allowed.

Arrays aren't passed by value, they're passed by pointer.

Quote

I can't think of a way to do it.

You can't think of a way to do what? You make the variable constant by using the const keyword.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 newbcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-February 14

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 03:14 PM

passed by pointer means making a copy of the pointer, right? how is that different from pass by value? in the sense that both are making a copy of the data.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: [pointers] const pointer pass by reference in print function

Posted 22 April 2014 - 09:13 PM

Yes, but when you pass the pointer, you are making a copy of the pointer. You can clobber the pointer the value all you want but it won't affect the caller the same way you can clobber the value passed in when you do a pass by value. When you pass a pointer, you can dereference the pointer, and then start clobbering data that will affect the caller.

Consider this naive implementation of strcpy():
void MyStrCpy(char * dst, const char * src)
{
    while (*src)
        *dst++ = *src++;
    *dst = 0;
}



When called with code like this:
void TestIt()
{
    char * buffer = new char[256];
    char * testData = "This is a test.";
    
    MyStrCpy(buffer, testData);

    cout << buffer << endl;
    cout << testData << endl;
}



Notice that the pointers buffer and testData did not change after calling MyStrCpy(), but the memory location that buffer pointed to has been changed.
Was This Post Helpful? 2
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: [pointers] const pointer pass by reference in print function

Posted 23 April 2014 - 06:29 AM

Before C++ came with it's reference semantics passing variables by pointers was often referred to as passing by reference, or pointer reference. So you may want to check with your instructor for his/her interpretation.

Next it is possible to have multiple const qualifiers in a declaration, although IMO this gets confusing quickly.

void print (const Inventory&(const *ptr), int size)

Note I didn't try the above so it may or may not do exactly what you want, I don't have access to a compiler at present.

But like I said in an earlier post I myself would wrap this nasty manual memory management in a class or a structure. This class would have at a minimum a pointer variable of the proper size for the array, a variable to keep track of the array size, a size() member function, a default constructor to construct an empty instance, a constructor with one size_t parameter to construct an instance with that size, the destructor that properly deletes the memory allocated in your constuctors, and either disable or create the copy constructor and assignment operators. By encapsulating the array in a class it becomes much easier to pass this array around to your functions using references and proper const qualifiers.

The other option in my opinion would be to use either the std::vector class or the std::array class instead.


Jim
Was This Post Helpful? 2
  • +
  • -

#10 newbcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-February 14

Re: [pointers] const pointer pass by reference in print function

Posted 23 April 2014 - 09:28 PM

Thanks guys! I will revisit this post in the near future to grasp the idea of classes, encapsulation and such.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1