ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have two char array: one of them is char directory_array[] = "../some/directory/" and the second one is files name of the directory content : char file_name_array;
the file_name_array changed repeatedly and it must be concatenate to directory_array[];
what is the best way to define the file_name_array and concatenate to directory_array in c++ ?
my code is :
Code:
void session_Separator(char pcap_dir[])
{
DIR *dir;
struct dirent *ent;
char extention[] = ".pcap";
std::ofstream myfile;
myfile.open("./results");
if ((dir = opendir (pcap_dir)) != NULL)
{
/* Read the directory file names */
while ((ent = readdir (dir)) != NULL)
{
count = 1;
// Exclude none pcap files
if(!memcmp(&ent->d_name[strlen(ent->d_name) - 5], extention, 5))
{
printf("%s\n", ent->d_name);
int len = strlen(pcap_dir);
char *temp = (char *)malloc(256);
memcpy(temp, pcap_dir, len);
if(temp != NULL){
for (int i = 0; ent->d_name[i]; ++i)
temp[i + len] = ent->d_name[i];
// Open pcap files to read the packets
pcap_handle = pcap_open_offline(temp, NULL);
if ( pcap_handle == NULL)
{
fprintf(stderr,"\nUnable to find input file.\n");
return;
}
pcap_loop(pcap_handle, 0, packet_handler, NULL);
if(count == 8){
//printf("%s\n", ent->d_name);
myfile << ent->d_name << "\n";
}
}
}
}
myfile.close();
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
}
}
the above code has segmentation fault
the segmentation fault is not in pcap_loop callback handler.
without pcap_open_offline it works well and maybe my first code.
I think the segmentation fault is related to pcap_open_offline. directory content is included more than 10000 files. and every time i open a new pcap_open_offline. maybe the segmentation error is because of that .!!!!!!!!!!
I found the problem.
Because every time I open a handler for pcap file by pcap_open_offline maybe I program didn't can handle these too much open handler. I take the pcap_close(handler) after pcap_loop and it did work.
without pcap_open_offline it works well and maybe my first code.
I think the segmentation fault is related to pcap_open_offline. directory content is included more than 10000 files. and every time i open a new pcap_open_offline. maybe the segmentation error is because of that .!!!!!!!!!!
you should think about giving the memory back that you occupy temporarily, otherwise you produce memory leaks.
as a rule of dumb and for the begin think about that each malloc requires a free
I found the problem.
Because every time I open a handler for pcap file by pcap_open_offline maybe I program didn't can handle these too much open handler. I take the pcap_close(handler) after pcap_loop and it did work.
Doesn't make a lot of sense to me. Perhaps if you would post the code clip where you found the problem, as a way to assist others in diagnosing similar issues.
Please mark the thread as solved if you feel you have solved it, and I continue to urge you to try to describe the problem/solution very clearly so that the thread can be helpful to others in the future.
Regarding large malloc() operations, or rather any allocation operation, I recommend checking your resultant pointer to verify that you did receive the memory you requested. After all, there are no guarantees and if the chunk of memory really was extremely large, then maybe the answer was "no" from the system, you got, and ignored an ERRNO, the return from the malloc() was -1, and thus when you attempted to access that allocated memory to copy or concatenate into it, that caused the segmentation fault.
Also, if this is your program, then compile with debug symbols, and specifically -ggdb as a potential flag. Turn on core files in your terminal or environment. When a segmentation violation occurs in the future, you should get a core file dump. And then you can analyze that using GDB. I have a blog (link in my signature) discussing how to debug C programs using GDB.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.