is there any way to allocate more memory

i have simulated a queue with pointer "*" but it dont allocate memory that i want and it overflows in about 10800 members
can i expand this limit ?
Yes. Use the new command to allocate as much memory as you need.



I'm afraid 'new' can not solve this problem.
The problem is all about memory shortage, either phisical or virtual.
I don't think techniques on programming could solve it.
Could you please notify me if you fix it?
I'd like to know how.
Good Luck :)
How do you know that's the problem? It's very easy to overflow the stack, and in such a case the solution is to use new.
I test both "malloc" and "new" in this machine. The result is almost the same. Here's the testing code.
P.S: I use g++ complier and Linux Mint11 OS.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define A_MEGABYTE ( 1024 * 1024 )
#define MAX_ARRAY_NUM ( 1024 * 1024 )

int main()
{
int megabyte = A_MEGABYTE;
char *memArray[MAX_ARRAY_NUM];
for(int i = 0; i < MAX_ARRAY_NUM; i++)
{
memArray[i] = NULL; //initialize the pointer to be NULL
//memArray[i] = new char[megabyte];
memArray[i] = (char*)malloc(megabyte);
if ( memArray[i] != NULL ) //memory allocation success
{
sprintf(memArray[i], "Hello\n");
printf("%4d\t%s",i,memArray[i]);
}
else //memory allocation failed
{
printf("Memory alloctaion failed\n");
return 1;
}
}
return 0;
}

Yes, your code exhausts the heap (runs out of actual physical memory). We still don't know if the OP was overflowing the stack or the heap.
You are asking 1024G memory (1024*1024*1Megabyte) from your PC. Are you serious?


b2ee
A "toy" program to demonstrate that "malloc" and "new" can allocate almost the same size of memory.
In my understanding about the memory using, the allocated memory must be not in continuous space and so it very possibly involved a lot of paging swapping to fetch some certain pointers. May can first try comment below: (it means avoiding using the memory firstly, then make sure the system can afford the assigning, then use it secondly)

if ( memArray[i] != NULL ) //memory allocation success
{
sprintf(memArray[i], "Hello\n");
printf("%4d\t%s",i,memArray[i]);
}
else //memory allocation failed
{
printf("Memory alloctaion failed\n");
return 1;
}
I don't know what you are trying to put into your queue, but normally 10800 *anything* shouldn't make you run out of memory - unless that *anything* are bitmaps or something the like.

You can't change your physical limits, but you could change the way you work with things - if you are actually attempting to to store lots of large objects, you may want think about streaming to or from a file.
new just throws if not enough memory could be allocated, that saves you a lot of checks.
Topic archived. No new replies allowed.