Open-source News

My open source journey with C from a neurodiverse perspective

opensource.com - Tue, 05/10/2022 - 15:00
My open source journey with C from a neurodiverse perspective Rikard Grossma… Tue, 05/10/2022 - 03:00 Register or Login to like Register or Login to like

I was born in 1982, which in human years is only 40 years in the past (at the time of writing). In terms of computer development, it's eons ago. I got my first computer, a Commodore 64, when I was ten years old. Later, I got an Amiga, and by 13 I got an "IBM Compatible" (that's what they were called, then) PC.

In high school, I did a lot basic programming on my graphing calculator. In my second year of high school, I learned basic C programming, and in my third year I started doing more advanced C programming, using libraries, pointers, and graphics.

My journey from programming student to teacher

In my college days, I learned Java and so Java became my primary language. I also made some C# programs for a device known as a personal data assistant (PDA), which were pre-cursors to the modern smart phone. Because Java is object-oriented, multi-platform, and made GUI programming easy, I thought I'd do most of my programming in Java from now on.

In college, I also discovered that I had a talent for teaching, so I helped others with programming, and they helped me with math when I took computer science. I took some courses on C programming, aimed at basic embedded programming and controlling measurement instruments in my later college years.

After turning 30, I've used C as a teaching tool for high school kids learning to program in C. I've also used Fritzing to teach high school kids how to program an Arduino. My interest in C programming was awakened again last year, when I got a job helping college students with learning differences in computing subjects.

How I approach programming in C and other languages

All people learn differently. Being a neurodiverse person with Asperger's and ADHD, my learning process is sometimes quite different from others. Of course, everyone has different learning styles, though people who are neurodiverse might have a greater preference for a certain learning style than someone else.

I tend to think in both pictures and words. Personally I need to decode things step by step, and understand them, step by step. This makes C a suitable language for my learning style. When I learn code, I gradually incorporate the code into my mind by learning to see lines of code, like #include in front of me. From what I've read from descriptions of other neurodiverse people on the internet, some of them seem to have this kind of learning style as well. We “internalize code”.

Programming and development Red Hat Developers Blog Programming cheat sheets Try for free: Red Hat Learning Subscription eBook: An introduction to programming with Bash Bash shell scripting cheat sheet eBook: Modernizing Enterprise Java

Some autistic people are a lot better at memorizing large chunks of code than me, but the process seems to be the same. When understanding concepts such as structs, pointers, pointers to pointers, matrices, and vectors, it's helpful for me to think in pictures, such as the ones you find in programming tutorials and books.

I like to use C to understand how things are done at a lower level, such as file input and output (I/O), networking programming, and so on. This doesn't mean I don't like libraries that handle tasks such as string manipulation or making arrays. I also like the ease of creating arrays and vectors in Java. However, for creating a user interface, though I have looked at such code in C, I prefer to use grapical editors, such as Netbeans and similar.

My ideal C GUI open source tool for creating applications

If I imagine an ideal open source tool for creating a GUI using C, it would be something similar to Netbeans that, for example, making GTK-interfaces by dragging and dropping. It should also be possible to put C on buttons, and so on, to make them perform actions. There may be such a tool. I admittedly haven't looked around that much.

Why I encourage young neurodiverse people to learn C

Gaming is a big industry. Some studies suggest neurodiverse kids may be even more focused on gaming than other kids. I would tell a neurodiverse high school or college kid that If you learn C, you may be able to learn the basics of, for example, writing efficient drivers for a graphics card, or to make efficient file I/O routines to optimize their favorite game. I would also be honest that it takes time and effort to learn, but that it's worth the effort. Once you learn it, you have greater control of things like hardware.

For learning C, I recommend a neurodiverse kid to install a beginner-friendly Linux distro, and then find some tutorials on the net. I also recommend breaking down things step by step, and drawing diagrams of, for example, pointers. I did that to better understand the concept, and it worked for me.

