LinuxQuestions.org
Help answer threads with 0 replies.
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 07-07-2012, 12:35 AM   #1
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Rep: Reputation: Disabled
Post Char pointer on string malloc-ed, strcpy causes crash and burn


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

struct junk {
   char * data;
};

int main() {
   struct junk * ljunk = malloc(sizeof(struct junk));
   strcpy(ljunk->data, "GREENLAND!");
   printf("%s\n", ljunk->data);
   return 0;
}
Segmentation fault.

What is happening? How do I fix this?
 
Old 07-07-2012, 02:28 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
You need to malloc ljunk->data too after the struct allocation.
Add this line before the strcpy:
Code:
ljunk->data = malloc(sizeof(*(ljunk->data)) * 100);
and the program should be working ok.
I put 100 but it's an overkill for the "GREENLAND!" string
 
Old 07-07-2012, 08:23 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
A simpler approach is to use strdup(), not malloc() followed by strcpy(). Also, don't forget to free() allocated memory.

Something like:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct junk {
   char * data;
};

int main() {
   struct junk * ljunk = malloc(sizeof(struct junk));
   ljunk->data = strdup("GREENLAND!");
   printf("%s\n", ljunk->data);

   
   free(ljunk->data);
   free(ljunk);
   

   return 0;
}
 
Old 07-07-2012, 12:16 PM   #4
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
No dice. Whenever I print the string I get segfault.

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

struct junk {
   char * data;
};

int main() {
   struct junk * ljunk = malloc(sizeof(struct junk));

   ljunk->data = malloc(sizeof(char) * 32); // I forgot to post this which is not a minor detail

   memcpy(ljunk->data, "GREENLAND!", strlen("GREENLAND!"));
   /* This doesn't blow it up */

   strcpy(ljunk->data, "GREENLAND!");
   /* This doesn't blow it up */

   ljunk->data = strdup("GREENLAND!");
   /* This doesn't blow it up */

   printf("%s\n", ljunk->data);
   /* But no matter what, this does */

   free(ljunk->data);
   free(ljunk);

   return 0;
   /* So we never get here */
}
Thanks for any help.

Last edited by 809areacode; 07-07-2012 at 12:21 PM. Reason: memcpy missing a parameter, my mistake on fast entry & forgot semicolon
 
Old 07-07-2012, 12:32 PM   #5
809areacode
LQ Newbie
 
Registered: Aug 2011
Location: Godthåb, Grønland
Distribution: Debian
Posts: 28

Original Poster
Rep: Reputation: Disabled
NVM, I found the problem. It was another variable in the struct

struct junk {
double otherdata; /* this one */
char * data;
};

That was causing the sigsev in printf
...
printf("otherdata: %d\tdata: %s\n", ljunk->otherdata, ljunk->data);

Thanks for all help, though.
 
Old 07-07-2012, 02:15 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
When printing 'otherdata', which has been declared as a double, you would want to use the "%g" format option, not the "%d" option.
 
1 members found this post helpful.
Old 07-08-2012, 11:34 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
By the way, if you pass the right options (-Wformat, also enabled by -Wall) to gcc it will warn you about incorrect format strings.
 
1 members found this post helpful.
  


Reply



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
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast nasim751 Programming 2 02-10-2008 10:47 PM
is malloc necessary for char *s? duryodhan Programming 2 11-14-2006 12:48 PM

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

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