LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-30-2016, 02:36 AM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
Question c++ char array concatenation


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.

Last edited by kikilinux; 03-30-2016 at 02:39 AM.
 
Old 03-30-2016, 02:42 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Why aren't you using C++ strings?
 
Old 03-30-2016, 03:10 AM   #3
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
I used this code for concatenation :
Code:
				char *temp = (char *)malloc(strlen(pcap_dir) + strlen(ent->d_name) + 1);
				strcpy(temp, pcap_dir);
				strcat(temp, ent->d_name);
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 .!!!!!!!!!!
 
Old 03-30-2016, 03:25 AM   #4
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-30-2016, 05:32 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by kikilinux View Post
I used this code for concatenation :
Code:
				char *temp = (char *)malloc(strlen(pcap_dir) + strlen(ent->d_name) + 1);
				strcpy(temp, pcap_dir);
				strcat(temp, ent->d_name);
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
 
1 members found this post helpful.
Old 03-30-2016, 07:32 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
If you use "character arrays" in C++, then the "++" can't really help you much.

Therefore: use the built-in string type, which neatly takes care of all these things for you. (And, that's what it's for!)
 
1 members found this post helpful.
Old 03-30-2016, 08:21 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by kikilinux View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
c++, concatenate



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
Returning char pointer from function which has array of char pointer srinietrx Programming 3 10-30-2015 03:55 AM
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-26-2010 11:27 PM
How to convert short array to char array? bvkim Programming 4 06-08-2010 09:26 AM
C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" heathf Programming 2 04-25-2009 09:20 PM

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

All times are GMT -5. The time now is 08:33 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