0

So I have created a struct, in which one of the variable is a pointer to a dynamic array of chars. So I implemented it as a pointer to pointer. Then I used a separate function to initialize the struct:

#include<stdio.h>
#include<stdlib.h>
//create a struct
typedef struct{
    //use a double pointer
    char **dynamicArray; 
    int size;
    int topValue; 
}Stack; 

/*
    Inintializes the stack
    Dyanmic Array will have a size of 2
*/
void intializeStack(Stack *stack){
    stack->size = 2; 
    stack->topValue = 0;

    //create a dyanmic array of char and set the value in the struct to the address for the newly created array
    char *dyanmic; 
    dyanmic = malloc(2 * sizeof(char)); 
    stack->dynamicArray = &dyanmic; 
}
int main(){

    Stack stack; 
    intializeStack(&stack);

    printf("stack.size: %d\n", stack.size);
    printf("stack.topValue: %d\n", stack.topValue); 
    int i; 
    for (i = 0; i < stack.size; i++){
        *(stack.dynamicArray)[i] = 'r'; 
        printf("%d value of the dynamic array: %c\n", i, *(stack.dynamicArray)[i]);
    }

    printf("Check if the stack is empty: %s\n",isEmpty(&stack)?"true":"false");

    return 0; 
}

The array is initially set to a size of 0. The problem is when I try to access the second element in the array, i get a segmentation fault error.

Segmentation fault (core dumped)

Am I doing something wrong in the implementation?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Vineet Patel
  • 477
  • 7
  • 17
  • 2
    Undefined behavior for using the address of an object after the end of the object's lifetime. – EOF Sep 19 '17 at 19:45
  • 1
    Why the pointer-to-pointer? From everything I can see, you want single-indirection; not double. – WhozCraig Sep 19 '17 at 19:48
  • @WhozCraig Because I have to dynamically change it's size, so I could create a temp array that's bigger than then set the pointer to that. – Vineet Patel Sep 19 '17 at 19:49
  • 1
    @VineetPatel you can do that with a `char*` – bolov Sep 19 '17 at 19:52
  • `char *dyanmic; dyanmic = malloc(2 * sizeof(char)); stack->dynamicArray = &dyanmic; ` saves a pointer to an object that no longer exists! – Ajay Brahmakshatriya Sep 19 '17 at 19:53
  • Possible duplicate of [What is a segmentation fault?](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) – Bernhard Barker Sep 19 '17 at 19:54
  • @AjayBrahmakshatriya but I used malloc, shouldn't that memory still exist in the heap? – Vineet Patel Sep 19 '17 at 19:55
  • @VineetPatel You used `malloc` for the value of `dynamic`, but that variable itself is in `auto` scope of the function. The pointer is the object that will die out after the function. The malloced memory will still remain. Why don't you make s`tack->dynamicArray` also `char*` and simply assign it? – Ajay Brahmakshatriya Sep 19 '17 at 19:58
  • 2
    The memory of *what*? You're retaining the address of a local automatic variable that ceases to exist as soon as your function returns. There is a big difference between `&dyanmic` and `dyanmic`. [See it live](https://ideone.com/Ko1MSD). – WhozCraig Sep 19 '17 at 19:58
  • @WhozCraig You're right, my bad. – Vineet Patel Sep 19 '17 at 20:05
  • @WhozCraig I ended up implemented your approach. Thank you! – Vineet Patel Sep 19 '17 at 20:25

2 Answers2

1

The following construct is confusing and ultimately incorrect:

for (i = 0; i < stack.size; i++){
      *(stack.dynamicArray)[i] = 'r'; 
      printf("%d value of the dynamic array: %c\n", i, *(stack.dynamicArray)[i]);
}

You are actually referencing the FIRST level of ** by this construction. Try this instead:

for (i = 0; i < stack.size; i++){
    stack.dynamicArray[0][i] = 'r';
    printf("%d value of the dynamic array: %c\n", i, stack.dynamicArray[0][i]);
}
Blunt Jackson
  • 616
  • 4
  • 17
0

Abstracting ffrom the sense of this :)

char **dyanmic; 
dyanmic = malloc(sizeof(char *));
*dyanmic = malloc(2 * sizeof(char)); 
stack->dynamicArray = dyanmic; 
0___________
  • 60,014
  • 4
  • 34
  • 74