Solved

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

Posted on 2012-03-26
4
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSH setup on ASA 5505 17 126
NFS v4 7 76
bash: ./Facebooker.pl :Permission Denied 9 58
Cron job never runs on vCenter 6.5 appliance 7 34
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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…
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

759 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