Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
04-17-2004, 08:51 PM
#1
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Rep:
how does linux handle memory allocation?
im programming a uri parser which has a lot of pointers and double pointers involved (arrgh)
and I am checking for memory leaks by running the function that does all the allocating then running the function that does all the freeing in an infinite loop and observing the memory usage in the Info Center.
When I run the program the memory stays the same for about 10 secs then starts going down rapidly. First of all this seems odd. Why doesnt the memory start leaking immediately. I also can't *find* any memory leeks, although its nearly 3am too ;0
anyone who knows a little about linux process memory organisation?
04-17-2004, 09:01 PM
#2
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
There's a tool called dmalloc that might help. It hijacks malloc(), free(), and friends and can keep track of which lines of your code have allocated memory which was never freed.
04-17-2004, 09:02 PM
#3
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Original Poster
Rep:
im using electricfence to check for violations. Ill check that dmalloc, thanks
04-17-2004, 09:25 PM
#4
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Original Poster
Rep:
well for anyone felling up to some debugging, or if you just want to read my code, here it is
Code:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct _authority
{
}
authority;
typedef struct _net_path
{
authority * auth;
char ** abspathsegs;
}
net_path;
typedef struct _hier_part
{
char ** abspathsegs;
char * query;
}
hier_part;
typedef struct _parseduri
{
char * scheme;
char * opaque_part;
hier_part * hierpart;
}
parseduri;
bool is_reserved(char c)
{
if (c==';' || c== '/' || c=='?' || c==':' || c=='@' ||
c=='&' || c=='=' || c== '+' || c=='$' || c==',') return true; else return false;
}
bool is_mark(char c)
{
if (c=='-' || c== '_' || c=='.' || c=='!' || c=='~' || c=='*' || c=='\'' ||
c== '(' || c==')' ) return true; else return false;
}
bool is_upalpha(char c)
{
if (c>=65 && c<=90) return true; else return false;
}
bool is_lowalpha(char c)
{
if (c>=97 && c<=122) return true; else return false;
}
bool is_digit(char c)
{
if (c>=48 && c<=57) return true; else return false;
}
bool is_alpha(char c)
{
if (is_upalpha(c) || is_lowalpha(c)) return true; else return false;
}
bool is_alphanum(char c)
{
if (is_alpha(c) || is_digit(c)) return true; else return false;
}
bool is_unreserved(char c)
{
if (is_alphanum(c) || is_mark(c)) return true; else return false;
}
bool is_hex(char c)
{
if (is_digit(c)
||c=='A'||c=='B'||c=='C'||c=='D'||c=='E'||c=='F'
||c=='a'||c=='b'||c=='c'||c=='d'||c=='e'||c=='f'
) return true; else return false;
}
bool is_escaped(char * c)
{
if (*c=='%') if (is_hex(*(c+1))) if (is_hex(*(c+2))) return true;
return false;
}
void freeptrs(void ** ptrs)
{
while (*ptrs)
{
free(*ptrs);
*ptrs=NULL;
ptrs++;
}
}
void freehier_part(hier_part * hier_part)
{
if (hier_part->abspathsegs)
{
freeptrs((void**)hier_part->abspathsegs);
printf(" freed *abspathsegs\n");
free(hier_part->abspathsegs);
printf(" freed abspathsegs\n");
hier_part->abspathsegs=NULL;
}
if (hier_part->query)
{
free(hier_part->query);
printf(" freed query\n");
hier_part->query=NULL;
}
}
void freeparseduri(parseduri * parseduri)
{
if (parseduri->scheme)
{
free(parseduri->scheme);
printf("freed scheme\n");
parseduri->scheme=NULL;
}
if (parseduri->opaque_part)
{
free(parseduri->opaque_part);
printf("freed opaque_part\n");
parseduri->opaque_part=NULL;
}
if (parseduri->hierpart)
{
freehier_part(parseduri->hierpart);
printf("freed hier_part\n");
parseduri->hierpart=NULL;
}
}
void nullparseduri(parseduri * parseduri)
{
parseduri->scheme=NULL;
parseduri->opaque_part=NULL;
parseduri->hierpart=NULL;
}
void nullhier_part(hier_part * hier_part)
{
hier_part->abspathsegs=NULL;
hier_part->query=NULL;
}
void setscheme(char * firstchar,char * lastchar,char * &destloc)
{
/* if this function is successful the scheme name will be stored at destloc (NULL-terminated),otherwise
destloc will me made to NULL point. */
char * mrkr1=firstchar;
bool ok;
/* its not ok if the string is of length 0 */
if (firstchar>lastchar) ok=false; else ok=true;
while (mrkr1<=lastchar && ok)
{
if (mrkr1==firstchar) { if (!is_alpha(*mrkr1)) ok=false; }
else { if (!(is_alphanum(*mrkr1) || *mrkr1=='+' || *mrkr1=='-' || *mrkr1=='.')) ok=false; }
mrkr1++;
}
if (ok)
{
destloc=(char*)malloc((lastchar-firstchar)+2);
strncpy(destloc,firstchar,(lastchar-firstchar)+1);
*(destloc+((lastchar-firstchar)+1))=NULL;
/* convert any uppercase chars to lowercase */
mrkr1=destloc;
while (*mrkr1!=NULL)
{
if (*mrkr1>=65 && *mrkr1<=90) *mrkr1+=32;
mrkr1++;
}
}
else
{
destloc=NULL;
}
}
void setabspath(char * firstchar,char * lastchar,char ** &destlocs)
{
unsigned long numsegs=1;
unsigned long x;
char * mrkr1=firstchar;
char * mrkr2=mrkr1;
while (*mrkr2!=NULL && *mrkr2!='?')
{
if (*mrkr2=='/' && *(mrkr2+1)!=NULL && *(mrkr2+1)!='?') numsegs++;
mrkr2++;
}
/* allocate space for the pointers, including 1 ((NULL-pointing) terminating pointer) */
destlocs=(char**)malloc((sizeof(char*)*(numsegs+1)));
/* since we know the # of pointers in advance, we will set the NULL terminator right now */
*(destlocs+numsegs)=NULL;
/* allocate the actual segments */
mrkr2=mrkr1;
x=0;
while (*mrkr1!=NULL && *mrkr1!='?' && (x<numsegs))
{
if (*mrkr2=='/' || *mrkr2==NULL || *mrkr2=='?')
{
if (x>0) mrkr1++;
*(destlocs+x)=(char*)malloc((mrkr2-mrkr1)+1);
strncpy(*(destlocs+x),mrkr1,(mrkr2-mrkr1));
*((*(destlocs+x))+(mrkr2-mrkr1))=NULL;
x++;
mrkr1=mrkr2;
}
mrkr2++;
}
}
bool parseuri(char * uri,parseduri * parseduri)
{
char * mrkr1,* mrkr2,* mrkr3;
/* set all pointer fields in the parseduri struct to NULL. This is to facilitate
the freeparseduri() function so it won't attempt to free un malloc()`d memory */
nullparseduri(parseduri);
mrkr1=uri;
mrkr2=uri;
while (*mrkr2!=':' && *mrkr2!=NULL) mrkr2++;
if (*mrkr2==NULL) return false;
mrkr3=mrkr2+1;
if (*mrkr3==NULL) return false;
setscheme(mrkr1,mrkr2-1,parseduri->scheme);
if (!parseduri->scheme) return false;
/* scheme has been set; mrkr3 points to first char after ':', already established not to be NULL */
if (*mrkr3!='/')
{
/* rest of uri is opaque. check that its valid */
while (*mrkr3!=NULL && (is_reserved(*mrkr3) || is_unreserved(*mrkr3) || is_escaped(mrkr3))) mrkr3++;
if (*mrkr3!=NULL)
{
/* opaque part contained illegal characters */
freeparseduri(parseduri);
return false;
}
else
{
/* opaque part is fine; allocate and copy */
mrkr2++;
parseduri->opaque_part=(char*)malloc((mrkr3-mrkr2)+1);
strcpy(parseduri->opaque_part,mrkr2);
return true;
}
}
else
{
/* rest of uri is hierarchial */
mrkr3++;
if (*mrkr3==NULL || *mrkr3=='?')
{
/* theres nothing left in the path, it is therefore invalid */
freeparseduri(parseduri);
return false;
}
parseduri->hierpart=(hier_part*)malloc(sizeof(hier_part));
nullhier_part(parseduri->hierpart);
if (*mrkr3=='/')
{
/* net path */
mrkr3++;
if (*mrkr3==NULL || *mrkr3=='?')
{
/* theres nothing left in the path, it is therefore invalid */
freeparseduri(parseduri);
return false;
}
/* TODO */
}
else
{
/* abs path */
/* first make sure the abs_path is valid. (double-slashes are not welcomed) */
mrkr1=mrkr3;
while (*mrkr3!=NULL && *mrkr3!='?' && (is_unreserved(*mrkr3) || is_escaped(mrkr3) ||
*mrkr3==':' || *mrkr3=='@' || *mrkr3=='&' || *mrkr3=='=' || *mrkr3=='+' || *mrkr3=='$'
|| *mrkr3==',' || (*mrkr3=='/' && *(mrkr3-1)!='/'))) mrkr3++;
if (*mrkr3==NULL||*mrkr3=='?')
{
/* it is valid */
setabspath(mrkr1,mrkr3-1,parseduri->hierpart->abspathsegs);
}
else
{
/* its invalid */
freeparseduri(parseduri);
return false;
}
}
}
return true; /* TEMP */
}
int main()
{
parseduri parseduri;
char uri[1000];
printf("Enter a URI->");
scanf("%s",uri);
if (parseuri(uri,&parseduri))
{
printf("URI was parsed.\n");
if (parseduri.scheme){ printf("scheme:"); puts(parseduri.scheme); }
if (parseduri.opaque_part){ printf("opaque_part:"); puts(parseduri.opaque_part); }
if (parseduri.hierpart)
{
if (parseduri.hierpart->abspathsegs)
{
int i=0;
while (*((parseduri.hierpart->abspathsegs)+i)!=NULL)
{
printf("path segment %d:",i+1);
puts(*((parseduri.hierpart->abspathsegs)+i));
i++;
}
}
}
}
else
{
printf("URI was not parsed.\n");
}
freeparseduri(&parseduri);
return (0);
}
04-17-2004, 10:10 PM
#5
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Original Poster
Rep:
no takers i see. well Im going to bed.
hopefully my brain will figure out the problem while im asleep
Nighty nite!
All times are GMT -5. The time now is 01:21 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News