1

I am working on a homework assignment that requires the creation of a dynamically allocated array which is to be populated with strings from a text file. I then need to print the array to standard output, shuffle the array, and then print it again.

My current issue is that I cannot seem to populate the array with anything without getting a segmentation fault. I tested the program with a static array and everything worked, so I know there isn't an issue with any of the other code.

Here is a section of my program.

void alloc2dArray(char ***source, int dim1, int dim2)
{
    int i = 0;

    source = malloc(sizeof(char *) * dim1);

    if(source == NULL) 
    { 
        printf("Memory full!");
        exit(EXIT_FAILURE);
    }
    for(i = 0; i < dim1; i++)
    {
            source[i] = malloc(sizeof(char) * (dim2 + 1));
            if(source[i] == NULL) 
        { 
            printf("Memory full!"); 
            exit(EXIT_FAILURE);
        }
    }
}

EDIT:

In an effort to avoid being a Three Star Programmer I changed my code to the below snippet. As luck would have it, that fixed my issue. So thanks to Kniggug for posting the link to something I had no idea about before.

char** alloc2dArray(int dim1, int dim2)
{
        int i = 0;

        char **twoDArray = malloc(sizeof(char *) * dim1);

        if(twoDArray == NULL)
        {
                printf("Memory full!");
                exit(EXIT_FAILURE);
        }
        for(i = 0; i < dim1; i++)
        {
                (twoDArray[i]) = malloc(sizeof(char) * (dim2 + 1));
                if(twoDArray[i] == NULL)
                {
                        printf("Memory full!");
                        exit(EXIT_FAILURE);
                }
        }

        return twoDArray;
}

Thank you.

3
  • 4
    Three star programmer! Commented Oct 28, 2013 at 21:25
  • The answers posted look right, but you should understand why they're correct. The above code in the alloc2dArray function is modifying a copy of the address of **source. What you need to do is modify the value pointed to by ***source, which is accomplished through *source as mentioned twice below. Commented Oct 28, 2013 at 21:42
  • jucestain, thanks for the clarification. I am still trying to wrap my head around pointers. This is only an intro course and my first time trying my hand at C. Commented Oct 28, 2013 at 21:51

2 Answers 2

4
Void alloc2dArray(char ***source, int dim1, int dim2)
{
    int i = 0;

    source = malloc(sizeof(char *) * dim1);

The assignment above has no effect outside this function, apart from leaking memory. You mean to do:

    *source = malloc(sizeof(char *) * dim1);

similarly:

(*source)[i] = malloc(sizeof(char) * (dim2 + 1));
Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate the quick response. I tried the changes that you suggested, but that caused a segmentation fault even if I comment out the problem strcpy line. I have included more of the program to perhaps help figure out if I have a larger issue.
Do you have valgrind installed? If so run under valgrind, if not use a debugger to see where it crashes.
@Chad, also set the maxChars to something larger than 0
Thanks again for the help perreal. I tried Valgrind and found that I had several errors as well as leaks. This prompted me to try to rework the code and I was able to fix it.
1

Change source to (*source) in alloc2dArray

Void alloc2dArray(char ***source, int dim1, int dim2)
{
    int i = 0;

    *source = malloc(sizeof(char *) * dim1);

    if(*source == NULL)
    {
        printf("Memory full!");
        exit(EXIT_FAILURE);
    }
    for(i = 0; i < dim1; i++)
    {
        (*source)[i] = malloc(sizeof(char) * (dim2 + 1));
        if((*source)[i] == NULL)
        {
                printf("Memory full!");
                exit(EXIT_FAILURE);
        }
    }
}

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.