C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2009, 01:50 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 1
Problem with memory clean up in calling function

I am working on following code snippet and am having problems stated below. Please help me:

Code:
typedef struct {
char* hostname;
int connects;
} Str;

char * getandprint(int *argcnt, Str * strarr)
{
        static char buff[8];
        int i,k;
        Str *strarr1=0;
        strcpy(buff,"PRINTER"); //this will be returned
        *argcnt=getcount();
        printf("count is %d\n",*argcnt);
        for (i=0;i<(*argcnt);i++)
        {
                strarr1=(Str *)malloc(sizeof(Str));
                if(strarr1 !=(Str *)0){
                        memset(strarr1,0,sizeof(Str));
                        seqarr1=GetStr();
                        if(seqarr1 !=(Str *)0) {
                                strarr[i]=*strarr1;
                                free(strarr1);
                                printf("outside %d string is %s\n",seqarr[i].connects,seqarr[i].hostname);                        }
                }
        }
        for (i=0;i<(*argcnt);i++)
        {
                printf("printed %d string is %s\n",seqarr[i].connects,seqarr[i].hostname);        }
        return buff;
}

Str * GetStr()
{
//This dynamically allocates the memory and then populates Str element and returns pointer to it
}
I am able to print everything correctly in the printf of first for loop.(one in Green)

However, the printf in second for loop(one in red) gives following problems if *argcnt is large:
1) Some of the initial members print "seqarr[i].connects" correctly but junk values in "seqarr[i].hostname".
2) The program gives a segmentation fault if the count is very large.

For example, if count is 3000, for first 45-50 times, seqarr[i].connects is printed correctly but "seqarr[i].hostname" prints junk.
And after printing some first 2950 times, I get a segmentation fault.

I suspect these could be due to clean up issues.

Please let me know why am I getting these errors and how do I correct them?

Last edited by karthur3; 03-22-2009 at 01:57 PM. Reason: missed out on free()
karthur3 is offline   Reply With Quote
Reply

Tags
dynamic memory, pointer, string, structure

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting number Leslie C Programming 8 05-20-2009 04:23 AM
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
problem getting result when calling a function drb2k2 Windows Programming 1 04-14-2003 09:51 AM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM
Interface Question smog890 C Programming 11 06-03-2002 05:06 PM


All times are GMT -6. The time now is 07:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22