attn cpp coders

Hello Jose, I am not a Python programmer and i have yet to read anything about this language. I'm assuming it is Python and i get a headache looking at this code.
Yes, it's Python and it gave me quite a headache.
a class to check attributes of a database and its schemas?
checking an attribute string via slice starting at index 9?
containing a boolean testing the object is callable?
containing a boolean testing if the object is iterable?
error handling is self-explanatory
It generates methods with names like query_by_table_foo(...) at run time. I was pulling my hair out trying to figure out where they were defined. I could see they were getting called, and returning results, but I couldn't find them anywhere!
 
Most of the time, just use std::vector. It beats everything else hands down most of the time. Quite often you can use std::vector instead of any of the map/set containers (typically when you have an integral index that is contiguous). std::flat_map and std::flat_set are also good choices.

You need to take care of std::vector insertion resulting in iterator invalidation.

When the key is either non-integral or there are large holes in the range then you will need the map/set containers.

You will rarely or never need to use deque, stack, queue or priority queue. The performance of std::list is so bad that you will only ever need it if you are splicing many large objects into the middle or beginning of the container.

I haven't played with it yet, but C++26 added std::hive which should have most of the benefits of std::list (stable iterators and lower cost insertion) without the large memory overhead, spending vast amounts of time in new/delete and chronic memory fragmentation.
Hello Paul, Thank you for taking time out of your day to reply and help a beginner. I appreciate this advice very much. I have read about std::vector and i really like this concept. I want to play with it so that i can begin to understand all of the ways in which i can utilize it in my code creation process.

regarding queue, i was reading a few documents about the A star Algorithm and the pseudocode uses a queue, which is not present in PHP. I had to use arrays and a tricky loop to recreate the queue pseudocode. I am happy to know that C++ has queue available for just such algorithms. Thank you for reminding me about queue :)
 
Yes, it's Python and it gave me quite a headache.

It generates methods with names like query_by_table_foo(...) at run time. I was pulling my hair out trying to figure out where they were defined. I could see they were getting called, and returning results, but I couldn't find them anywhere!
Good morning Jose, i also get a headache looking at this code. I have made a few posts about my dislike for Python. I find the language to be weird. You mentioned Java before and i agree that it is a better language. I'm not trying to take anything away from the success of Python. I am aware that it is being used even in the Robotics field but i argue that C, C++ and Java could also be used and they would be better choices in my opinion.

well i am glad that you figured out wht the code is doing. I would have to setup some dummy code to work with the script to see exactly what it is doing. I cannot see the context of usage. Atleast you are a good programmer and you figured it out. You should get PHP (Python Headache Pay) or extra vacation days for that task :)

my example of bad PHP is just a function named calculate_center. I am in development phase and i often make short variables just to get code working (i hate typing. I am not a typist for sure). I still use comments in development code so that i do not forget what the code is doing. When i finish the dev phase, i clean it up so that the code becomes self-explanatory (good comments, good variable names and formatting which adheres to a standard). Oh! the code is part of a dynamic (user based input for m and n dimensions) grid graph. However, i use the space created by vertices instead of the vertices. I call the space a tile. I'm not a mathematician but i believe that matrix math only works off of the vertices, i changed the logic to fit my code but to still be able to calculate the grid graph. I also use a star algorithm to find a path from start to finish within the graph. I wanted to know which tile/tiles are at the center of the grid graph. Anyone can use math to calculate central point of the graph but i need to calculate the center/central tile/-s. So variables p=parity, mt=medial tiles, d=direction, t=tile
 
johnjohn You could throw half those books away and not be worse for it. Don't get bogged down by them. You've collected too many trees for your forest.
probably true for most people but i am a self-taught coder. I never had the privilege of textbooks, classmates and a asking a professor questions. Alot of programmers overlook the fact that newbies often have limited knowledge about computers. Whenever i started studying coding, i had to look up hex and binary because i had no idea what it meant. I like to take the 'storm the palace' approach and fill/train my brain with as much material as possible using beginner-to-advanced info. I learned how to code in PHP with this tactic. I learn differently or my brain is not naturally as sharp as yours :)
 
