• C

Why there are not error when use Turbor c++ 3.0 to compile and run a c file and error with vc and gcc?

There is a C file as following:
// C file
#include <stdio.h>
int main()
    int i, *p, **pp;
    i = 9;
    p = &i;
    *pp = p;

Using Turbor c++ 3.0 to compile the file, get a output 9.
Using vc under windows xp or gcc under linux, there is a running error.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

       p = &i;
    *pp = p;           <<-- i think this line should be pp = &p;
Kent OlsenData Warehouse Architect / DBACommented:

Luck of the draw, mostly.

The line "*pp = p;" is in error.  The TurboC compiled program is storing the value at a non-critical location so no abort occurs.  The code generated by the vc and gcc compilers result in the data being written to protected memory which is causing the error.

You haven't assigne a value to *pp* yet, so you cannot dereference it.  I suspect that what you want to do is

  pp = &p;

Good Luck,
bourbakiAuthor Commented:
hi Kdo

i don't know what the mean is "non-critical"? Can you explanation?

btw: My programme is written for test so it is exactly
*pp = p;

Kent OlsenData Warehouse Architect / DBACommented:

If you write to a seemingly random address in memory, one of three things will happen.

*) You'll overwrite "critical" memory, in which case your program will hang, segfault, the PC will lock up, etc.

*) You'll overwrite you own data (perhaps the wrong location in an array) which causes your program to give strange answers.

*)  You'll overwrite a cell that isn't being used so nothing bad will happen.  However, changing something else in your program changes the program size so this "random write" can suddenly become critical.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.