Solved

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

Posted on 2012-03-26
4
422 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 8

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now