memcpy segfaults, but not in windows


 
Thread Tools Search this Thread
Top Forums Programming memcpy segfaults, but not in windows
# 1  
Old 12-19-2005
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  
Old 12-19-2005
Where is buffer defined? What value is in sSize? It's one of those two things. Mostly likely buffer is too small.
# 3  
Old 12-28-2005
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  
Old 01-12-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Undefined reference to memcpy@GLIBC_2.14

Dear All, I am trying to compile OpenFOAM-1.7.x in RHEL. I could not able to compile some of the applications because of libc version issues. It is saying undefined reference to memcpy@GLIBC_2.14 Can anybody look into it? Thanks & Regards, linuxUser_ (3 Replies)
Discussion started by: linuxUser_
3 Replies

2. Programming

memcpy error

I am getting segmentation fault in memcpy.I have given sufficient memory but i dont know why it is occurring char *finalptr = ( char *)malloc(1048576* sizeof(char)); finaloffset=0;have=685516; memcpy(&(finalptr)+finaloffset,out,have); finaloffset=685516;have=359910;... (23 Replies)
Discussion started by: rajsekhar28
23 Replies

3. Programming

Segfaults on pointer deletion

Hey Everyone, I have a check similar to this: if (ptr) { delete ptr; ptr = null; }When I'm debugging in AIX (using dbx), if I attempt to print the value of 'ptr' it says "ptr is not defined" - however, it still enters that if block. So, I'm getting segfaults on the delete... (8 Replies)
Discussion started by: ctote
8 Replies

4. Programming

Segmentation Fault by memcpy

Hello everybody, I'm coding a test program for ARP protocol, and i don't know why i'm getting a SIGSEGV, i traced it with gdb and it says it's due to the memcpy function from /lib/libc.so.6. Program received signal SIGSEGV, Segmentation fault. 0xb7e9e327 in memcpy () from /lib/libc.so.6 This... (5 Replies)
Discussion started by: Zykl0n-B
5 Replies

5. Programming

Problem with memcpy

Hi , I am having records in a file like 00412772784705041008FRUITFUL STRWBRRY 00412772784703041008FRUITFUL STRWBERE 00000570632801448078 X i have declared a structure like typedef struct { char Uname; char Pname; ... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies
Login or Register to Ask a Question