LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 02-20-2010, 01:12 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
segmentation fault during memcpy()


Please point out what wrong I have done here ?
Why is this giving me a segmentation fault ?


Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

int main ()
{
	int  i  = 0;
	char *d = malloc ((sizeof(char)) * 50);
	
	memcpy ((void*) d, "anisha", 6);
	
	memcpy ((void*)(d + 6), (const void*)i, 4);
}
 
Old 02-20-2010, 01:19 AM   #2
sparker
Member
 
Registered: Aug 2007
Location: Canada
Distribution: OpenBSD 4.6, Debian Lenny
Posts: 64

Rep: Reputation: 16
The problem lies in:

memcpy ((void*)(d + 6), (const void*)i, 4);

If you read the man page for memcpy it gives you the prototype:

void *memcpy(void *dest, const void *src, size_t n);

In this case you are trying to copy an integer into the destination, however it is not a pointer to int. The correct solution would simply be:

memcpy ((void*)(d + 6), (const void*)&i, 4);
 
1 members found this post helpful.
Old 02-20-2010, 01:22 AM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
You don't need casts to convert a pointer to void*. Your code gives a good reason not to do it, because without the casts, you would have gotten a useful compiler error.
 
Old 02-20-2010, 01:22 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Quote:
Originally Posted by sparker
The problem lies in:

memcpy ((void*)(d + 6), (const void*)i, 4);

If you read the man page for memcpy it gives you the prototype:

void *memcpy(void *dest, const void *src, size_t n);

In this case you are trying to copy an integer into the destination, however it is not a pointer to int. The correct solution would simply be:

memcpy ((void*)(d + 6), (const void*)&i, 4);
Many thanks for replying!!

I tried what you said it worked fine..

and then I tried the following also, it also worked fine !
Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

int main ()
{
	int  *i = malloc ((sizeof(char)) * 50);
	*i = 0;
	
	char *d = malloc ((sizeof(char)) * 50);
	
	memcpy ((void*) d, "anisha", 6);
	
	memcpy ((void*)(d + 6), (const void*)i, 4);
	
	printf ("\n== %s ==", d);
}
 
Old 02-20-2010, 01:26 AM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Quote:
Originally Posted by Dan04
You don't need casts to convert a pointer to void*. Your code gives a good reason not to do it, because without the casts, you would have gotten a useful compiler error.
Thanks for replying!

Sorry I couldn't understand what you were trying to say, please explain it once more !
 
Old 02-20-2010, 01:38 AM   #6
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Ok now the segmentation fault problem has been solved, but when I am trying to print the string, it only shows the "anisha" string and not the number attached at the end ?

May I know what's the problem now ?
Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

int main ()
{
	int  i  = 1;	
	char *d = malloc ((sizeof(char)) * 50);
	
	memcpy ((void*) d, "anisha", 6);
	
	memcpy ((void*)(d + 6), (const void*)&i, 4);
	
	memcpy ((void*)(d + 10), (const void*)"\0", 1);
	
	printf ("\n==%s==", d);
}
 
Old 02-20-2010, 02:06 AM   #7
sparker
Member
 
Registered: Aug 2007
Location: Canada
Distribution: OpenBSD 4.6, Debian Lenny
Posts: 64

Rep: Reputation: 16
You could just use something like this instead

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
	char *p = malloc((sizeof(char)) * 50);
	
	snprintf(p, 50, "%s %d", "hello", 5);
	printf("%s\n", p);

       return 0;
}
 
1 members found this post helpful.
Old 02-20-2010, 02:19 AM   #8
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Quote:
Originally Posted by sparker
You could just use something like this instead

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char *p = malloc((sizeof(char)) * 50);

snprintf(p, 50, "%s %d", "hello", 5);
printf("%s\n", p);

return 0;
}
Many thanks for the useful inputs !!

Yes, the code shown by you worked ! I'll read about snprintf more.

Last edited by TheIndependentAquarius; 02-20-2010 at 05:10 AM. Reason: Removed a question, will start a new thread for it
 
Old 02-20-2010, 04:21 AM   #9
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by anishakaul View Post
Thanks for replying!

Sorry I couldn't understand what you were trying to say, please explain it once more !
If a function is declared as taking a void*, you can pass any type of pointer. You don't need a cast. And you shouldn't cast unnecessarily because casts hide compiler warnings. If you had written

Code:
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main ()
{
        int  i  = 0;
        char *d = malloc ((sizeof(char)) * 50);

        memcpy (d, "anisha", 6);

        memcpy ((d + 6), i, 4);
}
then gcc would have told you

Code:
foo.c:12: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast
With the cast, C assumes that you meant to reinterpret the integer as a pointer. If sizeof(int) == sizeof(void*) (which is generally true on 32-bit systems), you won't get a warning.

Because the integer happens to be zero, it's interpreted as a null pointer. Dereferencing it is an error.
 
1 members found this post helpful.
Old 02-20-2010, 04:23 AM   #10
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
BTW, if you malloc(), you have to free().
 
Old 02-20-2010, 04:41 AM   #11
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
@Dan04

Thanks for the useful information !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault in memcpy George2 Programming 6 08-25-2007 11:33 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
Need help: Seg fault, Memcpy, and dynamically allocated arrays benobi Programming 3 06-09-2005 10:58 PM
Segmentation fault with ls -l Sledge Fedora 8 03-29-2005 08:02 PM
why segmentation fault??? andrewnow Programming 8 03-26-2005 06:50 PM


All times are GMT -5. The time now is 02:41 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration