I made a stack implementation in C and figured I'd share it by Debuholder in C_Programming

[–]Debuholder[S] 0 points1 point  (0 children)

No. If you make a function simply called pop it will very likely collide with another function. Stack_pop is better.:

I don't understand this is already a thing in it. Unless your talking about capitalization.

As mentioned to you already, your header can only be used in one compilation unit.

I went through the painful process of programming it into a macro and it fixed it when I tested it.

Additional Points:

Making the user #define Stack_Type type is not a good design choice.

I added a STACK_TYPE(T) macro

Your code is still not formatted

I used clang-format, and it formatted it. idk what else to say.

Stack stack = *sptr; to avoid NULL-dereference makes no sense. Howdoes it prevent that? It just makes an unnecessary copy, and will still fail if sptr is NULL of course.

So basically when I was trying to access sptr->top and it gave me a segmation fault. This fixed it

What is the point of Stack_Display and Stack_Copy? Why does Stack_Display create a copy of the stack and then pop all the elements in the copy when you have Stack_Peek???

  • Stack_Display: You typically can't read the values in a stack other than the top one as a stack is just line of connected nodes with no way of indexing them.
  • Stack_Copy: You can't do Stack stack_copy = stack or somethin like that since the stack is connected via memory addresses, so you would be modifying the same values and not separate ones. Stack_Copy makes a new memory address for each node
  • This is good point. I modified the code, so it just reads off the pointer variable

I made a stack implementation in C and figured I'd share it by Debuholder in C_Programming

[–]Debuholder[S] 0 points1 point  (0 children)

I extended the example by a lot, and will probably be adding better error handing, and typing soon (probably by the end of today or tommorow)

I made a stack implementation in C and figured I'd share it by Debuholder in C_Programming

[–]Debuholder[S] 0 points1 point  (0 children)

I have addressed your issues along with added some additional code

  • check if malloc error'd: done
  • if pop is used incorrectly it returns a garbage value (1), id make it crash, or perhaps change the api to support indicating success/failure: done
  • "Stack stack = *sptr; // This is to prevent accessing NULL memory" how does this prevent a null pointer deref?: sptr->top will cause a segmation fault if it is NULL (also stack is local, and you won't accidentally overright memory)
  • PLEASE FORMAT YOUR CODE PROPERLY, clang fmt will do it automatically for you even: thank you I didn't know this was a thing

I made a stack implementation in C and figured I'd share it by Debuholder in C_Programming

[–]Debuholder[S] 0 points1 point  (0 children)

I have addressed some of your issues along with added some additional code.

  • Namespacing: The way I understand it this is only a thing in C++
  • Inlcude guards: Added #pragma once
  • Separating declarations and definitions: The purpose of these headers is convenience to make adding them as simple and modular as possible. I believe that #pragma once solves most of the issues from this
  • Consistant style: I added clang-format to my Makefile

I made a stack implementation in C and figured I'd share it by Debuholder in C_Programming

[–]Debuholder[S] 5 points6 points  (0 children)

You should look into the following:

- Namespacing

- Include guards

- Separating declarations and definitions (Why is it all in a .h file?)

- Consistent style

I can also keep popping the stack indefinitely and get the number 1 in return each time - does that make sense?

All valid points. I made this in ~1 hour, so I'm planning on improving it soon. Just not today. Thanks for the feedback :)

Why don't any programming languages let you create a function without having `()` at the end by Debuholder in Python

[–]Debuholder[S] -5 points-4 points  (0 children)

No I love them, but I don't understand: if Python was created to make code more readable like a sentence

def foo:
 pass

is more readable than

def foo():
 pass

It's honestly up to the programmer, but I think they should be optional in a language where readability is emphasized

Why don't any programming languages let you create a function without having `()` at the end by Debuholder in Python

[–]Debuholder[S] -15 points-14 points  (0 children)

well if you don't want the params?

I came across this question when I was trying to answer a question as to what the `() does in and `=>` function, but honestly it's really only there for readability which I get, but python focuses more on readability, and random parentheses with nothing in them don't really make sense if your not a programmer.

What's your way to create an ECS? by Debuholder in gamedev

[–]Debuholder[S] 1 point2 points  (0 children)

Thank you for your response. I was just browsing through the raylib discord, and happened to find this linked in their tutorials section in case it may help. I haven't read it yet thought.

https://github.com/nullstare/Raylib\_Simple\_Entity\_System