In the end, that's what it's about: Find a learning method that works for you, no matter what teachers and other students may say, and use it to learn the open source skill that interests you. It can be done, and anyone can do it.

I've learned that if you can find the method that works for you, no matter what teachers and other students may say, you can learn any open source skill that interests you.

Image by:

opensource.com

Programming Diversity and inclusion Accessibility What to read next Accessibility in open source for people with ADHD, dyslexia, and Autism Spectrum Disorder This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

How to (safely) read user input with the getline function

opensource.com - Tue, 05/10/2022 - 15:00
How to (safely) read user input with the getline function Jim Hall Tue, 05/10/2022 - 03:00 Register or Login to like Register or Login to like

Reading strings in C used to be a very dangerous thing to do. When reading input from the user, programmers might be tempted to use the gets function from the C Standard Library. The usage for gets is simple enough:

char *gets(char *string);

That is, gets reads data from standard input, and stores the result in a string variable. Using gets returns a pointer to the string, or the value NULL if nothing was read.

As a simple example, we might ask the user a question and read the result into a string:

#include
#include

int
main()
{
  char city[10];                       // Such as "Chicago"

  // this is bad .. please don't use gets

  puts("Where do you live?");
  gets(city);

  printf("<%s> is length %ld\n", city, strlen(city));

  return 0;
}

Entering a relatively short value with the above program works well enough:

Where do you live?
Chicago
<Chicago> is length 7

Programming and development Red Hat Developers Blog Programming cheat sheets Try for free: Red Hat Learning Subscription eBook: An introduction to programming with Bash Bash shell scripting cheat sheet eBook: Modernizing Enterprise Java

However, the gets function is very simple, and will naively read data until it thinks the user is finished. But gets doesn't check that the string is long enough to hold the user's input. Entering a very long value will cause gets to store more data than the string variable can hold, resulting in overwriting other parts of memory.

Where do you live?
Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
<Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch> is length 58
Segmentation fault (core dumped)

At best, overwriting parts of memory simply breaks the program. At worst, this introduces a critical security bug where a bad user can insert arbitrary data into the computer's memory via your program.

That's why the gets function is dangerous to use in a program. Using gets, you have no control over how much data your program attempts to read from the user. This often leads to buffer overflow.

The safer way

The fgets function has historically been the recommended way to read strings safely. This version of gets provides a safety check by only reading up to a certain number of characters, passed as a function argument:

char *fgets(char *string, int size, FILE *stream);

The fgets function reads from the file pointer, and stores data into a string variable, but only up to the length indicated by size. We can test this by updating our sample program to use fgets instead of gets:

#include

#include

int

main()

{

char city[10]; // Such as “Chicago”

// fgets is better but not perfect

puts(“Where do you live?”);

fgets(city, 10, stdin);

printf("<%s> is length %ld\n", city, strlen(city));

return 0;

}

