-2
#include<stdio.h>
#include<stdlib.h>
#define MAX 256
int main()
{
    char **ptr;
    char input[MAX];
    int i=0,j=0,k=0;
    printf("Enter the string\n");
    scanf("%[^\n]",input);

    ptr=(char **)malloc(8+1);
    if(ptr==NULL)
    {
            printf("ptr malloc: error\n");
            return 0;
    }
    for(i=0;i<8;i++)
    {
            ptr[i]=(char*)malloc(MAX);
            if(ptr[i]==NULL)
            {
                    printf("malloc: Error\n");
                    return 0;
            }
    }
    for(i=0;i<8;i++) {
            k=0;
            if(i!=0)
                    j++;
            while(input[j]!=' ' && input[j]!='\0') {
            ptr[i][k++]=input[j];
            j++;
            }
            ptr[i][k]='\0';
    }
    for(i=0;i<8;i++)
            printf("%s\n",ptr[i]);
  return 0;
 }


GDB traces:
(gdb) p j
$17 = 12
(gdb) n
31                      ptr[i][k++]=input[j];
(gdb) p i
$18 = 4
(gdb) p k
$19 = 0
(gdb) p j
$20 = 12
(gdb) p input[j]
$21 = 119 'w'
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400625 in main () at kk.c:31
31                      ptr[i][k++]=input[j];

Here, I am providing input as 9 words separated by one space. Till ptr[3] it is going smooth after that it got crashed. I want each pointer in double pointer to point to one word, later I want to calculate frequency of same word.

input : w1 w2 w3 w1 w4 w2 w3 w1 w1 ,it got crashed for i=4

Edit: I want output like this output : Each word in new line.

Saawaj
  • 41
  • 9
  • 2
    The size you give to [`malloc`](http://en.cppreference.com/w/c/memory/malloc) is the size of the memory you want to allocate, ***in bytes***. Eight pointers will take up quite a lot more space than nine bytes, especially on a 64-bit system (where a pointer is typically eight bytes). – Some programmer dude Oct 16 '17 at 06:30
  • 2
    You forgot to include `#include `, but there are other problems – Jabberwocky Oct 16 '17 at 06:31
  • 2
    Oh, that you missing to include `` make [this question](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) very relevant. I suggest you read it, and its answers. – Some programmer dude Oct 16 '17 at 06:31
  • What is this program supposed to do? What is the expected output for a given input? – Jabberwocky Oct 16 '17 at 06:33
  • `i<8` --> `i<9` – BLUEPIXY Oct 16 '17 at 06:34
  • @MichaelWalz : I want to calculate frequency of same words. – Saawaj Oct 16 '17 at 06:35
  • @Saawaj What is the expected output for a given input?? [Edit] your question and show us an example please. – Jabberwocky Oct 16 '17 at 06:36
  • 2
    Lastly, and somewhat related to your problems, but don't the compiler give a few warnings? Warnings are signs that you have done something wrong, something that you should fix. For example, you have a single `return;` statement, without returning a value. That's not valid in a function declared to return an `int` (like `main` is). – Some programmer dude Oct 16 '17 at 06:36
  • Make sure you're compiling with enough warnings. If you can get away without including ``, you are not using anywhere near enough compilation warnings. It simply isn't safe to run code that has been compiled without enough warnings enabled (preferably as errors) — and with no warnings showing when you compile. I use `gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes …` and often a few more, and I can't even run the code unless it compiles without any warnings because warnings are treated as errors. – Jonathan Leffler Oct 16 '17 at 07:09

2 Answers2

3

You are not allocating memory properly for ptr. You need to change:

ptr = (char **)malloc(8+1);

to:

ptr = malloc( (8+1) * sizeof(char*));

as your statement implies that you want to allocate 8+1 bytes (rather than pointers).

Also remember not to cast the result of malloc.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Marievi
  • 4,951
  • 1
  • 16
  • 33
0

As pointed out by Marievi, you pass the wrong parameter to the first malloc call.

Using malloc directly each time you want to allocate memory is a popular practice among C programmers. However, it is very error prone since, like you experienced, it's easy to get the size wrong. In my C projects I always define the following macro:

#define NEW_ARRAY(ptr, n) \
    (ptr) = malloc((n) * sizeof (ptr)[0]); \
    if ((ptr) == NULL) { \
        fprintf(stderr, "malloc: Error\n"); \
        exit(EXIT_FAILURE); \
    }

It is also a good idea to keep track of the length of a dynamic array in a separate variable. Your memory allocation code now becomes

ptrLen = 8 + 1;
NEW_ARRAY(ptr, ptrLen);
for (i = 0; i < ptrLen; i++) {
    NEW_ARRAY(ptr[i], MAX);
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60