The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9 0ktalmagik Filesystems, Disks and Memory 2 07-21-2008 04:38 AM
how to round up a memory address(memory alignment problem) nj302 High Level Programming 6 09-21-2005 05:57 PM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 05:45 PM
Restricting access to a machine by IP Address patch UNIX for Dummies Questions & Answers 2 10-20-2003 11:46 AM
Need help to access/mount so to access folder/files on a Remote System using Linux OS S.Vishwanath UNIX for Dummies Questions & Answers 2 07-30-2001 05:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-04-2008
Registered User
 

Join Date: Jul 2008
Posts: 13
Cannot access memory at address 0x8

Hi All
I have a structure pointer and setting that pointer as NULL. When i tried to access the elements in the structure i am getting the error message.
"Cannot access memory at address 0x8". This i tried in LINUX.
When the same program is tried thro UNIX (HP-UX), i am not getting the message instead the null value "\0" is printed.

Can any of you tell me why it is so and if possible what is the necessary action that needs to be taken in LInux to avoid that error message.
I have the following program



Code:
#include<stdio.h>
#include<stdlib.h>
struct IMS
{
char *ptr;
int j;
};
typedef struct IMS IMS_FILE;

int main()
{
IMS_FILE *ims_fp;
ims_fp=(struct IMS *)malloc(sizeof(struct IMS));
free(ims_fp);
ims_fp=NULL;
IMS_feof(ims_fp);
}
int IMS_feof( register IMS_FILE *ims_fp)
{
        printf("HAI\n");
}
REgards
RAJ
Reply With Quote
Forum Sponsor
  #2  
Old 07-04-2008
Registered User
 

Join Date: Sep 2006
Posts: 7
I tried your code on my linux box , but i didn't get the error message you said above
Reply With Quote
  #3  
Old 07-04-2008
Registered User
 

Join Date: Jun 2008
Location: Scotland
Posts: 121
mika is right -- the code you posted runs fine. What did you really do?
Quote:
I have a structure pointer and setting that pointer as NULL. When i tried to access the elements in the structure
Are you really trying to dereference a null pointer?
Reply With Quote
  #4  
Old 07-06-2008
Registered User
 

Join Date: Jul 2008
Posts: 13
Hi All
I have missed one line.
printf("%d",ims_fp->j);

I tried to acess one of the elements of the structure and then it gave the problem.

You may replace the function IMS_feof with the below

int IMS_feof( register IMS_FILE *ims_fp)
{
printf("%d",ims_fp->j);
printf("HAI\n");
}

REgards
rajrk
Reply With Quote
  #5  
Old 07-07-2008
Registered User
 

Join Date: Jun 2008
Location: Scotland
Posts: 121
You are dereferencing a null pointer:
Code:
ims_fp=NULL;
...
printf("%d",ims_fp->j);
According to the language definition, the behaviour is undefined if you do this, so that's why you have different behaviour on different platforms.
In order to make your code run correctly, you need to either remove the lines
Code:
free(ims_fp);
ims_fp=NULL;
or remove the function call
Code:
IMS_feof(ims_fp);
Reply With Quote
  #6  
Old 07-07-2008
Registered User
 

Join Date: Jul 2008
Posts: 13
I understood what you said, but it works in HP-UX. As the value in the members are filled with 0's, while in Linux (64 bit)

uname -a
Linux optiplex64 2.6.9-55.0.2.ELsmp #1 SMP Tue Jun 26 14:14:47 EDT 2007 x86_64

it is not working.

One more point is that in another linux box (32 bit )
<lnx22:~>$ uname -a
Linux 2.4.21-37.ELsmp #1 SMP Wed Sep 7 13:28:55 EDT 2005 i686 i686 i386 GNU/Linux

it works.

So ... i am looking for a suitable solution where the same code should work in both in LInux and HP-UX.

Regards
rkraj
Reply With Quote
  #7  
Old 07-07-2008
Registered User
 

Join Date: Jun 2008
Posts: 19
Working/not working depends on implementation of kernel.
Basically how particular kernel deals with memory.
Sometimes it might allow you to read whatever your want, or might not.

For example in case of DOS you call is always valid, becuase iterrupts vector area is located in that area.

At the same time it's big security flaw if any procees is allowed to read any piece of memory. It might be a way to easily obtaint passwords, decription keys, etc. That's one of the reasosn, why you have such exception.

Your program is wrong regardless if it's HP-UX or Linux. It does that you didn't intend to do. You must rewrtie in order to avoid defrefencing of tha null-pointer.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0