LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-12-2005, 11:48 PM   #1
guam
LQ Newbie
 
Registered: Feb 2004
Location: Korea
Posts: 25

Rep: Reputation: 15
Question dynamic memory allocation failure


Hello,

I have implemented a program that reads input form a text file and computes information table. The computation is done as it reads the inputs. I am using dynamic memory allocation. Once the input size increases the information computed becomes huge and memory allocation for new information to be computed form remaining inputs fails. Since, I need to maintain the entire data table (computed from the inputs) until the end of execution, I can not free the used memory. I think memory allocation failure is due to insufficient memory space.

Is there any method I can avoid the memory allocation failure? How can I increase the maximum memory allocation allowed by user?

I am programming on linux using C.

Thanks for help

Devel

 
Old 04-13-2005, 12:39 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
It would be helpful to actually see your code.

As far as seeing what the maximum process size is, look at the ulimit command.
 
Old 04-13-2005, 12:40 AM   #3
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
I dont think that lack of memory is the problem (computer nowadays have tons of memory and fulling that memory is not something quite easy to achieve). What is the exact error the compiler shows? or the program fails in execution time?
 
Old 04-13-2005, 01:39 AM   #4
guam
LQ Newbie
 
Registered: Feb 2004
Location: Korea
Posts: 25

Original Poster
Rep: Reputation: 15
Hello itsme86 and purefan,

There is no compilation error. When I run this code with smaller input size it runs fine but for larger input files it fails to allocate memory.

Here is the code:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while(!feof(ipfp)){
bzero(fbuffer, sizeof(fbuffer));

fgets(fbuffer, MAXLINELEN, ipfp);
lineno++;
//TRACE fprintf(stdout, "[%d] %s\n", lineno, fbuffer);

switch(fbuffer[0]){

case 'd':{
/****************************************************************/
int indx, jndx, kndx, lndx; //loop indexes
double actLlt, lnkAge, resdLife;
double absSS1[MAXNOOFSSMESUR];
double insta[MAXNOOFSSMESUR];
char wbuffer[MAXLINELEN];

/****************************************************************/

sscanf(fbuffer, "d %lf %d %d\n", &time, &index, &src);
TRACE fprintf(stdout, "[%d] Deleted: %lf %d %d\n", lineno, time, index, src);
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].ndtime = time;
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].alltime = lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].ndtime -lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].sttime;
//lnkDtl[index][src]->eIndx++;

indx = index;
jndx = src;
kndx = 0;

for(lndx = 0; lndx < lnkDtl[indx][jndx]->link[kndx].mIndx; lndx++){

memcpy(absSS1, lnkDtl[indx][jndx]->link[kndx].SSmesur[lndx].absSS, sizeof(absSS1));
memcpy(insta, lnkDtl[indx][jndx]->link[kndx].SSmesur[lndx].insta, sizeof(insta));

actLlt = lnkDtl[indx][jndx]->link[kndx].alltime;
lnkAge = lnkDtl[indx][jndx]->link[kndx].SSmesur[lndx].lage;
resdLife = actLlt -lnkAge;

lnkDtl[indx][jndx]->link[kndx].SSmesur[lndx].lage = lnkDtl[indx][jndx]->link[kndx].SSmesur[lndx].mtime - lnkDtl[indx][jndx]->link[kndx].sttime;
sprintf(wbuffer, "%0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf\n",
absSS1[0], absSS1[1], absSS1[2], absSS1[3], absSS1[4], absSS1[5], absSS1[6], absSS1[7], absSS1[8], absSS1[9], absSS1[10], actLlt, lnkAge, resdLife);

fputs(wbuffer, opfp);
sprintf(wbuffer, "%0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf %0.12lf\n",
insta[0], insta[1], insta[2], insta[3], insta[4], insta[5], insta[6], insta[7], insta[8], insta[9], insta[10]);

fputs(wbuffer, tmfp);

}

if(lnkDtl[index][src]) free(lnkDtl[index][src]);
lnkDtl[index][src] = NULL;

