LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   dynamic memory allocation failure (https://www.linuxquestions.org/questions/programming-9/dynamic-memory-allocation-failure-312631/)

guam 04-12-2005 11:48 PM

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

:scratch:

itsme86 04-13-2005 12:39 AM

It would be helpful to actually see your code.

As far as seeing what the maximum process size is, look at the ulimit command.

purefan 04-13-2005 12:40 AM

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?

guam 04-13-2005 01:39 AM

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.

itsme86 04-13-2005 09:16 AM

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.


All times are GMT -5. The time now is 02:33 PM.