Was Hemingway a typist or an author? I'm a programmer, not a "coder". A coder comes from the secretarial pool.
I am not sure if i am interpreting this post correctly. I hope that you are not offended in anyway. I call myself a coder, i certainly do not refer to you as a coder. I do not think that i deserve to label myself as a programmer just yet. I am self-taught and i have alot to learn. I am not in the same league as you. I would not walk into a room full of programmers and call myself a programmer. I remember a crude expression once spoken to me: "know your role and shut your mouth". I try not to place my skill level in the same category as pros. In my opinion, too many people step out-of-line these days. Do you know what i mean? I'm not trying to be rude. I just notice how alot of people read something on-line then speak about it as if they are experts. Reading an article or book about acting does not qualify one to criticize or otherwise speak of the profession as if one is a part of the profession. I certainly would not tell you how to program. My post is asking for help regarding programming.

I'm the sparrow in the tree full of Eagles. ;)
 
do you plan on entering pro wrestling?
He cracked his knuckles like they were about to meet an opponent, not a keyboard.

“Alright,” he muttered, staring down the glowing screen, “let’s step into the ring.”

The bug had been waiting for him all morning. Nasty one. Slippery. The kind that ducks under your punches and makes you doubt your own footing. But he’d seen its type before.

He rolled his chair in closer.

“First round—scouting the opponent.”

He circled the codebase like a wrestler pacing the mat, eyes sharp, breathing steady. Logs? That was tape review. Stack traces? Footage of past matches. Every line of output was a clue to how this thing moved, where it slipped, where it struck.

“There you are,” he whispered.

A null pointer, hiding in a corner case. Cheap shot.

He smirked.

“Going for the legs early, huh?”

He stretched his fingers and dove in.

“Lock-up.”

His hands flew across the keyboard—quick jabs of print statements, probes into state, assertions snapping into place like holds. He didn’t just read the code; he grappled with it. Twisted it. Forced it to reveal where it broke.

“Test suite—get in here. I need backup.”

He unleashed the tests like a tag team. They rushed the ring, hammering the system from every angle. Most passed clean. But one—one failed hard.

“There it is! That’s the weak spot!”

He leaned in, eyes burning now.

“Time to apply pressure.”

Refactor. He loved that part. That was technique. Craft. Not just brute force—this was knowing how to move your opponent’s weight against them.

He pulled functions apart, split responsibilities, tightened logic like a chokehold.

“Too much responsibility here—no wonder you’re collapsing. Let’s break you up.”

One class became two. Then three. Each leaner. Meaner. Focused.

“Now breathe. Now fight.”

He ran the tests again.

Green.

All of them.

He leaned back, exhaling like he’d just pinned a heavyweight.

“One… two… three.”

He raised his arms—well, metaphorically. The room was quiet, but in his head the crowd was roaring.

But he wasn’t done.

“No champion keeps the belt without defending it.”

He opened a new file.

“Documentation.”

His voice dropped, serious now. Respectful.

“This is the promo. This is where I tell the world how the match went down.”

He wrote clean, precise comments. Not fluff—truth. Why the fix worked. Where the danger had been. What to watch for next time.

“Leave a trail,” he said. “So the next fighter doesn’t walk in blind.”

Finally, he pushed the changes.

“Commit.”

He paused, fingers hovering.

“Message matters. Always does.”

> Fix null pointer in edge-case input handling; refactor parser for clarity and stability.

He nodded.

“Clean finish.”

Push.

The code was live now. The match was over.

He stood up, stretching like he’d just walked out of a steel cage.

“Another day,” he said, grabbing his coffee, “another title defended.”

And somewhere, deep in the system, another bug was already warming up—waiting for its turn in the ring.
 
Arguments for C/Cpp (and not other languages).

1. Compiler in base (no dependencies).
2. Plethora of examples in the wild.

