Solved

So little memory?

Posted on 1998-02-08
13
167 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

11 Experts available now in Live!

Get 1:1 Help Now