The malloc part crashes in the following code, but only in VS and not in CodeBlocks. As I have learned, that means that I am probably triggering some undefined behavior. But I can't figure out why...
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define TableLength 29
typedef int info;
typedef int tipkey;
typedef struct element
{
tipkey key;
info info;
} element;
typedef struct node* nodepointer;
typedef struct node
{
element element;
nodepointer next;
} tipnod;
typedef nodepointer table[TableLength];
int main()
{
table table;
for (int i = 0; i < TableLength; i++)
{
table[i] = NULL;
}
for (int i = 0; i < TableLength; i++)
{
element el = { i, i };
table[i] = (nodepointer)malloc(sizeof(nodepointer));
table[i]->element = el;
table[i]->next = NULL;
}
getch();
return 0;
}
}```
p = malloc(sizeof *p);orp = (T *)malloc(sizeof(T));. (Most of us prefer the first one since it is guaranteed to be correct; the latter can be wrong if you get T wrong , but will at least cause a compiler diagnostic in that case)