Hello everybody!
I have two questions about pointers and would be grateful if someone gave me some help.

1. When we use a table's name as a parameter the table is used as an address (the address of its first element). I mean, when i call a function that has a table parameter :

fill (tab, size);

then parameter tab is used as the address of its first element. If i write:

fill (&tab, size);

then &tab is a pointer to tab's first element address? What does this really mean?

2. If i use new in order to create a dynamic array without deallocating the previous chunk of memory then will that lead to a problem? I mean is it right syntactically the following?

for (int i = 0; i < 10; i++) {
   int *tab = new int[size];
    for (int j = 0; j < size; j++)
       tab[j] = rand();
 //delete [] tab;
}

In the example above i fill array tab with random numbers and without deallocating tab i reallocate the specific chunk of memory. This is wrong..isn't it?

Thank you for your time!

Recommended Answers

All 5 Replies

Yes in that for-loop example there would be a memory leakage if you don't deallocate the tab before reallocating memory for it again.

&tab means take the address of tab, in which if tab is an array, then when you pass it into a function, it decays into a pointer, so you will effectively be taking an address to a pointer, in which the pointer points to an array of elements.

Thank you for your reply..that's what i thought from the beginning, that i would definitely have a memory leakage..But my example works perfectly when i try to use my table's data, even if i didn't deallocate it correctly..Why is this happening?

>>When we use a table's name as a parameter the table is used as an address (the address of its first element)

Correct.

>>then &tab is a pointer to tab's first element address?

Correct. &tab is a pointer to the pointer to the first element in the table.

>>What does this really mean?

It means that tab is really just a pointer (if it is not a static array), and & operator takes its address (that is why it is called the address-of operator).

>>If i use new in order to create a dynamic array without deallocating the previous chunk of memory then will that lead to a problem?

Yes. This problem is called a "memory leak". The problem is not so much about allocating new memory before deallocating the previous memory (which is the correct way to do it), the problem is with overwriting the pointer value (tab) with the new address to the new memory. When overwriting, you loose the address of the previously allocated chunk of memory, since you don't have its address anymore, that memory is unreachable and thus, cannot be deallocated. The idiomatic way to reallocate memory is as such:

int* ptr = new int[10]; //original memory
  for(int i = 0; i < 10; ++i) ptr[i] = i;

  //allocating new memory:
  int* temp = new int[20]; //new memory.
  for(int i = 0; i < 10; ++i) temp[i] = ptr[i]; //copy old memory
  delete[] ptr; //delete old memory.
  ptr = temp; //overwrite the pointer.

Or, in C++ (not using C-style arrays), all this process is encapsulated in containers like std::vector or std::list, and you don't have to worry about memory leaks or memory transfers.

>> I mean is it right syntactically the following?

Being right syntactically doesn't guarantee much. When you think about it, all software bugs were syntactically correct when they were compiled!

>>works perfectly.. Why is this happening?

Memory leaks don't corrupt your execution or your memory. But, if you have a memory leak in the middle of a loop, you are going to increase memory consumption at every iteration, and eventually run out of memory or significantly slow down the entire system. It is exactly because memory leaks aren't "that dangerous" that they are "dangerous", the worst bugs are the bugs that can go unnoticed for a long time (imagine you have a function that leaks, and you have been using that function for years in various projects without problems, but all of a sudden you use that function in a long-running loop for the first time, get a huge memory consumption problem, but because you trust that good-old function that you have been using so much, it will be the last place you will think to look, and you might spend a long time finding the problem).


Make your life simpler and learn to work with std::vector and other STL containers.

Thank you for your reply..that's what i thought from the beginning, that i would definitely have a memory leakage..But my example works perfectly when i try to use my table's data, even if i didn't deallocate it correctly..Why is this happening?

Just because you have a memory leak doesn't mean your program will not work,it just mean that you are leaking memory.

Thank you all for your answers! You were more than helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.