Jump to content

shadow_ray

Member
  • Posts

    398
  • Joined

Reputation Activity

  1. Agree
    shadow_ray reacted to igormp in Why can't strings have some pre-allocated bytes in C for storing the length?   
    You can create a struct and do that.
    Many other languages do have such abstractions over strings, such a Cpp.
  2. Agree
    shadow_ray reacted to Eigenvektor in Why are inbuilt C (and perhaps other languages as well) libraries have so complicated and SLOWER code?   
    My guess: Your code is less portable. The length of "int" in C is architecture/compiler dependent, i.e. it could be 8, 16, 32 or 64 bit. While a pointer is also architecture dependent, I assume it is always large enough to count the length of the longest possible string a pointer can point to.
     

     
    ~edit: This is a warning shown in VS Code. So I assume your code might fail for longer strings…
     
    When you micro-benchmark something you should also generally repeat it thousands or even millions of times, to rule out measuring errors from the CPU doing other things in between (modern operating systems are not single threaded, after all). So you don't know if your program's thread got parked in the middle of your loop. Plus, you're also measuring the speed of printf, since it's between start and end.
     
    You might also want to try that again with a much, much longer string. Yours might be faster for short strings, but maybe the other variant is actually faster for longer strings.
     
    Last but not least, you might want to look at the compiled code (i.e. the resulting assembler/machine code), to see which one actually has less instructions. Both for and while might very well translate to the same machine code.
     
    And, how do you know that the strlen implementation on your platform (I assume you're still on Windows?) matches the implementation used by the Linux kernel?
     
    ~edit #2
    gcc strlen_compare.c -o strlen_compare
    strlen elapsed: 0.000824
    mystrlen elapsed: 0.976289
     
    gcc -O1 strlen_compare.c -o strlen_compare
    strlen elapsed: 0.000457
    mystrlen elapsed: 0.000459
  3. Agree
    shadow_ray reacted to Eigenvektor in Having trouble in C... help... somebody.   
    Just want to add something that is not directly related to the question.
     
    Reallocating any time an element is added is bad for performance.
     
    The way most libraries do it is to initialize the array for a certain number of elements, then e.g. double it in size when it is filled to capacity.
     
    And since you expect your list to only contain a specific type of element you could also determine sizeof once and store it.
  4. Agree
    shadow_ray reacted to Sauron in C programming ethics - Enums   
    Don't blindly trust chatgpt. It doesn't know what it's talking about and any correct answer is coincidental.
     
    Enum members are always integers, the label is only for your convenience. If you don't specify what number each label represents the compiler will choose for you.
  5. Like
    shadow_ray reacted to trag1c in Memory leak goes away?   
    This will ultimately depend on your compiler. A compiler may choose to optimize much of your code away since you're only assigning to 'a' and not accessing, essentially see's it as a superfluous call that has no net effect on the outcome.
     
    GCC from -O0 to -O3 for optimization will keep the malloc call but using the same flags on clang show a much different story where the malloc disappears. See the assembly below and you can see. This with printf() commented out.
     
    GCC 13 with -O3
    main: push {r4, lr} movs r0, #0 bl time bl srand .L2: movs r0, #8 bl malloc # YOUR MALLOC CALL mov r4, r0 bl rand str r0, [r4] asrs r0, r0, #31 str r0, [r4, #4] b .L2  
    Clang with 17 with -O3 with no Malloc
    main:                                   # @main         push    rax         xor     edi, edi         call    time@PLT         mov     edi, eax         call    srand@PLT .LBB0_1:                                # =>This Inner Loop Header: Depth=1         call    rand@PLT         jmp     .LBB0_1  
    Even with the printf() call uncommented Clang still does not generate the call to malloc while GCC does. If optimization is turned off Clang will happily generate the malloc call as you would expect.
      main: # @main push rbp mov rbp, rsp sub rsp, 32 mov dword ptr [rbp - 4], 0 xor eax, eax mov edi, eax call time@PLT mov edi, eax call srand@PLT mov qword ptr [rbp - 16], 0 .LBB0_1: # =>This Inner Loop Header: Depth=1 cmp qword ptr [rbp - 16], -1 ja .LBB0_4 mov edi, 8 call malloc@PLT mov qword ptr [rbp - 24], rax call rand@PLT movsxd rcx, eax mov rax, qword ptr [rbp - 24] mov qword ptr [rax], rcx mov rax, qword ptr [rbp - 24] mov rsi, qword ptr [rax] lea rdi, [rip + .L.str] mov al, 0 call printf@PLT mov rax, qword ptr [rbp - 16] add rax, 1 mov qword ptr [rbp - 16], rax jmp .LBB0_1 .LBB0_4: mov eax, dword ptr [rbp - 4] add rsp, 32 pop rbp ret .L.str: .asciz "%p\n"  
     
  6. Like
    shadow_ray got a reaction from RetroSmoo in Programmers Lounge   
    Performance of different programming languages often times comes up as a question on this forum so i think i should mention this video. A bunch of people spontaneously created a competition to beat a youtuber's code. One guy speed it up 40,832,277,770%, reducing the runtime from a month to 7ms. 😄
     
  7. Like
    shadow_ray reacted to C2dan88 in ofstream by default inputting a newline character when writing data to a file in cpp   
    To append data written to the file with ofstream set the mode to append mode using std::base_io::app
    ofstream outputfile(filename, std::ios_base::app); // <-- ofstream append mode  
     
    Tip when posting code in your post press the angle bracket button in post editor.

  8. Agree
  9. Like
  10. Agree
    shadow_ray reacted to Eigenvektor in Whats wrong with my code? I wanna get the summation of the array , C++   
    Please, post code as text in a code block… makes it much easier for people to try your code. If you're asking what the problem is, it's also usually a good idea to tell people if it just doesn't behave as expected (and what's happening instead) or if you're getting a compiler error (which one?)
     
    At a quick glance, your array contains doubles, but your sum total is an int. Either change your sum to a double, or round your values to int before trying to add them.
     
    ~edit: Here's a working example
     
  11. Agree
    shadow_ray reacted to Polderviking in Thoughts on Protonmail?   
    Probably referring to this.

    I don't think that can be called a leak. Even in Switzerland there are rules they need to follow.
    And they've been reasonably transparant about this.
     
    Some people have the wrong idea and turned it into a whole thing.
    They can't just go ahead and ignore law like some disguntled anachist. That would be how you get your infrastructure confiscated.
  12. Like
    shadow_ray reacted to Polderviking in Thoughts on Protonmail?   
    I've been using them for years.
     
    I don't even REALLY care all that much about things like mailbox encryption as most mail traffic I generate still inevitably traverses the big giant's mail servers and can therefore be perused.
    I just like their services generally and their privacy first angle.
    They also cater to power users, my data is in Europe and they are a nice full package experience alternative to the big name providers at a reasonable price. Especially with with the recently introduced password manager it's become great value. 
  13. Agree
    shadow_ray reacted to Agall in How to turn my skill into money/ a business?   
    Talk to your boss, a company will let you be content with less pay and usually the only person who will help argue that you should be paid more is yourself. I'm in the US, so labor laws and such are different, but regardless, if you believe you should be paid more, find a solid set of evidence why, and bring it to your boss.
     
    It costs a lot to retrain people, especially with the fact that people will go through complete training programs then just leave. It costs the company a lot to do that, so they'd rather retain good workers by paying them more than having to gamble on another human. I started off in the military and am now a civilian and it still amazing me how short people will work at a place before just leaving, like they didn't think about it before hand. 
  14. Like
    shadow_ray reacted to cmndr in How to turn my skill into money/ a business?   
    Full warning - I'm from the US. YMMV if your country is different. 
     
    Doing independent consulting is probably more "people heavy" than working in a technical role. 

    Your higher ups mostly take care of the politics for you. If you're a sole proprietor, you're doing marketing and politics yourself. 

    DO get good at interviewing though. This is what I suggest

    1. Adapt the advice here to your situation - https://mergersandinquisitions.com/free-investment-banking-resume-template/
    2. Get a set of good eyes to critique the heck out of your resume and make specific suggestions for rewrites (tip, it'll often be "be more specific and why does this matter?")
    3. For each bullet, on a separate sheet for your own preparation, write a paragraph outlining who/what/where/why/when/how/how-much/lessons-learned. Getting review on these is also useful (be SPECIFIC, be concise)
    4. Amazon leadership principles sample questions are good for behavioral questions (find sample questions). Leetcode/hacker rank is good for technicals. 
  15. Agree
    shadow_ray reacted to cmndr in How to turn my skill into money/ a business?   
    Just get a better ordinary job. 

    I knew someone in high school that was, at age 16, making around 3x minimum wage by coding. High school kid. 

    If you're worried about knowing what to do, learn some CI/CD stuff, learn how to do code check ins and learn how to write clean, readable code (look up PEP8 or similar). 
    https://peps.python.org/pep-0008/
     
    You can always hustle for other gigs later on. Get actual experience working with other people and learn from them, even if it's only for 1-3 years. 
     
    ----

    For what it's worth I met a guy who worked at Google who didn't have a college degree. He was definitely insecure about it. Most people had bachelors and masters degrees from elite universities. 
  16. Agree
    shadow_ray reacted to Neftex in Help with Boolean values and variables JS   
    function toggleRandom() { useRandom = !useRandom } youre not using the return value of the function anyway, right? so the function could just assign the negated value to the variable?
  17. Agree
    shadow_ray reacted to Radium_Angel in Can you give me rough estimate what it would cost to make this program?   
    If you are making the program, it shouldn't cost you anything. If you are hiring a programmer, then it's gonna depends on where they are in the world as to what they charge. You can hire an Indian programmer for something like this for probably as little as 100$ US. I suspect you could get this done in Excel or possibly Access, it seems to just be a database of nutritional (numbers) info.
  18. Like
    shadow_ray reacted to Paul Rudd in Metro: Last Light Complete Edition FREE on Steam May 18th   
    To celebrate its 10th Anniversary...
     
    Metro Last Light Complete Edition is FREE right now on Steam. 94% Very Positive reviews.

    Free to keep when you get it before May 25 @ 1:00pm.
     
    Metro Last Light first released 10 years ago on May 14th, 2013.
  19. Agree
    shadow_ray reacted to Eigenvektor in Callout to all Programmers   
    We can do that already using our CI/CD pipeline. Which has the advantage that we control every step of the process, the process itself is checked into our repository and we don't need to give root access to our cloud instances to a third party.
  20. Agree
    shadow_ray reacted to porina in Is there any limit to how faster the code executes the lower level you get?   
    Taking a high level view, even if you have perfectly optimised software, your performance will be limited by hardware. You need to know what the specific hardware can do, and make best use of it. You're not making general software at that point. How close do you want to get?
     
    Hybrid approaches work to get the most performance with reasonable effort. An example is Prime95. The bulk of it is written in some derivative of C, but the performance critical parts of it are written in assembler. There are multiple code paths optimised for different CPU architectures and instruction sets.
     
    There comes a point if performance is that important, you might consider not optimising the software but the hardware. Silicon designed to do a specific task can perform much faster than general uses.
  21. Agree
    shadow_ray reacted to Benji_w in How do I learn programming?   
    Try to understand what it is you're copying, break down the code into bitesize peices that make sense and then work out why its working.
  22. Agree
    shadow_ray reacted to sub68 in This Makes Hacking TOO Easy - Flipper Zero   
    Yes, Reminds me of Deviant ollam.
     
    Problem everything in the flipper zero can be easy bought separate.
    Reminds me of a LPL of simplisafe that can be bypass with HAM radio gear.
    the radio is a baofeng uv5r I own one to have for when I become licensed.
    The thing is everyone uses these radios I have seen them everywhere from bad actors to good ones.
    Yes its technically illegal to use by FCC but everyones still uses it.
  23. Funny
    shadow_ray reacted to Dedayog in Battlestar Galactica Deadlock FREE on Steam April 6th   
    Nice!  Thanks!  Was looking at that a few years ago and totally forgot about it.  Excellent price 🙂
     
  24. Agree
    shadow_ray reacted to Eigenvektor in Best way to automatically update docker container to latest image version from a Github action?   
    I would use docker compose to run the service, then all it takes to update is to do
    docker compose pull docker compose up -d in the directories that contain a docker-compose.yml. (Note: If you have an older version of docker, docker-compose is/was a script, now it's built in). This should be easy enough to automate through SSH.
  25. Funny
×