You can do some euler problems in probably a dozen different languages and learn a few algorithms which may serve you better in whatever language you end up in next (vs "syntax").
 
He cracked his knuckles like they were about to meet an opponent, not a keyboard.

“Alright,” he muttered, staring down the glowing screen, “let’s step into the ring.”

The bug had been waiting for him all morning. Nasty one. Slippery. The kind that ducks under your punches and makes you doubt your own footing. But he’d seen its type before.

He rolled his chair in closer.

“First round—scouting the opponent.”

He circled the codebase like a wrestler pacing the mat, eyes sharp, breathing steady. Logs? That was tape review. Stack traces? Footage of past matches. Every line of output was a clue to how this thing moved, where it slipped, where it struck.

“There you are,” he whispered.

A null pointer, hiding in a corner case. Cheap shot.

He smirked.

“Going for the legs early, huh?”

He stretched his fingers and dove in.

“Lock-up.”

His hands flew across the keyboard—quick jabs of print statements, probes into state, assertions snapping into place like holds. He didn’t just read the code; he grappled with it. Twisted it. Forced it to reveal where it broke.

“Test suite—get in here. I need backup.”

He unleashed the tests like a tag team. They rushed the ring, hammering the system from every angle. Most passed clean. But one—one failed hard.

“There it is! That’s the weak spot!”

He leaned in, eyes burning now.

“Time to apply pressure.”

Refactor. He loved that part. That was technique. Craft. Not just brute force—this was knowing how to move your opponent’s weight against them.

He pulled functions apart, split responsibilities, tightened logic like a chokehold.

“Too much responsibility here—no wonder you’re collapsing. Let’s break you up.”

One class became two. Then three. Each leaner. Meaner. Focused.

“Now breathe. Now fight.”

He ran the tests again.

Green.

All of them.

He leaned back, exhaling like he’d just pinned a heavyweight.

“One… two… three.”

He raised his arms—well, metaphorically. The room was quiet, but in his head the crowd was roaring.

But he wasn’t done.

“No champion keeps the belt without defending it.”

He opened a new file.

“Documentation.”

His voice dropped, serious now. Respectful.

“This is the promo. This is where I tell the world how the match went down.”

He wrote clean, precise comments. Not fluff—truth. Why the fix worked. Where the danger had been. What to watch for next time.

“Leave a trail,” he said. “So the next fighter doesn’t walk in blind.”

Finally, he pushed the changes.

“Commit.”

He paused, fingers hovering.

“Message matters. Always does.”

> Fix null pointer in edge-case input handling; refactor parser for clarity and stability.

He nodded.

“Clean finish.”

Push.

The code was live now. The match was over.

He stood up, stretching like he’d just walked out of a steel cage.

“Another day,” he said, grabbing his coffee, “another title defended.”

And somewhere, deep in the system, another bug was already warming up—waiting for its turn in the ring.

I've just read a review of the match posted by a noobile reporter named Celia, a.k.a CiCi. She gave the match a final grade of C++ - meaning that she approves of the Champion. C is her favorite letter.

You have quite an imagination eternal_noob I have read every word and i've enjoyed your sense of humour. I'm looking forward to the next opponent: Age Gent (hails from California, weight 1043).

I have to get some sleep now. The time is after 8pm here in Berlin. I must be awake at 4:30am
Goodnight and good luck with the next bug :)
 
so i have been reading about C instead of C++ because i think that it will be a better introduction to C++

for the sake of brevity, i've tried to recreate my calculate center function in C from PHP. The hardest part of this code was the scanf section because i couldn't figure out how to validate and clear the buffer. I did some research and came up with some code that seems to work. Perhaps i can get some reviews of my first C program (actually, i remember reading about C last year and i made a clock program in FreeBSD using cc). I am tired today. I will soon start playing around with the suggestions made in this thread regarding dynamic data. However, i have alot to learn in order to become useful in C/C++

so here is my gg.c file (grid graph):
Code:
//cc -lm -o estat ~/gg.c
//FreeBSD requires -lm to use floor
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int m, n, v, m_parity, n_parity;

