user2899069
user2899069

Reputation: 31

A small yet weird <Segmentation Fault>

I am not very experienced with C memory concepts. I tried searching for a solution, but could not find one.

I am just trying to create dynamic array in C. I tried to create it in this way while trying to check whether the addresses are contiguous. The program ran fine.

However, I got a segmentation fault after the statement system("pause"). I also tried to debug using the debugger, with no luck! I'm using Dev CPP.

Can anybody guide me?

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

main()
{
 int a[0], *ptr, i;

 printf("%d", sizeof(a[0]));
 a[0]=1;
 for(i=1;i<10;i++)
 {
  ptr=(int *) malloc(sizeof(int));
  printf("Enter a[%d]:  ", i);
  a[i]= *ptr;
  scanf("%d", &a[i]);

 }
 i=0;
 while(i<10)
 {printf("\n%u", &a[i++]);}
 free(ptr);
 system("pause");
}

Upvotes: 3

Views: 120

Answers (6)

Jacob Minshall
Jacob Minshall

Reputation: 1054

First your a[0] should be a[10] for the way your are trying to use it. Second, just malloc to the pointer and it is basically an array. Though you don't need to do it in a for loop. You can just say ptr = malloc(10 * sizeof(int)); and you will be good. No need to cast it. You can access it's elements through the array subscript like ptr[9].

Upvotes: 1

jaychris
jaychris

Reputation: 131

There are lots of problems in your code.

  1. int a[0]. You are defining an array of 0 elements. In C this will not work. You cannot do a[0]=1 since it will work only if a is an array of 1 or more elements.
  2. It gets worse with a[i]=*ptr. 2 problems here, as pointed above a[1], a[2], etc don't exist. and the second problem is you allocated ptr but assigning *ptr which might be having some garbage.

It is just the beginning.

Upvotes: 2

Mike Makuch
Mike Makuch

Reputation: 1838

Several problems: 1) Your expecting your array to be 10 elements long but you're declaring it with 0, and make it an array of pointers;

int *a[10];

then

a[i] = (int*)malloc(sizeof(int));

and when freeing;

free(a[i]);

Upvotes: 0

SJuan76
SJuan76

Reputation: 24780

Well, your dealing with a is epical:

int a[0];  // no-items array
...

a[0]=1;  // WHAT?
for(i=1;i<10;i++)
{
  ptr=(int *) malloc(sizeof(int));
  a[i]= *ptr; // a was int or pointer? And A[9]?
}

Upvotes: 0

Doug T.
Doug T.

Reputation: 65599

int a[0]

doesn't have any space allocated to it (its 0 width)

Yet you write to it on this line:

a[0]=1;

You also assume it has 10 elements here:

while(i<10)
{printf("\n%u", &a[i++]);}
free(ptr);

Practically speaking, as this is just stack memory, you're just writing to another piece of the stack. You could, for example, be overwriting the value of ptr. Often this can go undetected until the stack is unwound and part of it is apparently corruptted. Here the stack is unwound right after system("pause"), when main returns.

(Also, if ptr is overwritten by your writes to a you can't be sure that free does anything reasonable.)

To allocate a 10 integer array, use the syntax:

int a[10];

Then you can use a[0] up to a[9]. But this is C don't expect anything to protect you when you try to read/write to a[10].

Upvotes: 3

Beta
Beta

Reputation: 99094

int a[0], ...;
...
a[0]=1;

You are writing out of bounds. This is undefined behavior.

Upvotes: 1

Related Questions