Solved

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

Posted on 2012-03-26
4
430 Views
Last Modified: 2012-03-28
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

0
Comment
Question by:Eindoofus
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 300 total points
ID: 37769683
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
 
LVL 9

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 200 total points
ID: 37769790
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
 

Author Comment

by:Eindoofus
ID: 37769793
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
 
LVL 35

Expert Comment

by:mccarl
ID: 37769817
> 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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
Teach the user how to edit .vmx files to add advanced configuration options Open vSphere Web Client: Edit Settings for a VM: Choose VM Options -> Advanced: Add Configuration Parameters:
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…

820 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