• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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;
    printf("%d",**pp);
}
/***************************/

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.
why?

thanks.
0
bourbaki
Asked:
bourbaki
  • 2
1 Solution
 
van_dyCommented:
       p = &i;
    *pp = p;           <<-- i think this line should be pp = &p;
0
 
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,
Kent
0
 
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;

thanks.
0
 
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.


Kent
0

Featured Post

Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now