I get access violation if I'm allocating memory to list->v[list->length]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char* denumire, * producator;
int cantitate;
}produs;
typedef struct {
produs** v;
int lungime;
int capacitate;
} Lista;
produs crearePr(char* nume, char* producator, int cantitate)
{
produs material;
material.denumire = (char*)malloc(strlen(nume) + 1);
strcpy(material.denumire, nume);
material.producator = (char*)malloc(strlen(producator) + 1);
strcpy(material.producator, producator);
material.cantitate = cantitate;
return material;
}
void creareLi(Lista* lista)
{
lista->lungime = 0;
lista->capacitate = 1;
lista->v = (produs**)malloc(sizeof(produs*) * lista->capacitate);
}
void redim(Lista *l)
{
l->capacitate *= 2;
l->v = (produs**)realloc(l->v,sizeof(produs) * l->capacitate);
}
void adauga(Lista *lista, produs p)
{
if (lista->lungime == lista->capacitate) redim(&lista);
lista->v[lista->lungime] = (produs*)malloc(sizeof(produs));
*lista->v[lista->lungime] = p;
lista->lungime++;
}
int main()
{
Lista lista;
creareLi(&lista);
adauga(&lista, crearePr("Zahar", "Tarom", 100));
return 0;
}
Explanation:
crearePrshould create astruct produscreareLishould initialize the list, set capacity 1 and length 0redimis called to expand the capacityadaugashould addstruct produsto list
If you enable warnings in your compiler, you'll see something like this:
That's telling you something very important. What you have right now causes undefined behavior.
The variable
listais a pointer to the location of your data in memory. You are reinterpreting&redisas that pointer, but now theredisfunction is going to modify memory where thelistapointer was stored, instead of what it was pointing at.This will result in some form of memory corruption, as your struct is larger than a pointer and you're writing into uncharted territory.
Changing the call to
redim(lista)will fix this problem.