1

Very confused on why I'm getting a "Segmentation Fault (core dumped)".

const int bufSize = 32;
char* splitStrings[bufSize];
splitStrings[0][0] = 'a';
splitStrings[0][1] = '\0';
printf("testchar: %c",splitStrings[0][0]);
printf("teststr: %s",splitStrings[0]);

Thanks for any help

3
  • 2
    You declared a list of 32 char *s. And what they point to is undefined. Commented Nov 21, 2018 at 12:32
  • malloc is your friend. Commented Nov 21, 2018 at 12:41
  • amen to malloc, thanks guys Commented Nov 21, 2018 at 13:07

4 Answers 4

3

You missed a layer of declaration. You have a table of pointer on char splitStrings. Then you would need to book some memory space and point to it using your pointer. Here is the example for the first pointer:

splitStrings[0] = malloc(2*sizeof(char));
splitStrings[0][0] = 'a';
splitStrings[0][1] = '\0';
printf("testchar: %c",splitStrings[0][0]);
printf("teststr: %s",splitStrings[0]);

Read how malloc is working and especially how to de-allocate memory with free once your done to avoid memory leak.

Sign up to request clarification or add additional context in comments.

1 Comment

Because by definition sizeof(char) will always == 1, your statement can be simplified to ...malloc(2);
2

I think you want to define a 2-D array with char elements.

To do this make these steps:

First

Define a char** array of pointers.

char **splitStrings[bufSize];

Second

Allocate memory for row:

splitStrings = malloc(bufSize*sizeof(char*));

Third

For each row allocate memory for its columns.

for (int i=0;i<bufSize;i++){
   splitStrings[i]=malloc(bufSize*sizeof(char*));
}

Code

#include <stdio.h>///for printf()
#include <stdlib.h>///for malloc()

int main(void){

    const int bufSize = 32;
    char **splitStrings;

    splitStrings = malloc(bufSize*sizeof(char*));

    for (int i=0;i<bufSize;i++){
        splitStrings[i]=malloc(bufSize*sizeof(char*));
    }

    splitStrings[0][0] = 'a';
    splitStrings[0][1] = '\0';

    printf("testchar: %c\n", splitStrings[0][0]);
    printf("teststr: %s\n", splitStrings[0]);
    
    free(splitStrings);

    return 0;
}

Further Notes

  • For more information about 2-D dynamic array see

  • Using malloc() function in C to obtain heap memory, never cast the result see

  • After allocating dynamic memory in Heap never forget to release it by using free().

  • I suggest you read more about how to allocate dynamic memory in C with malloc() function.

  • Also check result of malloc() to ensure true return value(a pointer to allocated memory).

malloc manpage error section

ERRORS calloc(), malloc(), realloc(), and reallocarray() can fail with the following error:

   ENOMEM Out of memory.  Possibly, the application hit  the  RLIMIT_AS
          or RLIMIT_DATA limit described in getrlimit(2).

Last Edition on code

Change malloc(bufSize*sizeof(char)); to malloc(bufSize*sizeof(char*)); this is true way of allocating memory

Because in first case calling free() on splitStrings pointer cause memory error. Why?

1 Comment

I believe OP is still a begginer in terms of C, and given the question's content, I would assume he will have a lot of trouble understanding this answer without any explanation or comment.
1
char* splitStrings[bufSize]; //is expecting address, if it is derefencing directely will through segmentation fault.

we have two options one is dynamic memory allocation otherwise declare like below.

#include<stdio.h>
void main()
{
    const int bufSize = 32;
    char splitStrings[1][bufSize];
    splitStrings[0][0] = 'a';
    splitStrings[0][1] = '\0';
    printf("testchar: %c",splitStrings[0][0]);
    printf("teststr: %s",splitStrings[0]);
}

Comments

1

Others have answered how to fix the error, so I won't go over that. Instead, I will answer the question of why you are getting a segmentation fault.

So, let's step through your code and see what's happening!

const int buffsize = 32;
char* splitStrings[buffsize];

Alright! So far, you have declared a const int of size 32 (good use of const btw!), and you have created a pointer to an array of characters of size 32!

So let's look at the next line!

splitStrings[0][0] = 'a';

So now, you are trying to look at the first item in the array that splitStrings points to, and then looking at the first item in that array. And this is where you get your segmentation fault!

In C, a segmentation fault occurs when something tries to access memory that it is not allowed to access. In this case, it's alright for you to access splitStrings[0], but not splitStrings[0][0]. This is because you currently don't have an array at splitStrings[0] - instead, you just have an unassigned pointer to a character.

So, when the compiler tries to work through that, it says "Okay, let's look at the first item in splitStrings! Alright, that's a pointer to a character! Now, let's look at the first item under that character! It's - wait, hold on. I haven't assigned that yet, it isn't allowed to have its own array yet! Segmentation fault!"

To fix this, you will need to create a 2D array (that's an array of arrays) and allocate that memory. EsmaeelE has given good instructions on how to do that in their answer here. I also highly recommend the TutorialsPoint segments on multi-dimensional arrays and arrays of pointers. The site is an excellent resource for anyone trying to learn the C programming language!

I hope that helped you understand the segmentation fault better! Feel free to ask for any clarifications!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.