How do you create memory diagrams and data structures?

I need to create a short C program that manages strings, but I always come across some strange pointer errors. Although K & R is a great reference to the language, and I often look at it when I'm puzzled, it already suggests that you are an adequate programmer.

A lecturer who teaches us programming said good programmers make good diagrams on these materials, but I have no idea how to do this. Can you recommend a good book or lectures on this?

Thank you, I will be grateful for every reply received.

+7
c memory diagram
source share
11 answers

I repeat the suggestion to draw them on paper first, and then, if you feel the need, you can include the ascii version in the code.

I usually use these three formats:


to learn about memory:

       + -------- +
    0 |  |  <- start
       + -------- +
    1 |  |  <- q scans from start to end
       + -------- +
       ~ ..... ~
       + -------- +
       |  |  <- end
       + -------- + \
       |  |  |
       + -------- + | __ rest of the
       ~ ..... ~ |  allocated memory
   n |  |  |
       + -------- + /


to talk about lines:

     0 n
    + - + - - + - + - + - +
    |  |  ... |  |  | \ 0 |
    + - + - - + - + - + - +
      ^ ^ __ q moves from the
      |  end to the start
      p moves from
        start to the end


to talk about bits in a word:

    xxxx yyzz 00tt 11ss
    \ \ \ \ \ \ \ \ __ storage registry
     \ \ \ \ \ \ ___ always set to 1
      \ \ \ \ \ _____ temp value
       \ \ \ \ ______ always zeroed 
        \ \ \ ________ zero flag value
         \ \ _________ y ​​register
          \ _____________ x address            

I did something similar for state machines too, but they were too complicated (and took a lot of time), so now I directly insert graphviz code into the comment. Without even knowing about GraphViz, it should be easy to guess how to draw an FSM diagram.

 digraph G {
   mode = hier

   LIMBO [style = filled];
     node [shape = ellipse];

     LIMBO -> HEADER;
     HEADER -> LIMBO;
     HEADER -> TUNE;
     TUNE -> LYRICS;
     TUNE -> CHORD [style = dashed];
     TUNE -> LIMBO;
     GRACE -> TUNE;
     GRACE -> CHORD [style = dashed];
     SYMBOLS -> TUNE;
     SYMBOLS -> LIMBO;

     overlap = false
   sep = 1.5
 }


They cover a huge number of diagrams that I need. For more complex ones, I use GraphViz or OpenOffice Draw.

+10
source share

One of the most useful things I've ever done is my drawing app ...

In an application where there was a complex data structure specialized for the task (the standard hash table wasn’t executed in this case ;-) I had my application for outputting the “.dot” script of this graphviz point tool can be analyzed.

This was done using the dump procedure (well, method, it was C ++), which outputs the .dot header

digraph g { 

then went through my data structure and then wrote a footer

  } 

In the walk structure, he writes each pointer with

  SOURCE -> DESTINATION 

where Source is the memory address of the binding object previously prepared by O (O213435354), and Destination is the object pointed to by the same format.

At the beginning of each object, he also wrote

 SOURCE [ .... ] 

with ... object data.

Whenever the application was in the "intersting" state, I unloaded the graph, and then used the dot-tool from the graph to render. I found a lot of pointer errors quite easily so the eye is created to see regular structures in lines ...

By the way, I still regularly use graphviz , since it’s quite nice to write and edit graphs using a text editor, and the tool renders them later. If I need to draw .dot diagrams for a spoiled powerpoint audience, I upload them to OmniGraffle on my mac. (And my colleagues on the computer think that I have some features of superheroes with diagrams, because with this combination I get graphs 10 times faster than they do when they use Visio)

+8
source share

Good programmers do not make "pleasant" diagrams - they make messy, illegible ones that serve their purpose. Just draw what you need on any available surface. And you don’t have to think about drawing the “right” figures - a former colleague of mine once said that the diagram methodology is not worth sh * t if you could not draw diagrams on the back of the fag (cigarette for non-UK).

+7
source share

A typical memory model for C is that memory is a collection of boxes; each box is 1 byte wide. Each box has an address in hexadecimal format. A pointer is a variable in which there is an address.

For books / lectures, you are looking for a basic computer organization and a book on architecture.

I used these 3 books; I order them "reducing clarity." The classic is the last of Patterson and Hennessy, but I consider it the least readable. YMMV.

http://www.amazon.com/Introduction-Computing-Systems-gates-beyond/dp/0072467509/ref=sr_1_1?ie=UTF8& s = books & QID = 1238345302 & cf = 1-1

http://www.amazon.com/Digital-Design-Computer-Architecture-Harris/dp/0123704979/ref=sr_1_6?ie=UTF8& s = books & QID = 1238345245 & cp = 1-6

http://www.amazon.com/Computer-Organization-Design-Fourth-Architecture/dp/0123744938/ref=sr_1_3?ie=UTF8& s = books & QID = 1238345245 & cp = 1-3

+2
source share

Are you asking for charting tools or what diagrams should represent? I would recommend just using a piece of paper and a pencil for everything you do to try to figure it out for yourself; you don’t need fancy tools.

If you want to know how pointers work and what diagrams mean, I found this handout after Google that looks like it can be a decent tutorial.

+1
source share

To do this, you can see the Gustave Duartes blog . In his posts, he often draws memory diagrams, etc. He uses Visio for this, and I, for one, find the results pretty neat.

+1
source share

There are not many tools that will draw the style of memory cards that your professor refers to.

The idea he is trying to convey is that when you are a novice C developer, using pointers and arrays confuses you. What is it aimed at? Do these two pointers point to the same thing? What cells of a row array belong to each index, etc.?

Experienced programmers have a mental image of this. With experience, so are you. Until then, the idea is that you can physically draw something on paper to understand these things, and not have all this in your head.

I agree that K & R is not meant as a textbook and therefore does not have these visual effects.

You can see an example of these diagrams in the slides that I made for the C tutorial

+1
source share

Use a block of squares (quadrille, not a graph) and a decent pencil * if you want your diagrams to look beautiful. Also useful for representing memory cells, letters in strings, etc. You can even get quartz moleskines if you want to be hip and square at the same time.

*** I like uni shalaku dx 0.5 because it has a feeder button on the side, so you don’t need to change grip to get more leadership.

+1
source share

An easy way is to draw memory as one huge “staircase”. Each cell in the staircase represents one address in memory. Enter data in the input field, and enter the address next to it. Something like that:

http://cosketch.com/Saved/pe6sHGD0

Also find out how the debugger works, it often shows you the contents of pointers, variables, etc.

+1
source share

This only applies to those who are familiar with the insides of the machine, but the trick I use is to write a "fake" memory address on my diagrams so that it looks realistic. I mean the following:

  .------. A | 3FFC | '------' .------. B | 7 | '------' 

Guess which variable has a pointer and matters? In fact, I usually write the address in box (or above) B to show where it is. And then I will make another (completely different) fake number to show that A is also located somewhere. This is useful when you are just starting out. Later you will get used to all this index material.

+1
source share

the best way to visualize your problem is to debug your code. and focus on your variable. it will not only give you an idea of ​​your code, but also allow you to draw the exact code diagram that your lecturer told you.

+1
source share

All Articles