Skip to main content
added 265 characters in body
Source Link
EvilTeach
  • 29k
  • 21
  • 89
  • 144

There are a couple of things going on here that are important details.

Evgeny is correct that you need to allocate N things of sizeof int. Otherwise your program is wrong, and will write to invalid memory locations.

The cause of the segment violation took a few minutes to track down. I added printf statements throughout your code so as to be able to see where the segment violation occurred. It showed that the assignment of 1 to the array element was where it was happening.

I then suspected that the binding/order was the issue, so put the parenthesis around the *arr to make it very clear that it was the intent. The segment violation went away after that. I also changed the initialization to index so the assignment results would be easily verifiable.

#include <stdio.h>
#include <stdlib.h>

void foo(int **arr, int N) 
{
    *arr =(int *) malloc(sizeof(int) * N);

    for (int index = 0; index < N; index++)
    {
        printf("before assignment\n");
        (*arr)[index] = index;
        printf("after assignment\n");
    }
}

int main() 
{
  int *arr; 
  foo(&arr, 5);

  for (int bar = 0; bar < 5; bar++) 
  {
    printf("%d ", arr[bar]);
  }
}

In short, a useful quick and dirty technique is to insert a bunch of printfs to help narrow down where the issue is happening.

There are a couple of things going on here that are important details.

Evgeny is correct that you need to allocate N things of sizeof int. Otherwise your program is wrong, and will write to invalid memory locations.

#include <stdio.h>
#include <stdlib.h>

void foo(int **arr, int N) 
{
    *arr =(int *) malloc(sizeof(int) * N);

    for (int index = 0; index < N; index++)
    {
        printf("before assignment\n");
        (*arr)[index] = index;
        printf("after assignment\n");
    }
}

int main() 
{
  int *arr; 
  foo(&arr, 5);

  for (int bar = 0; bar < 5; bar++) 
  {
    printf("%d ", arr[bar]);
  }
}

There are a couple of things going on here that are important details.

Evgeny is correct that you need to allocate N things of sizeof int. Otherwise your program is wrong, and will write to invalid memory locations.

The cause of the segment violation took a few minutes to track down. I added printf statements throughout your code so as to be able to see where the segment violation occurred. It showed that the assignment of 1 to the array element was where it was happening.

I then suspected that the binding/order was the issue, so put the parenthesis around the *arr to make it very clear that it was the intent. The segment violation went away after that. I also changed the initialization to index so the assignment results would be easily verifiable.

#include <stdio.h>
#include <stdlib.h>

void foo(int **arr, int N) 
{
    *arr =(int *) malloc(sizeof(int) * N);

    for (int index = 0; index < N; index++)
    {
        printf("before assignment\n");
        (*arr)[index] = index;
        printf("after assignment\n");
    }
}

int main() 
{
  int *arr; 
  foo(&arr, 5);

  for (int bar = 0; bar < 5; bar++) 
  {
    printf("%d ", arr[bar]);
  }
}

In short, a useful quick and dirty technique is to insert a bunch of printfs to help narrow down where the issue is happening.

Source Link
EvilTeach
  • 29k
  • 21
  • 89
  • 144

There are a couple of things going on here that are important details.

Evgeny is correct that you need to allocate N things of sizeof int. Otherwise your program is wrong, and will write to invalid memory locations.

#include <stdio.h>
#include <stdlib.h>

void foo(int **arr, int N) 
{
    *arr =(int *) malloc(sizeof(int) * N);

    for (int index = 0; index < N; index++)
    {
        printf("before assignment\n");
        (*arr)[index] = index;
        printf("after assignment\n");
    }
}

int main() 
{
  int *arr; 
  foo(&arr, 5);

  for (int bar = 0; bar < 5; bar++) 
  {
    printf("%d ", arr[bar]);
  }
}