0

So I've very nearly finished my final project (I think) but have come up against a problem that I think is very simple but I'm having trouble fixing.

In the following code I have a global string key, initialised via string key; at the beginning of my program that I want to have no value at the start of the subroutine findkey. I then want to append the letter c to key to then be used in another subroutine. Currently I am getting a seg fault due to an invalid write of size 1 in the line key[i]=c;.

The variable key is assigned elsewhere using GetString but only if the program goes down a different path. But the variable key, regardless of where it gets it's value, must feed into another subroutine as a string.

Any help would be hugely appreciated!

int findkey(const char* dictionary, int pos, string key)
{
// Open the dictionary file
FILE* filein = fopen(dictionary, "r");
key = NULL;
for (int c = fgetc(filein); c != EOF; c = fgetc(filein))
{
    int i = 0;
    // check whether the character is alphabetical or '
    if (isalpha(c) || (c == '\''))
    {
        key[i] = c;
        i++;
    }

1 Answer 1

2

as you probably know, the type string (defined in the CS50 library) is the same as the built-in standard type char * (aka char pointer). recall that pointers must have memory allocated for them (memory they point to) in order to be able to use them in reading/writing operations.

you are setting key to NULL (aka location 0x0). memory at NULL is special in the sense that we can't read from/write to it. this is why you get a segfault when trying to do key[i] = c; afterwards.

two main solutions are possible:

  1. use a function like malloc to allocate memory for your pointer to point to before you try to read from/write to that memory through the pointer. see man malloc for more info! in case you chose to work with that, you wanna be careful to manually free the memory that you have allocated once you are done with it in order for your program not to leak memory. see Dynamic Memory Allocation for more information (short from the next version of the course)!

  2. declare your variable as an array of chars of a width that is big enough to hold your data as well as the null character. in this case, memory is gonna be allocated automatically for you and you don't have to worry about freeing it manually. however, you wanna be careful with the scope of your variable because if you declare it inside a function for example, it will go out of scope as this function returns. see Scope for more information!

for more information on strings, see Strings!

6
  • Hey thanks a lot! So I decided to use the malloc method but strangely seem to be having a problem with Scope even though key is a global variable. I have run the program in gdb and in the subroutine findkey, key is assigned characters correctly but when returning to main it has no value. Any ideas?
    – GCOTT94
    Dec 28, 2015 at 22:46
  • @GCOTT94 how exactly are you assigning key a value inside findkey? also are you sure key is never accessed somewhere else in the code?
    – kzidane
    Dec 28, 2015 at 22:54
  • Just with the line key[i]=c;. I assumed as key is a global variable it should keep this value even outside of findkey. And it is used elsewhere but not changed anywhere else (after this point) and I checked the value in gdb as soon as findkey was exited.
    – GCOTT94
    Dec 29, 2015 at 9:07
  • @GCOTT94, well, according to what you said, key should preserve its value after findkey returns. maybe you should update your question with the whole code to take a deeper look. or you could email me with the code if you don't want to publish it.
    – kzidane
    Dec 29, 2015 at 9:22
  • I have sorted out the problem with key not working now, but I am having another problem with my program that I think will be a lot more difficult to fix. Could I possibly email you my code for you to take a look at?
    – GCOTT94
    Dec 29, 2015 at 9:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .