TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Basics of Memory Addresses in C

62 pointsby denniskubesalmost 13 years ago

8 comments

glimcatalmost 13 years ago
The fastest way to grok memory issues is to do some work in Assembly. Few programmers will use Assembly for regular work, but the intuition it fosters will serve you everywhere.
评论 #4400680 未加载
评论 #4400394 未加载
评论 #4400329 未加载
评论 #4401188 未加载
tjoffalmost 13 years ago
I appreciate the distinction between arrays and pointers, but the article fails to mention a similar pitfall: <i>A struct is the sizeof its members.</i>, which isn't necessarily true.<p>It's like how everyone learns that (INT_MAX + 1) == INT_MIN (even non-developers seems to know this) yet that it actually is undefined (in C/C++), I feel that just noting that it isn't the whole truth (such as noting that the OS handles the memory behind your back) is quite valuable, even when learning the basics.<p>Otherwise you might end up feeling, as I do, that your foundation is shaky and built up on lies - not really knowing what "facts" you can trust.
评论 #4440049 未加载
sswezeyalmost 13 years ago
One thing to note:<p>Maybe you should explain the first element of an array having the same memory address as the actual array a little bit more, and relate it to why array indexing is 0-based too - the index is that many offsets from the beginning from the array.
dschatzalmost 13 years ago
Nice introduction. I think it is worth pointing out that much of what you discuss is implementation dependent, the c standard doesn't require an implementation to lay out data in memory in any particular way. Instead it requires that access semantics behave in a particular way. These semantics, in turn, align with easy, low level implementations.
评论 #4400573 未加载
nemetroidalmost 13 years ago
I like the style, but it was basics indeed. I look forward to following parts. Something that initially confused me about sizeof on arrays is the somewhat deceiving parameter form `char s[]`.
评论 #4399748 未加载
p4bl0almost 13 years ago
"Arrays are not pointers". Right. And wrong, actually.<p>The article says that arrays are different from pointers, but it does not prove it. It is quite simple to prove, see the program below.<p>Also, it's not interesting to limit the definition of arrays to just the locally and statically declared ones. If you do that then something like 90% of C programs (if not more, I think I never wrote such a C program except for exercises in class) don't use arrays at all. In all the other case (arrays passed as argument to a function, dynamically allocated arrays…), the <i>are</i> the same as pointers. Again, see the program below.<p>In reality, it is a bit pedantic to insist on this distinction, except for the rare case where it is a performance issue (the arrays of the article require one less memory access, the one to get the address of the memory at which the array starts).<p><pre><code> #include &#60;stdio.h&#62; #include &#60;stdlib.h&#62; void f (char a[], char *b, char *c) { printf("Once passed to a function as arguments:\n\n"); printf("What the article limits the definition of array to:\n"); printf("&#38;a = %p\n", &#38;a); printf("a = %p\n", a); printf("&#38;(a[0]) = %p\n", &#38;(a[0])); printf("a + 1 = %p\n", a + 1); printf("&#38;(a[1]) = %p\n", &#38;(a[1])); printf("\n"); printf("Pointer to an array:\n"); printf("&#38;b = %p\n", &#38;b); printf("b = %p\n", b); printf("&#38;(b[0]) = %p\n", &#38;(b[0])); printf("b + 1 = %p\n", b + 1); printf("&#38;(b[1]) = %p\n", &#38;(b[1])); printf("\n"); printf("Pointer to dynamically allocated memory:\n"); printf("&#38;c = %p\n", &#38;c); printf("c = %p\n", c); printf("&#38;(c[0]) = %p\n", &#38;(c[0])); printf("c + 1 = %p\n", c + 1); printf("&#38;(c[1]) = %p\n", &#38;(c[1])); printf("\n"); } int main (int argc, char *argv[]) { char a[4]; char *b = a; char *c = malloc(sizeof(*c) * 4); printf("What the article limits the definition of array to:\n"); printf("&#38;a = %p\n", &#38;a); /* behavior differs only here, this is the difference with pointers */ printf("a = %p\n", a); printf("&#38;(a[0]) = %p\n", &#38;(a[0])); printf("a + 1 = %p\n", a + 1); printf("&#38;(a[1]) = %p\n", &#38;(a[1])); printf("\n"); printf("Pointer to an array:\n"); printf("&#38;b = %p\n", &#38;b); printf("b = %p\n", b); printf("&#38;(b[0]) = %p\n", &#38;(b[0])); printf("b + 1 = %p\n", b + 1); printf("&#38;(b[1]) = %p\n", &#38;(b[1])); printf("\n"); printf("Pointer to dynamically allocated memory:\n"); printf("&#38;c = %p\n", &#38;c); printf("c = %p\n", c); printf("&#38;(c[0]) = %p\n", &#38;(c[0])); printf("c + 1 = %p\n", c + 1); printf("&#38;(c[1]) = %p\n", &#38;(c[1])); printf("\n"); f(a, b, c); return 0; } </code></pre> Here is a possible output of this program:<p><pre><code> What the article limits the definition of array to: &#38;a = 0x7fff5590ff00 a = 0x7fff5590ff00 &#38;(a[0]) = 0x7fff5590ff00 a + 1 = 0x7fff5590ff01 &#38;(a[1]) = 0x7fff5590ff01 Pointer to an array: &#38;b = 0x7fff5590fef8 b = 0x7fff5590ff00 &#38;(b[0]) = 0x7fff5590ff00 b + 1 = 0x7fff5590ff01 &#38;(b[1]) = 0x7fff5590ff01 Pointer to dynamically allocated memory: &#38;c = 0x7fff5590fef0 c = 0x202f010 &#38;(c[0]) = 0x202f010 c + 1 = 0x202f011 &#38;(c[1]) = 0x202f011 Once passed to a function as arguments: What the article limits the definition of array to: &#38;a = 0x7fff5590fec8 a = 0x7fff5590ff00 &#38;(a[0]) = 0x7fff5590ff00 a + 1 = 0x7fff5590ff01 &#38;(a[1]) = 0x7fff5590ff01 Pointer to an array: &#38;b = 0x7fff5590fec0 b = 0x7fff5590ff00 &#38;(b[0]) = 0x7fff5590ff00 b + 1 = 0x7fff5590ff01 &#38;(b[1]) = 0x7fff5590ff01 Pointer to dynamically allocated memory: &#38;c = 0x7fff5590feb8 c = 0x202f010 &#38;(c[0]) = 0x202f010 c + 1 = 0x202f011 &#38;(c[1]) = 0x202f011 </code></pre> As we can see, for their practical use arrays and pointers can really be seen as the same thing. So again, except if you are optimizing a program where you can statically declare your arrays and access them a lot (i.e., you are doing matrix multiplication), the difference between arrays and pointers does not really matter.
评论 #4400783 未加载
_kst_almost 13 years ago
<i>Thank you</i> for making it clear that arrays are not pointers!<p>I posted a few comments on the site.
iopuyalmost 13 years ago
Easy to follow, well written, cant wait for the follow ups. Thanks!