-1

So this course I'm doing wants us to play around with memory management and pointers. I'm not really fully understanding them.

I keep getting a error:

Segmentation fault (Core dumped)

Apparently I don't have access to memory?

It's something wrong in my slen function?

/*

In these exercises, you will need to write a series of C functions. Where possible, these functions should be reusable (not use global variables or fixed sized buffers) and robust (they should not behave badly under bad input eg empty, null pointers) .
As well as writing the functions themselves, you must write small programs to test those functions.

- Remember, in C, strings are sequences of characters stored in arrays AND the character sequence is delimited with '\0' (character value 0).

----------------------------------------------------

1) int slen(const char* str)
    which returns the length of string str [slen must not call strlen - directly or indirectly]
*/

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

/* Returns the length of a given string */
int slen(const char* str) {
    int size = 0;
    while(str[size] != '\0') {
        size++;
    }
    return size;
}

/*
2) char* copystring(const char* str)
    which returns a copy of the string str. [copystring must not call any variant of strcpy or strdup - directly or indirectly]*/
char* copystring(const char* str) {
    int size = slen(str);
    char *copy = (char*) malloc (sizeof(char) * (size + 1));
    copy[size] = '\0';
    printf("before loop");
    int i = 0;
    while (*str != '0') {
        copy[i++] = *str++;
    }
    return copy;
}


int main() {
    char *msg = NULL;
    printf("Enter a string: ");
    scanf("%s", &msg);
    int size = slen(msg);
    //printf("The length of this message is %d.", size);
//  printf("Duplicate is %s.", copystring(msg));

    // Reading from file


}
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 2
    Here's a hint at least: what do you expect msg to point to when you give it to scanf? – Wander Nauta Aug 18 '15 at 16:14
  • in C, when calling malloc() and family of functions. the returned value is a void*. A void pointer can be assigned to any other pointer, so do not cast the returned value. the expression 'sizeof(char)' is defined as 1, so has no effect on the parameter passed to malloc(). Suggest declutter the code by removing the cast and removing the 'sizeof(char)' expression. – user3629249 Aug 18 '15 at 22:08
  • regarding this line: 'char *msg = NULL;' 'msg' points to address 0, which is not allowed to be accessed/written to by a user program. suggest using 'readline()' or some other method to get 'msg' pointing to some value place in memory. – user3629249 Aug 18 '15 at 22:11

2 Answers2

4

The problem isn't in your slen function, it happens before that when you're using scanf:

  • you need to make some space for the string that you're reading from the user using scanf
  • you don't need to pass the address of your memory buffer to scanf, the variable is already holding an address.

Amended code:

char msg[101];
printf("Enter a string: ");
scanf("%s", msg);
int size = slen(msg);

Alternately, if you're being asked to learn about memory allocation, study the usage of malloc:

char *msg = malloc(101);
printf("Enter a string: ");
scanf("%s", msg);
int size = slen(msg);

While learning about malloc, don't forget to also study up on the associated usage of free.

Also important and significant here is the management of your buffer size: when you make memory for the string that you'll be scanning from the user, you should put a limit on the amount of string that you actually read. There are a few ways to do this: start by studying the scanf format string, where you can use:

scanf("%100s", msg);
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 3
    While we are here, `scanf("%100s", msg)` will be better as it would avoid any buffer overflow... – Serge Ballesta Aug 18 '15 at 16:17
  • @SergeBallesta sure, but I thought that buffer size limit handling was probably beyond the scope of this lesson – pb2q Aug 18 '15 at 16:18
  • IMHO beginners never learn to early to protect themselves from buffer overflow and are never too cautious about them :-) – Serge Ballesta Aug 18 '15 at 16:20
  • @SergeBallesta sure, and I think that your comments here are a useful and gentle introduction. But I'll add a blurb in the answer thx. – pb2q Aug 18 '15 at 16:21
0

You need to assign memory to msg in your main Either use char msg[10] or use malloc. char *msg = malloc(10*sizeof(char))

Nishant
  • 1,635
  • 1
  • 11
  • 24