void calculate_center(int m, int n, int v, int mp, int np) {
  int half = floor((v + 1) / 2);
  int halves = half + 1;
  int tile1 = half - floor(half / m);
  int tile2 = tile1 + 1;
  int tile3 = tile1 + n;
  int tile4 = tile3 + 1;
  printf("Medial tiles\n");
  switch (np) {
    case 0:
      if (mp) { printf("id: %d, %d\nposition: vertical\n", tile1, tile3); break;}
      printf("id: %d\nposition: center\n", half); break;
    case 1:
      if (mp) { printf("id: %d, %d, %d, %d\nposition: stack\n", tile1, tile2, tile3, tile4); break; }
      printf("id: %d, %d\nposition: horizontal\n", half, halves); break;
  }
  return;
}

int main(void) {

  printf("Grid Graph Dimensions (range 3-to-64)\n");
  printf("rows m: ");
  while(1) {
    int input_status = scanf("%d", &m);
    if (input_status == 1) {
      if (m > 2 && m < 65) { break; }
    }
    while(getchar() != '\n');
  }
  printf("columns n: ");
  while(1) {
    int input_status = scanf("%d", &n);
    if (input_status == 1) {
      if (n > 2 && n < 65) { break; }
    }
    while(getchar() != '\n');
  }

  v = m * n;
  m_parity = m %2 ? 0: 1;
  n_parity = n %2 ? 0: 1;
 
  printf("m parity: %d\nn parity: %d\n", m_parity, n_parity);
  printf("\n");
  calculate_center(m, n, v, m_parity, n_parity);
 
  //to view exit status from sh/bourne shell: echo $?
  return 0;
}

i compile it using cc and it runs without errors in FreeBSD (~./gg). I really respect the C/C++ programmers. Good Lord this language is frustrtatingly different than PHP. I doff my hat you ladies and gentleman. I have a deeper appreciation for C/C++ Programmers today :)
 
Effective Modern C++ (Scott Meyers)

Yes - Scott Meyers will lead you down the "path of light" to learning the Force....

Effective C++: 55 Specific Ways to Improve Your Programs and Designs - Scott Meyers
More Effective C++: 35 New Ways to Improve Your Programs and Designs - Scott Meyers

A lot of programmers try to learn "C++" without learning basic "C" programming because they don't want to study the (Ancient Jedi Texts). But to truly understand C++ you need to learn C as well. When you master both C and C++ you will become a (Great C++) programmer. Learning "C" will also help you make sense of all of the man(1) pages which reference "C" system calls in FreeBSD, *BSD, UNIX, Linux, Microsoft, etc.

I have seen a lot of programmers trying to learn the "new C++" and skip the "old C" -- and they later fall over trying to do programming that would otherwise have been pretty easy :-)...

When I was first learning C++ we used to call a program called (Link - Wikipedia) - Cfront which would translate our C++ code into native "C", which would then be compiled to machine code. So don't believe all the (Link Wikipedia) - FUD: Fear, uncertainty, and doubt - that "C" has some how been/or is replaced by the new and better C++ ... that is a path to the Dark Side.

Pretty much all of the modern programming languages trace their roots in some way back to "C".

Good luck on your path !
 
It really, really helps if one understands how the processor accesses memory and IO. At least for me it does. When I program, I sometimes visualize how the data travels back and forth across the motherboard and into everything but that's probably cause I started life as an electronic engineer.
 
CShell Thank you for the words of encouragement. I like the StarWars reference :) I did not know about CFront. I enjoy learning about the history of C/C++ Thank you for taking time out of your day to share your knowledge with us.

drhowarddrfine yes, indeed! memory and io is something that i need to fully understand in order to be useful with C/C++ Good advice for anyone reading this thread. Thank you, Sir.

I have been playing with the grid graph code a bit today to see what i can do with the C language. I tried to make adjustments to my code and learn new concepts. I do not know if i am correctly implementing concepts or not, so please advise where applicable. I also tried exploring system commands but that is a bit ahead of my current level of understanding. So here is my new gg.c file (gg2.c):

Code:
//cc -lm -o gg ~/gg2.c
/******************************************************************
    FreeBSD requires -lm to use floor
    otherwise an undefined symbol error occurs at compile time
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

unsigned int m, n, v, m_parity, n_parity;

/******************************************************************
    Function Junction
*******************************************************************/

void clear_screen() {
  int system(const char *command);
  system("clear");
  return;
}

unsigned int cli_prompts(unsigned int stage, unsigned int type) {
  unsigned int passes = 0; //loop safety control

  switch (stage) {
    case 0: //get the dimensions of the grid graph
      clear_screen();
      printf("Grid Graph Dimensions (range 3-to-64)\n");
      printf("m: %u, n: %u\n", m, n);
      printf("%s: ", type == 0 ? "Enter number of rows (m)": "Enter number of columns (n)");
      while(1) {
          if (passes > 2) {
            clear_screen();
            printf("gg: Too many attempts without a valid response.\nGoodbye.\n");
            break;
          }
          int input_status = scanf("%u", &type);
          if (input_status == 1) {
              if (type > 2 && type < 65) { break; }
          }
          while(getchar() != '\n');
          type = 0; passes++;
       }
    break;
  }
  return type;
}

void calculate_center() {
  unsigned int half = floor((v + 1) / 2);
  unsigned int halves = half + 1;
  unsigned int tile1 = half - floor(half / m);
  unsigned int tile2 = tile1 + 1;
  unsigned int tile3 = tile1 + n;
  unsigned int tile4 = tile3 + 1;
  
  printf("Medial tiles\n");
  switch (n_parity) {
    case 0:
      if (m_parity) {
        printf("id: %u, %u\nposition: vertical\n", tile1, tile3);
        break;
      }
      printf("id: %u\nposition: center\n", half);
      break;
    case 1:
      if (m_parity) {
        printf("id: %u, %u, %u, %u\nposition: stack\n", tile1, tile2, tile3, tile4);  
        break;
      }
      printf("id: %u, %u\nposition: horizontal\n", half, halves);
      break;
  }
  return;
}


/******************************************************************
    Main Street
*******************************************************************/

int main(void) {

  m = cli_prompts(0, 0); if (!m) { return 1; }
  n = cli_prompts(0, 1); if (!n) { return 1; }
  
  v = m * n; //number of vertices
  m_parity = m %2 ? 0: 1;
  n_parity = n %2 ? 0: 1;

  clear_screen();
  printf("m: %u, n: %u, v: %u\n", m, n, v);
  printf("m parity: %d\nn parity: %d\n", m_parity, n_parity);
  printf("\n");
  calculate_center();
  
  //to test exit status from sh/bourne shell: echo $?
  return 0;
}
}
 
I always struggled trying to figure out C++ and I once considered myself an expert at C. To me it's significantly different.
Ironically, that realisation alone makes you quite a good C++ programmer in comparison!

Part of the reason why C++ has such a bad name for itself in terms of safety (and everything else) is because people treated it too much like C in the early days. Nothing was exception safe, memory leaks, random hidden overhead.

I agree that C++ developers need to know C but mainly because C rules the entire computing platform and you will almost certainly be interfacing with C. But the architecture / design of C programs and libraries should remain wholly different to C++. That part isn't really transferrable.
 
Ironically, that realisation alone makes you quite a good C++ programmer in comparison!

Ya - there is "a completely stupid" push going on right now to stop teaching new programmers how to manage computer memory correctly. So the programmers (ALSO) don't learn how to use valgrind(1) or any other memory checking tool on their finished source code/binary because: "I don't need to do that ! The software is guaranteed to be memory safe! "

I can't tell you how many times I was fixing "memory leaks" in Java code -- but I was always told "It's Java! It can't leak memory !".

