/dpt/ - Daily Programming Thread

What are you working on, Any Forums?

Previous thread:

Attached: GOOD ENOUGH.jpg (908x505, 96.67K)

...

FUCK nene and FUCK nenequest
Shit programmer shit series shit game

C brainlet here. Why doesn't this work? I'm trying to add two linked lists node by node.

struct ListNode* makeNode(int val) {
struct ListNode* newNode;
newNode->val = val;
newNode->next = NULL;
return newNode;
}


struct ListNode* addTwoNumbers_r(struct ListNode* L1, struct ListNode* L2,
int carry, struct ListNode* head) {
// Add the values contained in the nodes.
int val = L1->val + L2->val + carry;
carry = val / 10;
val = val % 10;
L1->val = val;

// Disgusting logic to create new zero-valued nodes if either list
// comes to an early end.
if ((L1->next != NULL) && (L2->next != NULL)) {
printf("Both not null!\n");
return addTwoNumbers_r(L1->next, L2->next, carry, head);
} else if (L1->next == NULL && L2->next != NULL) {
printf("L1 null!\n");
L1->next = makeNode(0);
return addTwoNumbers_r(L1->next, L2->next, carry, head);
} else if (L1->next != NULL && L2->next == NULL) {
L2->next = makeNode(0);
return addTwoNumbers_r(L1->next, L2->next, carry, head);
} else if (L1->next == NULL && L2->next == NULL && carry != 0) {
printf("Both null but carry non-zero!\n");
L1->next = makeNode(0);
L2->next = makeNode(0);
return addTwoNumbers_r(L1->next, L2->next, carry, head);
} else {
printf("Returning!\n");
L1->next = NULL;
return head;
}
}

>struct ListNode* newNode;
> newNode->val = val;
> newNode->next = NULL;
> return newNode;
user? you arent allocating any memory...

>FUCK nene
Can do.

Attached: it is i who nuts to that.jpg (285x300, 15.36K)

>What are you working on, Any Forums?
optimizing what absolutely does not need optimizing

Attached: Screenshot_20220709_220637.png (557x398, 31.23K)

I suspected that was the issue, but I thought declaring a new local variable would allocate memory. Do I have to do a manual malloc?

>I thought declaring a new local variable would allocate memory
there's memory on the stack allocated *for the pointer*

struct ListNode makeNode(int val) {
struct ListNode newNode;
newNode.val = val;
newNode.next = NULL;
return newNode;
}

Attached: 1642101433457.jpg (782x1021, 80.73K)

What do you mean? Changing it a malloc worked, but honestly I haven't gotten to the part where I mess around with malloc and free yet, so I don't quite understand it.

I'm also not entirely comfortable with returning addresses/pointers from functions, but I'm getting there.

thats not what he wants either

How do you know they are a he?

No, my compiler warns me that this will return a local variable, so I assumed this was wrong too.

you have memory on the stack for local variables and function parameters
you need dynamic memory (i.e. memory that doesn't correspond to scope) which means you need malloc/free
the POINTER itself needs memory, which is available on the stack, but you need memory for the OBJECT you want it to point to

It will not warn you with this. You probably did something weird like return &newNode;

because they are posting in /dpt/

So will changing it like that fix it? I don't really know what to change L1->next = makeNode(0); to though. Doing something like &(makeNode(0)); doesn't work.

I think I understand, thanks.

({
useState : true
}) && [
args =>{
console.log("This is a basic hello world in Javascritp")
console.log("Donald Duck's three children are:")
args.map( x=> console.log(x) )
}
][main = 0](["Huey","Dewey","Louie"])

>So will changing it like that fix it?
No, not really, not with how you are using it anyway. Just use malloc.

Donald duck has no children.

thats also why you can't do because while you get a copy of the pointer and it still has memory, the object it points to is out of scope and in memory that got released
in order to use something like ListNode makeNode(int val) you would have to copy the ListNode into malloced memory, e.g.
ListNode* dynamic(ListNode node) {
ListNode* memory = malloc(sizeof(ListNode));
memcpy(memory, &node, sizeof(ListNode))
return memory;
}
dynamic(makeNode(3))
you are unlikely to use makeNode without wanting a pointer to dynamic memory so there isnt much point to doing it this way

those are Donald Duck's nephews. Donald Duck is the creepy uncle.

#define DYNALLOC(t, ...) dynalloc(sizeof (t), &(t){__VA_ARGS__})
void *dynalloc(size_t n, void *p) {
void *p2 = malloc(n);
return p2 ? memcpy(p2, p, n) : 0;
}
...
struct node *x = DYNALLOC(struct node, .val=val);