Hello.
I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a function. The function fills the structures with information on subdirectories in a given directory.
In the function itself, I can step through the array of structures and print to the console the list of directory names. However, when I try to do so from the calling program I get a Segmentation Fault. Snippets from my code are posted below.
Can you tell me what I'm doing wrong? Thanks! -
#include <dirent.h>
-
-
int main(void)
-
{
-
struct dirent **sEvents; // structure containing event directory names
-
int iResult; // return value
-
int iDirMax; // number of event directories
-
int iDirCtr; // directory counter
-
int GetDirectoryList(); // function to return list of directories
-
-
// get directory list
-
iResult = GetDirectoryList(&sEvents, &iDirMax);
-
-
fprintf(stdout, "Number of Directories = %d\n", iDirMax);
-
-
for (iDirCtr = 0; iDirCtr < iDirMax; iDirCtr++)
-
{
-
// THIS GIVES Segmentation Fault..................................
-
fprintf(stdout, "Event Directory: %s\n", sEvents[iDirCtr]->d_name);
-
}
-
-
return 0;
-
}
-
-
int GetDirectoryList(struct dirent **sEvents, int *iMax)
-
{
-
char cSOD[512]; // path to parent directory
-
int iCtr; // counter
-
-
// get the list of event directories
-
*iMax = scandir (cSOD, &sEvents, DirFilter, alphasort);
-
if (*iMax >= 0)
-
{
-
for (iCtr = 0; iCtr < *iMax; iCtr++)
-
{
-
// THIS PRINTS OK...........................................
-
fprintf(stdout, "EventDirectory %s\n", sEvents[iCtr]->d_name);
-
}
-
}
-
-
return 0;
-
}
-
-
13 3245
struct dirent **sEvents; // structure containing event directory names
is not a structure. And it's not an array either. There is no array in your code.
sEvents is a pointer to a struct dirent pointer. You do not have a struct direct pointer so you can't initializae this sEvents variable.
I think you just need an array: -
strict dirent myarray[100];
-
-
//and then call your function:
-
-
GetDirectoryList(myarray, 100);
-
-
//where:
-
-
int GetDirectoryList(struct dirent *sEvents, int iMax)
-
{
-
//in here sEvents[1] is the same as myarray[1]
-
}
-
is not a structure. And it's not an array either. There is no array in your code.
sEvents is a pointer to a struct dirent pointer. You do not have a struct direct pointer so you can't initializae this sEvents variable.
Thanks for the quick reply. I'm sorry to be dense, but now I'm even more confused. I'll try to explain...
The scandir() function in my GetDirectoryLis t() function requires a pointer to a dirent pointer (that is, **dirent). scandir() uses malloc to allocate memory space for the set of dirent structures, fills those structures, and returns the number of structures. Therefore, as I understand it, after scandir() executes the **dirent pointer is a pointer to the starting place in memory for a set of pointers to the actual structures. Since I know how many structures there are, I can step through the directory names inside the GetDirectoryLis t() function using the syntax sEvents[n]->d_name. Any problems with my understanding/explanation so far?
Now the issue: in main() I declared the **dirent pointer, which after GetDirectoryLis t() executes I *think* should contain the pointer to the starting place in memory for the pointers to the dirent structures. But when I attempt in main() to step through the structures and extract the directory names using the same syntax as before (or any syntax I can think of), I get the Segmentation Fault.
So... where in the above is my understanding of the process breaking down?
Thanks again for all your help.
My problem now is that I don't know what GetDirectoryLIs t() you are using. All of the sites for dirent.h do not contain this function and there of the may. many GetDirectoryLis t() functions I find, all of them require a patch as an argument and return a pointer to that folder.
There's nothing about filling an array.
Where are you getting this function from and where is it documented???
GetDirectoryLis t() is the function I wrote -- see the original code snippet. In it I call the scandir() function, which is a Linux/Unix command.
I seem to be in the weeds and have misunderstood you. I apologize.
This code:
for (iDirCtr = 0; iDirCtr < iDirMax; iDirCtr++)
{
// THIS GIVES Segmentation Fault.......... ............... .........
fprintf(stdout, "Event Directory: %s\n", sEvents[iDirCtr]->d_name);
}
This loop depends on iDirMax being correct from GetDirectoryLis t(). However, if GetDirectoryLis t() returns a 0 indicating an error, *iMax is not set to 0 so in the loop above you have garbage in iDirMax.
Change GetDirectoryLis t to return a void to force all checking to go through iMax (iDirMax in main()).
Thanks so much for all your help! I finally figured out my problem:
struct dirent **sEvents was declared in main(); this is a pointer to a pointer to a structure.
I wanted the function GetDirectoryLis t() to fill the set of structures, so I had to pass the address to the pointer (&sEvents) to GetDirectoryLis t(), and declare it in GetDirectoryLis t() as struct dirent ***sEvents -- that is, a pointer to my original pointer.
Because I didn't have that 3rd level of dereferencing, I was never actually changing the contents of my original pointer.
Thanks again!
Wait a minute. Your GetDirectoryLis t() is: -
int GetDirectoryList(struct dirent **sEvents, int *iMax)
-
etc...\
-
And your call is: -
iResult = GetDirectoryList(&sEvents, &iDirMax);
-
So if sEvents is a struct dirent **sEvents, then &sEvents is a struct dirent ***sEvents, which isn't what your function has as an argument. How are ytou getting this to compile?
That's exactly my point. The gcc compiler didn't catch it, and it took me a week (and your and one other person's help) to figure it out.
I finally did it by filling my code with fprintf statements showing the memory addresses of various variables.
JosAH 11,448
Recognized Expert MVP
That's exactly my point. The gcc compiler didn't catch it, and it took me a week (and your and one other person's help) to figure it out.
Did you use the -Wall flag for gcc? I'll warn about everything then.
I finally did it by filling my code with fprintf statements showing the memory addresses of various variables.
printf is the poor men's debugger and it is our friend ;-)
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray;
DoStuff(myArray);
|
by: justanotherguy63 |
last post by:
Hi,
I am designing an application where to preserve the hierachy and for
code substitability, I need to pass an array of derived class object in
place of an array of base class object. Since I am using vector
class(STL), the compiler does not allow me to do this.
I do realize there is a pitfall in this approach(size of arrays not
matching etc), but I wonder how to get around this problem. I have a
class hierachy with abstract base...
|
by: kalinga1234 |
last post by:
there is a problem regarding passing array of characters to another
function(without using structures,pointer etc,).can anybody help me to
solve the problem.
|
by: beetle |
last post by:
Hello,
I'm storing data in several different binary tree's.
The root node is located in a struct containing general data about the
tree.
struct lnode {
char *fname;
int nentry;
|
by: dogalacar |
last post by:
Hi All,
I am trying to pass array of structures from a C dll to C# as msdn
sample does(outarrayofstructs sample) but PtrToStructure function gives
error :
--> "structure must not be a value class"
What i am doing wrong ?
here is the code
| |
by: Steve Turner |
last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows:
typedef struct tag_scanparm
{
short cmd;
short fdc;
WORD dsf;
short boxcar;
short average;
short chan_ena;
|
by: Subodh |
last post by:
Hi All,
I want to use a C++ API in a static lib that returns a linked List in
C#
I am planning to P/Invoke to perform the Interop, I would like to know
which way will be better to interop a Linked list to C# in terms of
performance, I have came across following methods
I am creating a flat DLL to export the static lib functionality.
|
by: jason |
last post by:
Hello,
I'm a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.
If I pass an array of structures to a function, then suddenly I can't use
sizeof(array) / sizeof(array) anymore within that function ?
Help - What point am I missing ?
|
by: pereges |
last post by:
which one do you think is better ? I need to make my program efficient
and in some places I have passed the copy of a variable which makes
life some what easy while writing huge expressions but they do requrie
much more memory than a simple pointer.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |