Steve Litt on 5 Oct 2018 19:08:14 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Anyone Experienced with C Pointers?


On Fri, 05 Oct 2018 17:19:22 -0400
Ron Guilmet <ronpguilmet@gmail.com> wrote:

> I have been working with writing a device driver for my HP laptop. The
> wireless doesn't work well. I think it is an antenna selection issue.
> The laptop any distro shows about 10% on the wifi even when you are 10
> feet away. 
> 
> That said, I've been refreshing myself with C. Pointers is one of
> those areas that seem to give everyone a hard road to hoe. Sometimes
> a high overview helps. I wanted to present what my understanding is,
> and I was hoping someone could elaborate on it.
> 
> My understanding of pointer use is the following. I understand there
> are different sections of memory. A section for the code, a section
> for the running app called the stack, and then your free or available
> memory called the heap. 

You have the stack and the heap. They don't necessarily have specific
purposes: In many cases you have your choice of using either. Generally
speaking, if you need big hunks of memory, you're better off using the
heap because there's a lot more RAM in the heap. If you run out of
stack, your program crashes.

A stack is a very simple Computer Science set of data structures plus a
few algorithms. I'd suggest you implement your own stack just to
understand how a stack works. You can find simple stacks to program all
over the Internet.

Another way to familiarize yourself with stacks is to fool around with
the dc calculator that comes on almost every Linux distro.

Here are some examples of stack usage inside C:

char lastname[20]  /* Pop 20 * sizeof(char) off stack
char * buffer      /* Pop 1 * sizeof(char *) off stack

The preceding declarations pop the RAM necessary to create those
variables off the stack. This almost always happens inside a "block",
which is usually a function, and in the C language, when you exit that
block, the RAM for those variables is pushed back onto the stack, for
use by other blocks. However, and this can become crucial, the RAM
pushed back is not erased, so the following code will work most of the
time and then fail once in a while,  driving you nuts:

char * prependp(char * str){
  char buf[20];
  char *p = buf;
  *p++ = 'p';
  strncpy(p, str, sizeof(buf) - 2* sizeof(char));
  return(buf);
}

char *thename = prependp("rudential");
printf(thename);
char bigchunk[100];
strcpy(bigchunk, "antidisestablishmentarianism anesthetizes androids");
char *thename = prependp("rudential");
printf(thename);

It's probable that the first and second print statement will be
different because the strcpy() to bigchunk probably overwrote the part
of the stack that contained buf within function prepend().

About the heap: Assuming char pointer p was already declared, it is
made to point to a huge hunk off the heap like this:

p = malloc(1000 * sizeof(char));

Unlike stuff off the stack, such a heap allocation stays valid after
the block ends, and stays valid until you perform free(p). So with the
heap, YOU need to keep track of freeing what you malloc. With the
stack, the language does it for you (when you exit the block in which
you declared it).


> I understand that if the running program needs
> more memory, it has to access the heap, 

No, it can declare a local variable and gain more memory.

> and the heap can only be
> addressed by memory addresses. 

If in the preceding you change the word "memory address" to "pointer",
yes. In very early C compilers (on very early MS-DOS computers), yes, a
pointer could e assigned to a specific memory address, but with paged
memory and protected memory etc of modern computers, it's better just
to call it a "pointer".

Seriously, take an hour to look up and implement a very 

 
SteveT

Steve Litt 
September 2018 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug