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
ftp to windows avivcohen HP-UX 4 05-09-2009 01:46 AM
Changing windows server alias name on windows or unix? NycUnxer UNIX for Dummies Questions & Answers 1 03-26-2008 05:54 PM
scp from windows BG_JrAdmin SUN Solaris 1 05-17-2006 01:21 AM
X windows dino_leix Linux 4 06-11-2005 08:51 AM
About rcp at Windows NT OS. livic Security 1 07-30-2001 05:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-19-2005
khoma khoma is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 1
memcpy segfaults, but not in windows

Hi

Having a lil trouble with a rather simple application I'm writing. It so happens that I have to copy some data using memcpy() and so far I've been doing just fine compiling it with VC.Net and running it on Windows XP. Now I'm trying to port the thing to Solaris (which shouldn't really be too big a deal) and for some reason it is causing problems.

Code:
int SendPacket(unsigned short packNo, const char* data, unsigned short sSize){
	int addrlen = sizeof(struct sockaddr);
	int iRet = 0;
	PutShortAt(buffer, OP_DATA);	// Insert DATA op code
	PutShortAt(buffer + 2, packNo);	// Insert packet number
	memcpy(buffer + 4, data, sSize); // Insert the actual data
	iRet = sendto(iSock, buffer, sSize + 4, 0, (struct sockaddr *)&sraddr,addrlen);	// Send packet

	printf("Sent %d bytes\n", iRet);
	return IOK;
}
The line which causes the segfault is the memcpy() line. Basically, what I'm trying to do is this;

const char* data contains 512 bytes of arbitrary data that is to be added to a 516 byte packet which will then be transmitted to a computer somewhere on the internet. Byte 4-516 will contain the data. The first 4 bytes in the packet are reserved for packet information in the form of two shorts. sSize is of course the data size.

Any ideas as to what may be causing this? Like I said earlier, it runs just fine on Windows (of course with the appropriate modifications to net code, winsock and such). Any help would be greatly appreciated.
  #2 (permalink)  
Old 12-19-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
Where is buffer defined? What value is in sSize? It's one of those two things. Mostly likely buffer is too small.
  #3 (permalink)  
Old 12-28-2005
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,960
And to check the obvious, you are including <string.h>, yes? On some systems/configurations, omitting a header can cause things like memcpy to segfault from calling memcpy with the wrong function definition etc.
  #4 (permalink)  
Old 01-12-2006
cjediknight cjediknight is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 5
Yes I think your problem is the buffer variable, where dir you define it and how did you define it,

if you just defined it in a global variable like this,

char * buffer, then definetely you have a problem or they are right the buffer would be too small..

It run in windows because I experience some window based C/C++ compiler allow you to access memory that was not allocated or that is not belongs to you.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0