# double pointer usage

**URL:** <https://community.unix.com/t/double-pointer-usage/166583>\
**Category:** Programming\
**Created:** [November 4, 2006, 8:50am UTC](https://community.unix.com/t/double-pointer-usage/166583 "2006-11-04T08:50:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [November 4, 2006, 8:50am UTC](https://community.unix.com/t/double-pointer-usage/166583/1 "2006-11-04T08:50:37Z")

</div>

Hi,

Is there anything wrong in the following code : - Because I am freeing the local variable tt, will it affect 'str' variable in the main function. will there be any memory leaks for bulk requests?

Thanks in advance

main(){  
char \*str="hi";  
fun(&str);  
printf("str=%s\n",str);  
}  
fun(char \*\*str){  
char \*tt;  
tt=(char \*)malloc(sizeof("hello"));  
strcpy(tt,"hello");  
tt=(char \*)realloc(tt,sizeof("hello world"));  
strcpy(tt,"hello world");  
\*str=tt;  
free(tt);  
}

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [November 4, 2006, 10:32pm UTC](https://community.unix.com/t/double-pointer-usage/166583/2 "2006-11-04T22:32:48Z")

</div>

You are not allowed to continue to use malloc'ed space after you free'd it. Lots of people do that, but it is not legal. You program might happen to work with some malloc libraries. But probably not after both a free() followed by another malloc() as implied by your "bulk" remark. But on the bright side, no, this will not result in a memory leak. I have seen this stuff done a lot. Usually someone allocates a bunch of stuff and the beginning of a function, immediately frees it all, and continues to use it during the function. The idea is that the space is then almost like it resides in the stack. After a return, another function can use malloc and thus reuse the space that was freed. You are upping the ante by trying to use the freed space after a return. So my advice is don't do stuff like this.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [November 5, 2006, 2:00am UTC](https://community.unix.com/t/double-pointer-usage/166583/3 "2006-11-05T02:00:11Z")

</div>

Thanx Perderabo

I have a requirement like this:  
I need to prepare a string dynamically whose size is not known ahead of time. How can I program this?

```
     Example:- 

             main\(\) \{
                 /* DO SOME THING */
                 prepare\_string\(string\_to\_be_prepared\);
                 \ *******
                 \ *******
             \}

             prepare\_string\(string\_to\_be_prepared\) \{

              /* prepare the dynamic size string */
              return;
             \}

```

Any pointers pls

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [November 5, 2006, 2:28am UTC](https://community.unix.com/t/double-pointer-usage/166583/4 "2006-11-05T02:28:05Z")

</div>

You were on the right track. Just don't invoke free(). At least not until you are finished with the string. If you have more than a predictably few such strings, you must free each string when you are done or you will have that memory leak you were worried about.

---

<div class="post-metadata">

**Author:** ![sajinkoroth](https://community.unix.com/user_avatar/community.unix.com/sajinkoroth/32/3500_2.png) [@sajinkoroth](https://community.unix.com/u/sajinkoroth)\
**Post date:** [November 5, 2006, 3:14am UTC](https://community.unix.com/t/double-pointer-usage/166583/5 "2006-11-05T03:14:19Z")

</div>

> [@axes](#):
>
> Thanx Perderabo
> 
> I have a requirement like this:  
> I need to prepare a string dynamically whose size is not known ahead of time. How can I program this?
> 
> Example:-
> 
> main() {  
> /\* DO SOME THING _/  
> prepare\_string(string\_to\_be\_prepared);  
> \*_\*\*\*\*\*  
> \*\*\*\*\*\*\*  
> }
> 
> prepare\_string(string\_to\_be\_prepared) {
> 
> /\* prepare the dynamic size string \*/  
> return;  
> }
> 
> Any pointers pls

```
                      I think you need to create a string at run time .And if that is the case you could use following code 

```

int prep\_string(char \*);

int main()  
{  
char \*str; // The dynamicallly allocated string u could use it like any other string  
// Like str[30] ,str [i]is valid since it is charachter string

if(prep\_string(str) == -1)  
//code to handle not enough memory condition  
// Now use the str as any other charachter string

return 0;  
}

int prep\_string(char \*str)  
{  
int size ;  
printf("\nEnter the size of the string ");  
scanf("%d",&size);  
str =(char \*) malloc(size \* sizeof(char));  
if(str == NULL)  
// Not enough memory to satisfy the request  
return -1;  
else  
return 0;  
}

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [November 5, 2006, 6:06am UTC](https://community.unix.com/t/double-pointer-usage/166583/6 "2006-11-05T06:06:59Z")

</div>

Sajin,

```
   I think using single pointer will not cater my requirement, we need to use double pointer,

```

because str parameter in prep\_string function is different from  
str pointer variable in main function

CORRECT me if i am wrong

---

<div class="post-metadata">

**Author:** ![sajinkoroth](https://community.unix.com/user_avatar/community.unix.com/sajinkoroth/32/3500_2.png) [@sajinkoroth](https://community.unix.com/u/sajinkoroth)\
**Post date:** [November 5, 2006, 6:18am UTC](https://community.unix.com/t/double-pointer-usage/166583/7 "2006-11-05T06:18:11Z")

</div>

> [@axes](#):
>
> Sajin,
> 
> I think using single pointer will not cater my requirement, we need to use double pointer,
> 
> because str parameter in prep\_string function is different from  
> str pointer variable in main function
> 
> CORRECT me if i am wrong

I think the following serves ur purpose if any thing again is wrong with that pls correct me , actually i just typed that code in a hurry that's the reason..  
char \* prep\_string();

int main()  
{  
char \*str; // The dynamicallly allocated string u could use it like any other string  
// Like str[30] ,str [i]is valid since it is charachter string

if( (str =prep\_string() ) == NULL)  
//code to handle not enough memory condition  
else  
// Now use the str as any other charachter string

return 0;  
}

char \* prep\_string()  
{  
int size ;  
printf("\nEnter the size of the string ");  
scanf("%d",&size);  
str =(char \*) malloc(size \* sizeof(char));  
if(str == NULL)  
// Not enough memory to satisfy the request  
//Place the code to handle the situation  
return NULL;  
else  
return str; // Now both pointers point to the same location  
}
