[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

So little memory?

Posted on 1998-02-08
13
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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