Archived

This topic is now archived and is closed to further replies.

Help allocating memory!!

This topic is 5855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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"

Share on other sites
Hmmm... does that malloc() call really allocate 150 megabyte? Isn''t 1byte*800*150=120KByte?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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"

Share on other sites
This code doesn''t crash on my computer. Probably something else you''re doing wrong.

Share on other sites
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?

Share on other sites
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[i],n+1,archi1);i++;}fclose(archi1);}void ordena(char (*d)[n],int cont){ int k,j,cota; char aux[n]; cota=cont; /*son cont líneas para ordenar*/ k=1; while(k!=0)  {  k=0;  for(j=0;j<cota;j++)    if(strcmp2(d[j],d[j+1])>0)      {       strcpy(aux,d[j]);       strcpy(d[j],d[j+1]);       strcpy(d[j+1],aux);       k=j;     }  cota=k;  } }void crearchi(char (*d)[n],FILE *archi2,int cont){int i;for(i=0;i<cont;i++)fputs(d[i],archi2); }int main(){FILE *archi1,*archi2;char (*d)[n];    /*puntero a arreglo con n columnas*/int cont=0;char c;archi1=fopen("dcreal.txt","r");while(!feof(archi1))  /*cuenta cantidad de líneas del texto*/{if(c=fgetc(archi1)=='\n')cont++;}fclose(archi1);printf("El texto tiene %d líneas", cont);if((d=(char (*)[n])malloc(sizeof(char)*cont*n))==NULL)  /*espacio para puntero que apunta a cont arreglos de 70 columnas*/ printf("No se pudo ubicar la memoria");archi2=fopen("dcreal2.txt","w");  /*nuevo archivo ordenado*/cargarres(archi1,d);  /*cargo los arreglos auxiliares*/ordena(d,cont);  /*ordena los arreglos alfabéticamente por nombre del título*/crearchi(d,archi2,cont);fclose(archi2);  /*escribo cont número de cadenas ya ordenadas en un nuevo archivo*/free(d);}

"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"

[edited by - Stoffel on June 6, 2002 2:41:58 AM]

Share on other sites
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.

Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 37
• 12
• 12
• 11
• Forum Statistics

• Total Topics
631414
• Total Posts
2999952
×