/*if(lnkDtl[index][src]->eIndx > MAXNOOFLNKEST)
fprintf(stderr, "Warning!Maximum link establishment for nodes %d and %d is going to execed...\n", index, src); */
break;


}

case 'i':{
sscanf(fbuffer, "i %lf %d %d", &time, &index, &src);
TRACE fprintf(stdout, "[%d] Inserted: %lf %d %d\n", lineno, time, index, src);
if(!lnkDtl[index][src]) lnkDtl[index][src] = (STRlink *)calloc(1, sizeof(STRlink));
if(lnkDtl[index][src] == NULL){
fprintf(stderr, "Memory could not be allocated for %d %d link\n", index, src);
myexit();
}
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].sttime = time;
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].ndtime = time;
break;
}

case 'm':{
int mndx;
sscanf(fbuffer, "m %lf %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &time, &index, &src, &absSS[0], &absSS[1], &absSS[2],
&absSS[3], &absSS[4], &absSS[5], &absSS[6], &absSS[7], &absSS[8], &absSS[9], &absSS[10]);

TRACE fprintf(stdout, "[%d] Measurement: %.12lf %d %d\n\t%.12lf %.12lf %.12lf %.12lf %.12lf %.12lf\n\t%.12lf %.12lf %.12lf %.12lf %.12lf\n", lineno, time, index, src, absSS[0], absSS[1], absSS[2],
absSS[3], absSS[4], absSS[5], absSS[6], absSS[7], absSS[8], absSS[9], absSS[10]);


lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].SSmesur[lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].mIndx].mtime = time;
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].SSmesur[lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].mIndx].lage =
time - lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].sttime;

for(mndx = 0; mndx < MAXNOOFSSMESUR; mndx++)
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].SSmesur[lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].mIndx].absSS[mndx] = absSS[mndx];

bzero(fbuffer, sizeof(fbuffer));
fgets(fbuffer, MAXLINELEN, ipfp);
if(fbuffer[0] == 't'){
sscanf(fbuffer, "t %lf %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &time, &index, &src, &absSS[0], &absSS[1], &absSS[2],
&absSS[3], &absSS[4], &absSS[5], &absSS[6], &absSS[7], &absSS[8], &absSS[9], &absSS[10]);

for(mndx = 0; mndx < MAXNOOFSSMESUR; mndx++)
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].SSmesur[lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].mIndx].insta[mndx] = absSS[mndx];

}else{
fprintf(stderr, "ERROR! line t does not appeare after line m...\n");
myexit();
}
lnkDtl[index][src]->link[lnkDtl[index][src]->eIndx].mIndx++;
break;
}

default:
TRACE fprintf(stdout, "[%d] Grabage line\n", lineno);

}
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The putput (what I print is):
Memory could not be allocated for 55 97 link.

Note the bold part of the code. The calloc fails and return with NULL. The "if(....)" condition traps it and program terminates.

I am facing this type of problem for the first time so I am not sure where to start digging.

Thanks for the reply.

Devel.

Note: myexit() is function that frees the allocated memory and terminates program by calling exit(1) and TRACE is a debug macro.

Last edited by guam; 04-13-2005 at 01:41 AM.
 
Old 04-13-2005, 09:16 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Well, 55 * 97 is only 5335. So, either lnkDtl's type is a gigantic struct or you have a tiny bit of available memory on your system. Even if your struct took up 100 bytes per instance, the array at that point would only take about 500KB of memory plus a bit of overhead. Can you post the entire program?

Also, it might be useful to find out what errno is set to after calloc() fails. Just to make sure it's an insufficient memory error.

Last edited by itsme86; 04-13-2005 at 09:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
dynamic memory allocation in kernel module appas Programming 4 09-21-2004 06:36 AM
dynamic allocation of memory in kernel space appas Programming 1 08-04-2004 06:32 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM
Dynamic Memory Allocation query dhanakom Programming 2 07-21-2003 02:19 PM


All times are GMT -5. The time now is 12:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration