Solved

So little memory?

Posted on 1998-02-08
13
168 Views
Last Modified: 2010-04-10
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;
}
0
Comment
Question by:learn
  • 6
  • 5
  • 2
13 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1181240
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
 
LVL 3

Expert Comment

by:q2guo
ID: 1181241
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
 

Author Comment

by:learn
ID: 1181242
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
 
LVL 3

Expert Comment

by:q2guo
ID: 1181243
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
 

Author Comment

by:learn
ID: 1181244
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
 
LVL 3

Expert Comment

by:q2guo
ID: 1181245
Each file (object) has a limit of 1MB.  But, your program
can always have more than one file.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1181246
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
 

Author Comment

by:learn
ID: 1181247
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
 

Author Comment

by:learn
ID: 1181248
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
 
LVL 3

Expert Comment

by:q2guo
ID: 1181249
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
 

Author Comment

by:learn
ID: 1181250
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
 
LVL 22

Expert Comment

by:nietod
ID: 1181251
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
 

Author Comment

by:learn
ID: 1181252
Thank you Experts.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now