Need help fixing a problem with the behavior of a C program on a Linux Mint VM

Hi, I was hoping someone can help me out. I'm running Linux Mint on a Windows 7 VM and I need my C programs to access memory locations at their will but this program gives me 3.14 twice which the second output should be different.

I mean it's great and all that they can't access each other memory location, but that's what I need them to do in my VM. And, it worked fine for my professor in class. Is it something with my VMWare configuration?:

#include <stdlib.h>
#include <stdio.h>

int	main ()
{
  long int a[2];
  double d = 3.14;
  printf("BEFORE: d = %.15g\n", d);
  a[2] = 1073741824; /* Out of bounds reference */
  printf("AFTER: d = %.15g\n", d);
  return(EXIT_SUCCESS);
}

Open in new window

EindoofusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

mccarlIT Business Systems Analyst / Software DeveloperCommented:
No, there is nothing with your VM configuration that might affect this (other than it is probably due to maybe a 64-bit system vs a 32-bit system)

But the main difference between you running it and your professor running, would be how the compiler has laid out the memory usage for those variables. The code you have, while it may highlight the issue of out of bounds references, does not necessarily have to work as you expect. DIfferent compilers may do totally different things.

To make it do what you want, the first thing I would try is to set more 'invalid' locations with different values, ie. try something like the following...

#include <stdlib.h>
#include <stdio.h>

int	main ()
{
  long int a[2];
  double d = 3.14;
  printf("BEFORE: d = %.15g\n", d);
  a[2] = 1073741824; /* Out of bounds reference */
  a[3] = 1073741824; /* Out of bounds reference */
  a[4] = 1073741824; /* Out of bounds reference */
  a[5] = 1073741824; /* Out of bounds reference */
  a[6] = 1073741824; /* Out of bounds reference */
  a[7] = 1073741824; /* Out of bounds reference */
  a[8] = 1073741824; /* Out of bounds reference */
  a[9] = 1073741824; /* Out of bounds reference */
  printf("AFTER: d = %.15g\n", d);
  return(EXIT_SUCCESS);
}

Open in new window


You could have a play and see which one of the above actually makes the change to 'd'
0

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
Subrat (C++ windows/Linux)Software EngineerCommented:
Hi,

It is nothing in your VMWare configuration.
Here this should fall in "RUNTiME ERROR". Thats what you should expect. You are not allocated space but tring to use that.i.e a[2].

One more thing, it might be in your mind that when you are writing to a[], though you are crossing the array bound, it should write in other memory location(adjacent). But here there is no guarentee that d and a[] will be in adjacent location. or simply d may not be after a[].

Any way you should get runtime exception, if you are not getting it, that is due to your compiler or work environment. If you are interested in experimenting this, then one way I can suggest that try to check address of d and a. Then you can proceed smoothly.

If you need further help, u r welcome!
0
EindoofusAuthor Commented:
Thanks, with that code on Linux Mint 8 32bit VM I got:

BEFORE: d = 3.14
AFTER: d = 3.14
Segmentation fault

And with CentOS 6.2 64bit VM I got:

BEFORE: d = 3.14
AFTER: d = 5.30498947741318e-315
Segmentation fault

Why didn't it give me a segmentation fault the first time? And will I have more accurate results if I run a 64bit VM?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
> Why didn't it give me a segmentation fault the first time?

The seg fault would most likely be because it is now writing well past where it should and so other factors are coming in.

> And will I have more accurate results if I run a 64bit VM?

What do you mean by "accurate"? And why are worrying about this so much. This isn't the right way of doing anything. It is just a piece of code given to you to attempt to show you what might happen if you have bugs in your code.
0
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
VMware

From novice to tech pro — start learning today.