0

I tried to create an Array type ADT.

// Array ADT using structure
struct ARRAY_ADT
{
    int *A; // For storing the pointer to base address of the array
    int size; //For storing the size of the array
    int len; //For storing the length of the array
};

when I initialize this struct instance call it Array using a function called init.

struct ARRAY_ADT init()
{
    struct ARRAY_ADT Array;

    printf("Enter the size of the Array: ");
    Array.size = Integerinput();

    //Creating the pointer for the Array;
    Array.A = (int*) malloc(Array.size * sizeof(int));
    //printf("Size of from init: %d", sizeof(Array.A));
    do
    {
        printf("Enter the length of the Array: ");
        Array.len = Integerinput();
    }while(OutofRange(Array.size,Array.len));

    printf("Please enter the values in the array: ");
    for(int i = 0; i < Array.len; i++)
    {
        Array.A[i] = Integerinput();
    }

    return Array;
}

Now, theoretically I can only add 4 integers if size is 4 as I have only allocated 4 memory location using malloc but in this case I can add as much number as I want to add in the Array ADT. I had commented the OutofRange function for sake of testing purpose. Even, It's not losing or overwriting any number I have checked for it too.

So, what is happening here? Why it's showing such type of behaviour?

Is there any way to correct or resolve this type of issue rather than relying on your own OutofRange function?

trincot
  • 317,000
  • 35
  • 244
  • 286
Tarster
  • 105
  • 1
  • 1
  • 9
  • 4
    C doesn't provide any automatic bounds checking. You need to implement that yourself. As for: *It's not losing or overwriting any number*. This is a result of Undefined Behaviour. If you overflow buffers the result is undefined. It may appear to "work" but may show different behaviour on a different machine, different compiler, different surrounding code, etc. – kaylum Jan 28 '21 at 00:43
  • 1
    Tip: Having variables with upper-case names is downright confusing. `Array` implies it's some kind of type, which it is absolutely not. – tadman Jan 28 '21 at 00:54
  • Why is this taking input via text prompts? That `Init()` function shouldn't be concerned with that. Add those as arguments. Get input externally if and when that is necessary. – tadman Jan 28 '21 at 00:55
  • Thank you for clearing the confusion. Sorry for bad practice of using uppercase for a variable. Is there any guide @tadman which should be followed in case of C for naming and other thing regarding good coding practices? – Tarster Jan 28 '21 at 01:03
  • 1
    C is old enough there's *many* schools of thought, but if you look to the C Standard Library you'll see a lot of conventions with respect to function and argument names. There are very few capital letters in the C standard functions, structures, or arguments, like I can't even think of any off the top. – tadman Jan 28 '21 at 01:05
  • If there is no inbuilt checker than why we get a segmentation fault on some machines when to try to access some array index that is not included in an array. @kaylum, by the way thanks for the help. – Tarster Jan 28 '21 at 01:05
  • 1
    That's not C doing the checking, a segmentation fault is your operating system saying your process violated your protected memory bounds and got nuked. On older operating systems lacking this feature the behaviour may be entirely different, like reading another process's memory. – tadman Jan 28 '21 at 01:06
  • 1
    Ok, Now I get it. Again thanks for the help. Have a nice day. – Tarster Jan 28 '21 at 01:09
  • As I said, it is Undefined Behaviour. That means the result can be seg fault, can be "correct" result, can be wrong values, etc. The exact behaviour for any given instance is unpredictable and cannot be relied on. See [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – kaylum Jan 28 '21 at 01:09
  • Yes, It perfectly answer my question. @kaylum – Tarster Jan 28 '21 at 01:21
  • C is a very power language. With great power comes great responsibility. – Black Frog Jun 13 '21 at 12:51

0 Answers0