Help allocating memory!!

Started by
41 comments, last by Tai-Pan 21 years, 10 months ago
Please ima newbie and Im having this problem. I need to allocate this: cont=800; d=(char(*)[150])malloc(cont*sizeof(char)*150); that would be 800*150=150 Megabytes!! when I run the program it crashes cos I don''t have enough memory..Im using an old compiler that I have here at school..its Borland C++ 3.0 Is there any way to get all that memory??..Can I use windows virtual memory?? Any help will be welcome. Thanks "Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
Advertisement
Hmmm... does that malloc() call really allocate 150 megabyte? Isn''t 1byte*800*150=120KByte?
quote:Original post by Tai-Pan
Please ima newbie and Im having this problem.
I need to allocate this:

cont=800;
d=(char(*)[150])malloc(cont*sizeof(char)*150);

that would be 800*150=150 Megabytes!!

when I run the program it crashes cos I don''t have enough memory..Im using an old compiler that I have here at school..its Borland C++ 3.0

Is there any way to get all that memory??..Can I use windows virtual memory??

Any help will be welcome.

Thanks

"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"


Should this be:
int cont = 800;
char * d = (char *)malloc(cont * sizeof(char) * 150);

It allocates 1 * 800 * 500 = 400000 bytes/120 KB.
As the previous poster stated, that line allocated 120K which is no big deal at all, BUT you are using the wrong alloc (it is not a BUG, but it is less obvious than my suggestion).

You should use calloc, it is MADE to allocate memory for arrays of things, without as much chance for error ... a malloc prototype looks like:

void* malloc(int numBytes); // memory allocate

and is called like:

int numInts = 20;
int **myIntArray = (int**) malloc(numInts * sizeof(int));

calloc looks like:

void* calloc(int numElements, int elementSize); // continuous allocate

and is called like:

int numInts = 20;
int **myIntArray = (int**) calloc(numInts, sizeof(int));


now the above is just to help you use C/C++ better, but will not fix your current problem at all ...

I''m not sure your program crashes due to insufficient memory ... it might be crashing when you free() or delete the memory. Are you calling free?

Also, are you checking the malloc pointer for NULL, and if so ... IS IT NULL? You say it crashes, but do you mean it returns NULL? and perhaps your code tries to use NULL. If it is returning NULL, then you are right, it cannot allocate that much memory - but that is HIGHLY unlikely with 120K ... with 120MB that would be possible.

More info please.
Thank you Xai, you are right, i got confused, its 120K..dont know why I wrote 150MB..
and yes..I did this:

....
char (*d)[150];
...
cont=800;
if((d=(char(*)[150])malloc(cont*sizeof(char)*150))==NULL)
printf("Couldn´t allocate enough memory");
....

free(d);

I think that calloc and malloc both handles the memory the same way..I could be wrong though.

I dont know which is the problem..all the code is pretty simple and stupid..and it compiles great..but when executed the program just crashes.

I need this space cos I Need a pointer that points to 800 arrays of 150 characters each..
Cant detect the problem here..



"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
This code doesn''t crash on my computer. Probably something else you''re doing wrong.
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by Tai-Pan
I need this space cos I Need a pointer that points to 800 arrays of 150 characters each..

800 arrays of 150 characters each is not the same as one array of 800*150 characters. How are you using this data?
---visit #directxdev on afternet <- not just for directx, despite the name
Ok..this is the full code since its very very short:
The objective of the program is to take a txt file with 800 lines each one consisting of a video game name, then it must generate another file but with all the names sorted alphabetically...
I create the 800 arrays to hold the 800 titles(strings) and so I can sort them with a simple bubble method.
Please take a look and ignore the strange variable names and the comments since its in spanish..

Thanks:


  #include <stdio.h>#include <conio.h>#include <alloc.h>#include <string.h>#define n 120int strcmp2(char *a,char *d)   /*compara cadena d con a*/{for(;*a==*d;a++,d++)   if(*a=='\0')       return(0);  /*son iguales*/return(*a-*d); /*apenas sean distintos devuelve la resta de los caracteres*/ }void cargarres(FILE *archi1, char (*d)[n]){char c;int i=0;archi1=fopen("dcreal.txt","r");while(!feof(archi1)){  fgets(d,n+1,archi1);<br>i++;<br>}<br>fclose(archi1);<br>}<br><br><br>void ordena(char (*d)[n],int cont)<br>{<br> int k,j,cota;<br> char aux[n];<br> cota=cont; /*son cont líneas para ordenar*/<br> k=1;<br> while(k!=0)<br>  {<br>  k=0;<br>  for(j=0;j&lt;cota;j++)<br>    if(strcmp2(d[j],d[j+1])&gt;0) <br>     {<br>       strcpy(aux,d[j]);<br>       strcpy(d[j],d[j+1]);<br>       strcpy(d[j+1],aux);<br>       k=j;<br>     }<br>  cota=k;<br>  }<br> }<br><br><br>void crearchi(char (*d)[n],FILE *archi2,int cont)<br>{<br>int i;<br><br>for(i=0;i&lt;cont;i++)<br>fputs(d,archi2); <br><br>}<br><br><br>int main()<br>{<br><br>FILE *archi1,*archi2;<br>char (*d)[n];    /*puntero a arreglo con n columnas*/<br>int cont=0;<br>char c;<br><br><br><br><br>archi1=fopen("dcreal.txt","r");<br><br><br>while(!feof(archi1))  /*cuenta cantidad de líneas del texto*/<br>{<br>if(c=fgetc(archi1)=='\n')<br>cont++;<br>}<br>fclose(archi1);<br><br>printf("El texto tiene %d líneas", cont);<br><br><br><br>if((d=(char (*)[n])malloc(sizeof(char)*cont*n))==NULL)  /*espacio para puntero que apunta a cont arreglos de 70 columnas*/ <br>printf("No se pudo ubicar la memoria");<br><br><br>archi2=fopen("dcreal2.txt","w");  /*nuevo archivo ordenado*/<br><br>cargarres(archi1,d);  /*cargo los arreglos auxiliares*/<br><br>ordena(d,cont);  /*ordena los arreglos alfabéticamente por nombre del título*/<br><br>crearchi(d,archi2,cont);<br><br><br>fclose(archi2);  /*escribo cont número de cadenas ya ordenadas en un nuevo archivo*/</font><br>free(d);<br>}  </pre></DIV><!–ENDSCRIPT–><br><br>"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"<br><br><SPAN CLASS=editedby>[edited by - Stoffel on June 6, 2002 2:41:58 AM]</SPAN>    
"Those who follow the path of the warrior must be ready to die, to stand for their convictions, live for one´s convictions, die for one´s convictions"
I always thought the c in calloc() stood for "clear" as it also sets the memory to 0. And since the memory it allocates is perfectly fine for anything (not just arrays), it stuck in me head.

Go fig.
There is a problem with the while loop that counts number of lines in a file:

  int cont = 0;...while(!feof(archi1)){  if(c = fgetc(archi1) == ''\n'')    cont++;}  

It actually sets cont to the number of lines minus one.
At least when I changed
d = (char (*)[n])malloc(sizeof(char) * cont * n)  

to
d = (char (*)[n])malloc(sizeof(char) * (cont + 1) * n)  

programs doesn''t crashed when it called free(d) like it did before.

This topic is closed to new replies.

Advertisement