If you compile and run this program, you can enter an arbitrarily long city name at the prompt. However, the program will only read enough data to fit into a string variable of size=10. And because C adds a null (‘\0') character to the ends of strings, that meansfgets will only read 9 characters into the string:

Where do you live?
Minneapolis
<Minneapol> is length 9

While this is certainly safer than using fgets to read user input, it does so at the cost of "cutting off" your user's input if it is too long.

The new safe way

A more flexible solution to reading long data is to allow the string-reading function to allocate more memory to the string, if the user entered more data than the variable might hold. By resizing the string variable as necessary, the program always has enough room to store the user's input.

The getline function does exactly that. This function reads input from an input stream, such as the keyboard or a file, and stores the data in a string variable. But unlike fgets and gets, getline resizes the string with realloc to ensure there is enough memory to store the complete input.

ssize_t getline(char **pstring, size_t *size, FILE *stream);

The getline is actually a wrapper to a similar function called getdelim that reads data up to a special delimiter character. In this case, getline uses a newline ('\n') as the delimiter, because when reading user input either from the keyboard or from a file, lines of data are separated by a newline character.

The result is a much safer method to read arbitrary data, one line at a time. To use getline, define a string pointer and set it to NULL to indicate no memory has been set aside yet. Also define a "string size" variable of type size_t and give it a zero value. When you call getline, you'll use pointers to both the string and the string size variables, and indicate where to read data. For a sample program, we can read from the standard input:

#include
#include
#include

int
main()
{
  char *string = NULL;
  size_t size = 0;
  ssize_t chars_read;

  // read a long string with getline

  puts("Enter a really long string:");

  chars_read = getline(&string, &size, stdin);
  printf("getline returned %ld\n", chars_read);

  // check for errors

  if (chars_read < 0) {
    puts("couldn't read the input");
    free(string);
    return 1;
  }

  // print the string

  printf("<%s> is length %ld\n", string, strlen(string));

  // free the memory used by string

  free(string);

  return 0;
}

As the getline reads data, it will automatically reallocate more memory for the string variable as needed. When the function has read all the data from one line, it updates the size of the string via the pointer, and returns the number of characters read, including the delimiter.


Enter a really long string:
Supercalifragilisticexpialidocious
getline returned 35
<Supercalifragilisticexpialidocious
> is length 35


Note that the string includes the delimiter character. For getline, the delimiter is the newline, which is why the output has a line feed in there. If you don't want the delimiter in your string value, you can use another function to change the delimiter to a null character in the string.

With getline, programmers can safely avoid one of the common pitfalls of C programming. You can never tell what data your user might try to enter, which is why using gets is unsafe, and fgets is awkward. Instead, getline offers a more flexible way to read user data into your program without breaking the system.

Getline offers a more flexible way to read user data into your program without breaking the system.

Image by:

Image by Mapbox Uncharted ERG, CC-BY 3.0 US

What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

How to Run a Command Multiple Times in Linux

Tecmint - Tue, 05/10/2022 - 13:46
The post How to Run a Command Multiple Times in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

For one reason or the other, you may wish to run a command repeatedly for several times in Linux. This guide will discuss some of the common and effective ways to achieve just that.

The post How to Run a Command Multiple Times in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Fueling the spark of innovation

Red Hat News - Tue, 05/10/2022 - 12:00

The following is an excerpt from my keynote today at Red Hat Summit.

 

Open source can fan the sparks of potential around the world, unlike any system that has ever existed. It solves two fundamental problems that limit innovation: 

  • Having ideas be noticed

  • Having ideas be iterated on. 

The new normal for IT starts with open source

Red Hat News - Tue, 05/10/2022 - 12:00

Today I gave a keynote address at Red Hat Summit; the following is an excerpt from that talk.

 

The past two years have focused on "what we all had been going through." But I think we should look at it as "what we have and are accomplishing." 

Announcing the 2022 Red Hat Certified Professional of the Year, Neil D’Souza

Red Hat News - Tue, 05/10/2022 - 12:00

Each year during Red Hat Summit, we recognize Red Hat Certified Professionals who make a difference in their organizations by demonstrating enthusiasm, commitment and dedication to their role and industry. We’re proud to announce that Neil D’Souza, Senior Site Reliability Engineer at ServiceNow, has been named the 2022 Red Hat Certified Professional of the Year.

The new standard: Red Hat In-Vehicle Operating System in modern and future vehicles

Red Hat News - Tue, 05/10/2022 - 12:00

Open source has long been a force multiplier for innovation. Time and time again, operating in the open source way has proven that the power of many is stronger than the power of few.

Intel's ControlFlag 1.2 Released To Use AI To Provide Full Support For Spotting C++ Bugs

Phoronix - Tue, 05/10/2022 - 08:46
Last year Intel open-sourced the ControlFlag project for using machine learning to uncover bugs within code. With today's ControlFlag 1.2 release, C++ is now a fully supported language for this AI-driven project for uncovering bugs within arbitrary code-bases...

Pages