r/C_Programming • u/neonge1674 • Sep 01 '26
Question Why isn't my code working?
I just started learning C(2 days ago) and as a first project I decided to make some data structures, starting with dynamic arrays. I made a struct called List and some functions for. The function setList() sets the value of an index of the array, if the index is larger that the current size of the array, it resizes it. However, when i tried to use in a for loop, it didn't work despite it working elsewhere.
#include <stdio.h>
#include <stdlib.h>
#define itirate(index, limit) for(int index = 0; index < limit; index++)
typedef struct
{
size_t size;
int* arr;
} List;
List* newList (size_t size)
{
List *newone = malloc(sizeof(List));
newone->arr = calloc(size, sizeof(int));
newone->size = size;
return newone;
}
void setList(List* list, int index, int value)
{
if (index >= list->size)
{
list->arr = realloc(list->arr, index + 1 * sizeof(int));
list->size = index + 1;
}
list->arr[index] = value;
}
int main()
{
List *mok = newList(5);
itirate(i, 5) setList(mok, i, i);
itirate(i, 5) printf("%d\n", mok->arr[i]);
//setList(mok, 13, 9); this works
//printf("%d\n", mok->arr[13]);
for(int i = 5; i < 10; i++) setList(mok, i, i); // this does not somehow
for(int i = 5; i < 10; i++) printf("%d\n", mok->arr[i]);
return 0;
}
7
Upvotes
30
u/flyingron Sep 01 '26 •
* binds tighter than +. You want (index + 1)*sizeof (int).
17
u/tstanisl Sep 01 '26
index + 1 * sizeof(int)->(index + 1) * sizeof(int)?8
u/ReallyEvilRob Sep 01 '26
Try stepping through with a debugger.
7
u/sciencekm Sep 01 '26 edited Sep 01 '26
Someone has already mentioned that the problem is that the computation of the memory to be reallocated is wrong.
This could have been avoided by simply increasing the size before calling realloc.
From your code:
list->arr = realloc(list->arr, index + 1 * sizeof(int));list->size = index + 1;to this
list->size = index + 1;list->arr = realloc(list->arr, list->size * sizeof(int));4
u/SmokeMuch7356 Sep 01 '26
As others have pointed out, you're getting bit by precedence issues with
which should be
A few notes:
If
realloccannot satisfy the request, it will returnNULLwhile leaving the original allocated buffer in place. For this reason, you should assign the result to a temporary variable first and only update the original pointer when you know the operation succeeded. Otherwise you risk losing your only reference to that allocated buffer, leading to a memory leak.Similarly, you don't want to update your index until you know the
realloccall succeeded.You should keep track of both the total number of elements allocated as well as the last-used index.
Putting all that together, I'd extend your
Listtype asYour
newListfunction becomes:Create a separate
resizefunction:Then your
setListfunction becomes:You should also have a
destroyListfunction so you can clean up after yourself:1
u/jijijijim Sep 01 '26
Does the new list function even compile?
3
u/Adorable_Deal7 Sep 01 '26
Wrap macro's parameter around parenthesis to be sure your arguments are passed as you assume.
You can replace the malloc to the calloc. Need wrap the `index + 1` around parenthesis like math
-3
u/Desperate_Tie_648 Sep 01 '26
I am just curious why didn't you use chatgpt to get instant answer? Especially when problem so basic