double pointer usage


 
Thread Tools Search this Thread
Top Forums Programming double pointer usage
# 1  
Old 11-04-2006
double pointer usage

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);
}
axes
# 2  
Old 11-04-2006
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.
# 3  
Old 11-05-2006
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
axes
# 4  
Old 11-05-2006
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.
# 5  
Old 11-05-2006
You mean dynamic allocation??

Quote:
Originally Posted by 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;
}
# 6  
Old 11-05-2006
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
axes
# 7  
Old 11-05-2006
I think i made a mistake

Quote:
Originally Posted by 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
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies

2. Programming

double pointer allocation

hi guys how can I allocate a double char pointer? I want an array of pointers to char objects. the objects the double pointer points to are : char b; (1 Reply)
Discussion started by: vlm
1 Replies

3. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

4. Shell Programming and Scripting

Confused with the usage of one variable usage

Hi All I am not able to understand the usage of d# in the below variable declaration. FILE_LOC contains the directory path And also help me to know about what will be saved in the variable j. Thanks!!! j=${d#${FILE_LOC}/} (2 Replies)
Discussion started by: mohanm
2 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

8. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies
Login or Register to Ask a Question