So little memory?

Hello,

Following code caused the error:
This program has performed an illegal operation and will be terminated.......
Is this something to do with memory? But not much....


#include<iostream.h>
#include<string.h>

int main()
{const int l=100; const int l1=1000;
char *s[l]; char s1[l1];
 for (int i=0; i<l1-1; i++)
 {s1[i]='a';}
 s1[l1-1]=NULL;
 for (i=0; i<l; i++)
 {s[i]=new char[l1];
  strcpy(s[i], s1);
 }
 return 0;
}
learnAsked:
Who is Participating?
 
q2guoConnect With a Mentor Commented:
Make sure you clean up after you are done!
Don't forget to free the memory by calling delete

#include<iostream.h>
#include<string.h>

int main()
{
    const int l=100; const int l1=1000;
    char *s[l];
    char s1[l1];
   
    for (int i=0; i<l1-1; i++)
    {s1[i]='a';}
    s1[l1-1]=NULL;

    for (i=0; i<l; i++)
    { s[i]=new char[l1];
      strcpy(s[i], s1);
    }
     
    for (i=0; i<l; i++) delete [] s[i];
    return 0;
}
0
 
q2guoCommented:
Also, I assume that your compiler's memory model must
have been set on Tiny, small or medium.  
The memory models you choose determines the default method
of memory addressing.
For small and medium model, you program is limited to
64k data and stack space .  Because you program is using more than 100k of memory, you are getting the illegal operation error.

To make you program work, you can either decrease the amount of memory it uses (e.g. set l1 to 600), or set your compiler on large model (1MB of dat and stack space).
0
 
learnAuthor Commented:
1. I thought the memory may be back to free after the program finished.
2. Why "delete [] s[i]" instead of "delete s[i]"?
3. When I use "new", I use the free memory? Why you memsioned the stack space?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
q2guoCommented:
1.  May be, may be not..  But trust me, calling delete is always     a good practice

2.  To delete an array in C++, you need to add [] before the         variable name.  Since s[i] is an array of char, [] is needed.

3.  I am just trying to tell you that your program is limit to
    64K of memory space, this includes stack and free store     space.
0
 
learnAuthor Commented:
Hi you let me understand C++ much better...
However, do you mean the max memory can be used in C++ is 1MB? You know a PC can have 8, 16 or 32 memory....Can we use at least half of them?
0
 
q2guoCommented:
Each file (object) has a limit of 1MB.  But, your program
can always have more than one file.  
0
 
nietodCommented:
I'm not sure what q2quo meant by the last comment there.

A DOS based program is limited to less than 640K (not a meg) of memory. A windows based program can have practically unlimited memory.  Limited only by the virtual memory available with is usually many meg.
0
 
learnAuthor Commented:
To Q2gro:

I have just tried the code with "delete" and with setting the compiler model to Compact/Large/Huge. Unfortunately, it still caused the same error message and I have got to turn off and on the PC!

Anything else wrong?
0
 
learnAuthor Commented:
To Nietod:

I am running TC++ at the Dos window in Win95.
Do you think this is limited to 640KB? However, in the menu, I found an item for Large/Huge memory = 1MB.

Thank you for your comment.
0
 
q2guoCommented:
Nietod is right, a dos base program is limited to 640K of memory if it 's not using the Extend Dos (lots of dos games use it).

Did you try decrease l1 to 600?


0
 
learnAuthor Commented:
To Q2guo:

Can you tell me how to using the Extend Dos? I am running TC++ at the Dos window in Win95.
I will test what will happen if let li=600. But, I want 2 or 3 NB :-)

Cheers.
0
 
nietodCommented:
I don't recomend using DOS extenders.  7 years ago they were about the only way to get to extended memory so it was worth putting up with all their problems.  Now with windows 95 and NT you have much safer much more efficient access to extended memory.  But to access the memory you have to create a 32 bit windows or console application.  If you need more than 640 K that is the way to go.
0
 
learnAuthor Commented:
Thank you Experts.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.