memcpy error


 
Thread Tools Search this Thread
Top Forums Programming memcpy error
# 1  
Old 10-14-2011
memcpy error

I am getting segmentation fault in memcpy.I have given sufficient memory but i dont know why it is occurring

Code:
char *finalptr = ( char *)malloc(1048576* sizeof(char));
finaloffset=0;have=685516;
memcpy(&(finalptr)+finaloffset,out,have);
finaloffset=685516;have=359910;
memcpy(&(finalptr)+finaloffset,out,have);

The first time it copies successfully but second time it gives segmentation fault

Code:
signal SEGV (no mapping at the fault address) in memcpy%sun4v-hwcap3 at 0xffffffff7f01b874
0xffffffff7f01b874: memcpy%sun4v-hwcap3+0x0604: stx      %o4, [%o0 - 8]t.

By calculation I am not crossing boundary but it is giving error
OS:Solaris

Last edited by pludi; 10-14-2011 at 08:02 PM..
# 2  
Old 10-14-2011
You tried to copy the data to the address of the pointer (&) which is not correct.

The code should be:

Code:
memcpy(finalptr + finaloffset, out, have);

This User Gave Thanks to MacMonster For This Post:
# 3  
Old 10-14-2011
Quote:
Originally Posted by rajsekhar28
char *finalptr = ( char *)malloc(1048576* sizeof(char));
Unrelated to the segfault, there are a couple of things about this statement of which you should be aware.

In general, if you're going to multiply a number by the size of a data type, you should use calloc(). If not, you should check for overflow before passing the result to malloc(). I say in general because in this specific instance, where the number is hardcoded and the product is almost certainly less than the max value of size_t, it's extremely unlikely that it will cause a problem.

Unless you know why you're doing it, it's best not to cast unnecessarily. Many C programmers do it unthinkningly, but it is a practice with zero benefit and a potentially insidious drawback: supression of compiler warnings and errors.

If the malloc implementation does not return void * (or in this instance * char), the explicit cast tells the compiler that its okay if malloc returns an int (which is not unheard of) or some other type, and, should such a mismatch occur, that you want it to silently coerce the return value regardless of its type. Such coercion can lead to unexpected behavior.

Regards,
Alister
# 4  
Old 10-14-2011
@MacMonster Thanks because of it i was getting segmentation fault although i dont know why my format is wrong because i wasn't getting any compilation error.
@alister although I haven't written here I am checking for correct memory allocation.So that isn't the culprit.

Regards,
Rajsekhar
# 5  
Old 10-14-2011
Quote:
Originally Posted by rajsekhar28
@MacMonster Thanks because of it i was getting segmentation fault although i dont know why my format is wrong because i wasn't getting any compilation error.
You wouldn't get a compilation error - the function is expecting a pointer, which is what you were supplying. Just not the right pointer Smilie.
# 6  
Old 10-14-2011
The compiler will happily chop off its own foot if you ask it to, as long as you do so with correct grammar. It doesn't understand the intent of your program.
# 7  
Old 10-14-2011
Quote:
Originally Posted by alister
Unrelated to the segfault, there are a couple of things about this statement of which you should be aware.

In general, if you're going to multiply a number by the size of a data type, you should use calloc(). If not, you should check for overflow before passing the result to malloc(). I say in general because in this specific instance, where the number is hardcoded and the product is almost certainly less than the max value of size_t, it's extremely unlikely that it will cause a problem.
Why calloc? It zeros the memory and, as such, will touch every page allocated, forcing the kernel to actually create a physical page for it. This is wasteful, IMO, especially if you'll be writing to that memory again immediately (as he is) or, worse, if you'll be writing to the memory sparsely. Plus, I always thought that "calloc(x, y)" was pretty much equivalent to "malloc(x*y)" minus the obvious zeroing of the returned memory.
Quote:
Originally Posted by alister
Unless you know why you're doing it, it's best not to cast unnecessarily. Many C programmers do it unthinkningly, but it is a practice with zero benefit and a potentially insidious drawback: supression of compiler warnings and errors.

If the malloc implementation does not return void * (or in this instance * char), the explicit cast tells the compiler that its okay if malloc returns an int (which is not unheard of) or some other type, and, should such a mismatch occur, that you want it to silently coerce the return value regardless of its type. Such coercion can lead to unexpected behavior.

Regards,
Alister
I've never seen a malloc implementation return int, but since the compiler assumes any functions that it hasn't seen a definition for take integer parms and return int, casting is a good way to step on your foot if you forget to include the header for malloc, lol. I've seen this before, and it's not pretty when someone spends forever trying to debug a core dump due to the compilers' (mis)understanding that malloc returns int and sign extends the result badly into the pointer causing its value to be incorrect and, thusly, causing a crash. All warnings were suppressed because of the cast. (BTW, it wasn't me, lol. I was the one who noticed stdlib wasn't included, and upon me were bestowed many thanks, lol). Of course, to this day I still have to force myself not to cast the result of malloc, but that day inspired me to stop doing it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print Error in Console and both Error & Output in Log file - UNIX

I am writing a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file. But i am facing the below error. #! /bin/sh errExit () { errMsg=`cat... (1 Reply)
Discussion started by: sarathy_a35
1 Replies

2. 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

3. Solaris

Rpcinfo: can't contact portmapper: RPC: Authentication error; why = Failed (unspecified error)

I have two servers with a fresh install of Solaris 11, and having problems when doing rpcinfo between them. There is no firewall involved, so everything should theoretically be getting through. Does anyone have any ideas? I did a lot of Google searches, and haven't found a working solution yet. ... (2 Replies)
Discussion started by: christr
2 Replies

4. UNIX for Dummies Questions & Answers

> 5 ")syntax error: operand expected (error token is " error

im kinda new to shell scripting so i need some help i try to run this script and get the error code > 5 ")syntax error: operand expected (error token is " the code for the script is #!/bin/sh # # script to see if the given value is correct # # Define errors ER_AF=86 # Var is... (4 Replies)
Discussion started by: metal005
4 Replies

5. 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

6. AIX

nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error

-------------------------------------------------------------------------------- Hello, help me please. I am trying to create a mksysb bakup using nim. I am geting this error, how to correct it ? : Command : failed stdout: yes stderr: no... (9 Replies)
Discussion started by: astjen
9 Replies

7. 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

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. Programming

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... (3 Replies)
Discussion started by: khoma
3 Replies

10. UNIX for Dummies Questions & Answers

Error: Internal system error: Unable to initialize standard output file

Hey guys, need some help. Running AIX Version 5.2 and one of our cron jobs is writing errors to a log file. Any ideas on the following error message. Error: Internal system error: Unable to initialize standard output file I'm guessing more info might be needed, so let me know. Thanks (2 Replies)
Discussion started by: firkus
2 Replies
Login or Register to Ask a Question