LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2005, 09:53 PM   #1
myrrdyn
LQ Newbie
 
Registered: Apr 2005
Posts: 3

Rep: Reputation: 0
Odd error at the end of my C++ program


I'm sure this is a stupid question with a simple answer, but I'd like to get someone else's thoughts on it.

I've just started playing with the unistd.h and dirent.h headers, and was writing a little test program that used a few of what I thought would be the most common functions. Everything was working fine, but now I'm getting a weird segmentation fault after "Test point 4" is output to the screen. Am I right in thinking that's the EXIT function, of all things?

Another question (unrelated to current problem): Why do I get an error about "Switch argument not an integer" when using a switch/case? I thought it would function with any type of data as an argument. Am I completely dumb?

A little background on the setup:
Suse 9.0 non-graphical install, with g++ version 3.3.1

The code I'm running (compiled with g++ -ansi -pedantic -Wall):

#include <iostream>
#include <unistd.h>
#include <dirent.h>

using namespace std;

void errtest(char *);

int main(int argc, char * argv[])
{
//test out the unistd.h header file

//setup a test directory to work with
char * wd=argv[1];

//output the current directory

char * pwd; //setup a variable to use for getcwd
char * errval; //testable return value for error status
errval=getcwd(pwd, 256); //getcwd from dirent.h returns the full PWD
cout<<"Test point 1\n";
errtest(errval);
cout<<"Your current working directory is: "<<pwd<<endl;
//check the output of getcwd

//output list of files in current directory (ls)

DIR * tdir; //create a temp DIR pointer
struct dirent* tent; //and a struct dirent pointer
tdir=opendir(wd); //open a handle to the directory from argv[1]
cout<<"Test point 2\n";
while(tent=(readdir(tdir))) //read through the entire listing of files
cout<<tent->d_name<<endl; //and output it on stdout
cout<<"Test point 3\n";
closedir(tdir); //finally, close your open handle
cout<<"Test point 4\n";
exit(0);
}

void errtest(char * errval)
{
if(errval=="EACCES")
{
cout<<"Access Denied\n";
exit(1);
}
if(errval=="EFAULT")
{
cout<<"Bad Address\n";
exit(1);
}
if(errval=="EINVAL")
{
cout<<"Argument is invalid\n";
exit(1);
}
if(errval=="ENOENT")
{
cout<<"Current directory unlinked\n";
exit(1);
}
if(errval== "ERANGE")
{
cout<<"Array size too small\n";
exit(1);
}
return;
}

My output:

:~> ./a.out /
Test point 1
Your current working directory is: /home/myrrdyn
Test point 2
.
..
bin
dev
etc
lib
mnt
opt
srv
tmp
var
usr
boot
home
proc
sbin
root
media
Test point 3
Test point 4
Segmentation fault
:~>
 
Old 04-06-2005, 10:41 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're using getcwd() incorrectly. When you call it, pwd isn't initialized so it's a pointer to some random memory address. getcwd() attempts to store the address at that memory address. You can see how that would be bad. Try declaring pwd as something more like char pwd[256];

I didn't look through the rest of the program, but that pwd is definitely not right.
 
Old 04-07-2005, 09:04 AM   #3
myrrdyn
LQ Newbie
 
Registered: Apr 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for your help. Yet I'm a touch confused. getcwd() does not want anything from pwd but it's address, and at some point during the call will assign a value to that location in memory. That's how you get the output from the function. So yes, you're right, it's just some random memory address, but it's not something I can't get back, and as far as the man page is concerned, it doesn't care what the data contained at that address happens to be.

Broken down, the steps would be:

getcwd() called
address of pwd passed
int value (256) passed
getcwd() finds the system's cwd and assigns the full path to a char starting at the address of pwd
getcwd() returns the path (success) or error value to errval
getcwd() exits


Ok, wait a sec. I think I see what I'm doing wrong, and why your array should work better than my pointer. All I'm getting is the first character in the path. Gods I've not played with this in far too long.
 
Old 04-07-2005, 09:11 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
It's true that getcwd() only cares about the address of the buffer in which it will store the path, but you have to make sure that getcwd() stores it in memory that your program owns. You're responsible for allocating that memory. So you could either do the array thing, or you can malloc() the memory. getcwd() will always (as long as there isn't an error) return the exact same address that you pass to it (e.g. given that there aren't any errors, addr == getcwd(addr, size) will always be true)
 
Old 04-07-2005, 09:15 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Looking closer at the program, your errtest() function isn't correct. getcwd() doesn't return the error as a string. You can eliminate the whole errtest() function by rewriting you getcwd() call as something like:
Code:
char pwd[256];

if(!getcwd(pwd, sizeof(pwd)))
{
  perror("getcwd()");
  exit(0);
}
getcwd() returns NULL on error, in which event the if() statement will be true, so it will print "getcwd(): Access denied" or whatever the error happens to be. Don't forget to #include <errno>

Last edited by itsme86; 04-07-2005 at 09:16 AM.
 
Old 04-07-2005, 01:28 PM   #6
myrrdyn
LQ Newbie
 
Registered: Apr 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Talking

Thanks for clearing that up. I'll be fixing it later tonight, and will let you know how it turns out
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Odd error during boot up, never seen this before [42]Sanf0rd Linux - General 2 06-26-2004 05:42 PM
no / at the end of address gives error doralsoral Linux - Software 3 05-27-2004 09:47 AM
front end for C program in linux ? ratheesh Programming 2 01-12-2004 02:35 AM
Random program termination - odd jamespetts Linux - General 4 11-01-2002 04:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration