Memory Allocation for References

Hi,
I have a question regarding the references in C++.
Is it possible for dynamically allocate memory for reference variables(&var_name) using malloc or new?Also please provide me better way of deallocating the memory.


Thanks,
Prasanth
References have no memory allocated if i understand correctly. They are an abstraction that is transformed at compile time.

Or do you want a reference to something dynamically allocated? In that case, you have to use pointers
Hi,
Please find the below code:

int simuReadFile(const char * const i_FileName,
char* &m_Address,
BIT32 &m_SizeRead,
BIT32 i_Size,
BIT32 i_Offset)
{
BIT32 l_file_len = simuFileSize(i_FileName) ;
FILE *l_file_ptr = Fopen(i_FileName, "rb");


// if input size is 0, use filelength as read size
if (i_Size == 0)
{
i_Size = l_file_len ;

} // if input size is 0, use filelength as read size

// verify size
if (i_Size > (l_file_len - i_Offset ))
{
err_exit("%s, line %d, file %s too small\n"
"size = %d, request - offset = (%d - %d) = %d\n",
__FILE__, __LINE__, i_FileName,
l_file_len, i_Size, i_Offset, i_Size - i_Offset) ;

} // verify size

// allocate space if address is NULL
if (m_Address == NULL)
{
m_Address = (char *) malloc(i_Size);
cout<<"The dynamically allocated address is "<<hex<<&m_Address<<endl;
}

if (1 != fread(m_Address, i_Size, 1, l_file_ptr))
{
syserr_exit("%s, line %d, fread(%s) fail\n",
__FILE__, __LINE__, i_FileName) ;
}

m_SizeRead = i_Size ;
}

In this code char* &m_Address is a reference to a char pointer right?
And this is allocated memory dynamically as below:
// allocate space if address is NULL
if (m_Address == NULL)
{
m_Address = (char *) malloc(i_Size);
cout<<"The dynamically allocated address is "<<hex<<&m_Address<<endl;
}

Is this the right way of allocating memory?

A reference variable always referes to an object. A pointer can refer to an object or no object, in which case it takes the null value.

If you want to allocate memory, you must refer to that memory using a pointer and you must release it using a pointer. During the object's lifetime, you may refer to it using a reference or pointer.

Your function simuReadFile passes m_SizeRead and m_Address as reference variables. But they are really synonyms for some real BYTE and char* variables resepectively. So the lines you wrote are syntactically correct:
1
2
    m_SizeRead = i_Size;
    m_Address = (char*)malloc(i_Size);


I can't comment on whether your code is good style as I don't know the context.
Hi.
let me define what is reference avriable first

1. Reference variable
C++ intriduces a new kind of variable known as the Reference variable. A reference variable provides an alias for a previously defined variable.

For Example
1
2
3
  
float total=100;
float &sum=total;


here sum is a reference variable to total.

here both the variables refer to the the same object .

2. malloc() and free()

malloc() -> allocates memory dynamically.
free() -> dealocates that dynamic memory.

we will use the above when we are not knowing in advance how much memory we are going to use.

3. new and delete unray operators .

new -> it allocates the memory in easier way.
delete-> it will free the memory in easier way.

for example

Creating with new operator
1
2
p= new int;
q= new float


Deleting with delete operator

1
2
3
4
   
delete p;
delete q; 
 


The better way to dealocte the memory is always delete and the new operator will always have additional advantages over malloc() function.

Go through the book : Object oriented programming with C++ written by E Balaguruswamy.

http://www.wiziq.com/tutorial/762-C-Basic-Introduction.
Regards
Kolla. Sanjeeva Rao.
Helo ksrao,
refer to the the same object

What this for? Such a Duo reference.
Topic archived. No new replies allowed.