c - dereferencing issue

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-04-29T11:16:15Z Indexed on 2010/04/29 11:17 UTC
Read the original article Hit count: 352

Filed under:
|

Hi,

I have simplified an issue that I've been having trying to isolate the problem, but it is not helping.

I have a 2 dimensional char array to represent memory. I want to pass a reference to that simulation of memory to a function. In the function to test the contents of the memory I just want to iterate through the memory and print out the contents on each row.

The program prints out the first row and then I get seg fault.

My program is as follows:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

void test_memory(char*** memory_ref)  {

    int i;
    for(i = 0; i < 3; i++)  {
        printf("%s\n", *memory_ref[i]);
    }
}

int main()  {
    char** memory;
    int i;
    memory = calloc(sizeof(char*), 20);
    for(i = 0; i < 20; i++)  {
        memory[i] = calloc(sizeof(char), 33);
    }

    memory[0] = "Mem 0";
    memory[1] = "Mem 1";
    memory[2] = "Mem 2";

    printf("memory[1] = %s\n", memory[1]);

    test_memory(&memory);

    return 0;
}

This gives me the output:

memory[1] = Mem 1
Mem 0
Segmentation fault

If I change the program and create a local version of the memory in the function by dereferencing the memory_ref, then I get the right output:

So:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

void test_memory(char*** memory_ref)  {

    char** memory = *memory_ref;
    int i;
    for(i = 0; i < 3; i++)  {
        printf("%s\n", memory[i]);
    }
}

int main()  {
    char** memory;
    int i;
    memory = calloc(sizeof(char*), 20);
    for(i = 0; i < 20; i++)  {
        memory[i] = calloc(sizeof(char), 33);
    }

    memory[0] = "Mem 0";
    memory[1] = "Mem 1";
    memory[2] = "Mem 2";

    printf("memory[1] = %s\n", memory[1]);

    test_memory(&memory);

    return 0;
}

gives me the following output:

memory[1] = Mem 1
Mem 0
Mem 1
Mem 2

which is what I want, but making a local version of the memory is useless because I need to be able to change the values of the original memory from the function which I can only do by dereferencing the pointer to the original 2d char array.

I don't understand why I should get a seg fault on the second time round, and I'd be grateful for any advice.

Many thanks

Joe

© Stack Overflow or respective owner

Related posts about c

    Related posts about dereference