Tell that to "Yourself" while you are on site working a major "production outage" during "Your Christmas/New Years" vacation, fixing those "impossible memory leaks" that you COULD HAVE identified and caught/fixed many months or years ago. Have fun with that.

They need to (restart) teaching memory management techniques in school again.
 
CShell I've installed valgrind in FreeBSD. Thanks for the tip! I also need to learn memory management. I am searching for references and books about the subject. Also, i changed/tweaked my code and edited my last post. I failed to realize that my variables are in global scope and i did not need to pass them as parameters (stupid of me but i am just beginning).

I know that i am new to C/C++ but i think that all programmers should take the advice of learning memory management. It may be horrible to have to think about memory to some programmers but it is just as important as the program being created. I see alot of people complaining about C/C++ memory issues but it seems like maybe memory management is not being learned or taught these days. Do we really blame the keyboard for typos now? I code PHP and i have the luxury of not worrying about memory but i understand that learning C or C++ means that i actually have to learn how to program and how to program correctly. I will not blame C or C++ for my shortcomings or mistakes.
 
CShell I've installed valgrind in FreeBSD. Thanks for the tip! I also need to learn memory management. I am searching for references and books about the subject. Also, i changed/tweaked my code and edited my last post. I failed to realize that my variables are in global scope and i did not need to pass them as parameters (stupid of me but i am just beginning).

johnjohn - My comment wasn't directed at you specifically.

And Yes! You need to learn memory management. C and C++ both can leak memory, so it's something that all programmers should learn and should handle in their code.

For Valgrind take a look at the "--leak-check" argument to the command -- when you run your program with Valgrind, Valgrind will tell you if your program leaked memory and (where) your program leaked memory. Valgrind has other tools that will tell you if you corrupted your STACK while your program was run, so it can find a lot of software coding probems for you. You can read more about Valgrind here: (Link Valgrind) - https://valgrind.org/

You should also start reading and learning about the gdb(1) command. If you make a coding mistake and your program "dumps a core file" you can use gdb to examine the core file and see what the mistake or issue is. There is a GNU book you can buy which explains how to use gdb(1) if you want to read up on it.

In C language you need to learn about FreeBSD malloc(3), realloc(3), strdup(3) and others that return pointers to allocated memory that you then must remember to call free(3) on. You can view the FreeBSD man(1) pages on each of the (above).

in C++ -- if you call "new" to create something (like a "Class Object", raw pointer or similar) -- you will need to also call "delete" to later free the memory used by that resource.

If you make mistakes with memory management -- well you are learning -- WHICH WAS MY POINT :-). I EXPECT YOU TO MAKE MISTAKES... it is how we learn.. :) . If we put our hands into a hot burning fire and they (DO NOT BURN), then we just keep putting our hands into every fire we see :cool:. More programmers need to "try things" so they learn more.

Keep up the excellent work ! If you want to improve with C/C++ you have A LOT of runway in front of you.
 
CShell I downloaded valgrind (pkg install valgrind) just after reading your post, then i looked at the website. I downloaded the pdf manual and looked at the usage section. I ran valgrind with the --leak-check=full option. Very nice tool. Thanks again for mentioning it.

gdb(1) is a helpful tip too. I am going to relax and read about memory management, gdb and more about valgrind usage before i go to bed. Sleep allows my brain to better absorb the info, so this is good timing :)

I am currently just playing/tinkering with basic concepts to get used to C and the C programming ideology. You know the old expression "thinking in...", well i need to start Thinking in C now instead of PHP. I will try working with memory as soon as i read enough material to do something useful with the concept. I have a book somewhere about writing Unix drivers and i want to read this book as well. I will check local bookstores for the GNU book about how to use gdb(1). Sometimes i have to wait until a book is printed (2-4 weeks) by local publishers but sometimes it is available via Amazon.

I hope to become a useful and well-trained C Programmer in the future but, for now, i am a beginner. I'll take my time and learn the language correctly. I am ignoring everything i have learned in PHP. I'm trying to erase my memory and start from scratch. I want to be successful with C/C++
 
Back
Top