?
Solved

So little memory?

Posted on 1998-02-08
13
Medium Priority
?
175 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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