[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

segmentation fault

Posted on 2012-09-05
7
Medium Priority
?
413 Views
Last Modified: 2012-10-28
#include<stdio.h>
char *func()
{
      
      char *a="hari";
      char *b;
      strcpy(b,a);
      return b;
}
main()
{
      
      char *c;
      c = func();
      printf("%s",c);
      
}

this code produces segmentation fault.but if i take a[100]. then the code works fine
i am missing some concept here. please help
0
Comment
Question by:nagaharikola
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 38367356
Hi nagaharikola,

to copy a string to another you have to allocate enough memory for the destination string, in you sample that char* b - try it somehow like this:
      char *a="hari";
      char *b = (char*)malloc( strLen( a ) + 1 ); // length of 'a' + terminating 0-char
      strcpy(b,a);

Open in new window

BTW, to just duplicate a string you can even use strdup, i.e.:
char *func()
{
      char *a="hari";
      return strdup( a );
}

Open in new window

Further you have to release the allocated memory in the calling function, so add this line to your main after the printf statement:
 free( c );

Open in new window

Hope that helps,

ZOPPO
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 38374571
nagaharikola, are you trying to solve a problem (which Zoppo addressed) or are you trying to understand a concept?

~Hugh
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 38374572
(It occurs to me that the fall semester has started at many colleges.)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:nagaharikola
ID: 38375080
I am trying to understand the concept.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38375720
To learn how pointers in C work and how they are use with strings you can find a lot of info when you search the internet for something like 'tutorial c char pointer', i.e. http://www.codingunit.com/c-tutorial-strings-and-string-library-functions or http://www.cplusplus.com/doc/tutorial/pointers/
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 38376104
I find that a code example will sometimes help a student.  Please let me know if this example helped you or not.  (I'm a teacher and it helps me to know what helps and doesn't help learners.)  Feel free to ask any question you want about what the sample program is doing.

# include <stdio.h>

int main()
{
  char ch = 'x';
  char ch_array[] = "hello world!";
  char *pch_array = NULL;

  printf ( "sizeof(ch) = %d\n", sizeof(ch) );
  printf ( "sizeof(ch_array) = %d\n", sizeof(ch_array) );
  printf ( "sizeof(pch_array) = %d\n\n", sizeof(pch_array) );

  printf ( "ch = %c\n", ch );
  printf ( "ch_array = %s\n\n", ch_array );

  printf ( "address of ch = %u\n", &ch );
  printf ( "address of ch_array = %u\n", &ch_array );
  printf ( "address pointed to by pch_array = %u\n\n", pch_array );

  printf ( "Setting pch_array to point to ch_array\n" );
  pch_array = ch_array;
  printf ( "address pointed to by pch_array = %u\n\n", pch_array );

  printf ( "string pointed to by pch_array = %s\n", pch_array );
  printf ( "character pointed to by pch_array = %c\n", *pch_array );
  printf ( "character pointed to by (pch_array+6) = %c\n", *(pch_array+6) );

  return 0;
}

Open in new window


Output

sizeof(ch) = 1
sizeof(ch_array) = 13
sizeof(pch_array) = 4

ch = x
ch_array = hello world!

address of ch = 3219661435
address of ch_array = 3219661422
address pointed to by pch_array = 0

Setting pch_array to point to ch_array
address pointed to by pch_array = 3219661422

string pointed to by pch_array = hello world!
character pointed to by pch_array = h
character pointed to by (pch_array+6) = w

Open in new window

0
 
LVL 8

Accepted Solution

by:
vinhnl earned 2000 total points
ID: 38406308
Hi Nagaharikola,

If you declare:

char *a="hari";
char *b;

You mean that:

a points to a memory space ['h','a','r','i',0].
b points to any address. Maybe content of this address is ['N', 'a', 'g', 'a', 'h', 'a', 'r', 'i', 'k', 'o', 'l', 'a', 0] like your name.

When you call strcpy(b,a), this function will copy from begin of content of b to a util caracter 0. In this case, free space of a is so small (5 chars). It produces a segmentation fault because the tail of b is copied into a private memory (memory of another variable or of running program).

If you declare a[100], your machine allocate for you 100 caracters. This space memory is enough for you copy from variable b to a. But if b points to a content no end (it means that a space with 1000 caracters 'x' for example), it also produces a segmentation fault like your code.

If you run your code 1000 times, I believe that you will meet sometime segmentation fault.

Hope this helps
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question