0

My book talks about the global data segment and the dynamic data segment. Here is a picture from my book: enter image description here

This makes me wonder about global variables? Where are global variables stored? It says that they are stored in the global data segment, but what if the variable is not known at start-up, then it should be in the dynamic data segment?

user394334
  • 243
  • 1
  • 10
  • Global variables are known at startup. Do you have an example where you think it is not the case? – Jester Nov 14 '22 at 22:08
  • @Jester I am thinking about the case where they are input from the key board for example. – user394334 Nov 14 '22 at 22:15
  • 2
    Doesn't matter, the variable already exists and has a value before that. – Jester Nov 14 '22 at 22:16
  • @Jester Thank you. Could you please give an example of data that is in the dynamic data segment? – user394334 Nov 14 '22 at 22:22
  • The book page already does. For example local variables live on the stack and stuff allocated by `malloc` on the heap. Both are dynamic data. – Jester Nov 14 '22 at 22:27
  • @Jester Thank you, do you then agree that local variables are not known at start up? – user394334 Nov 14 '22 at 22:35
  • They are not. They are allocated and freed dynamically as functions are invoked. – Jester Nov 14 '22 at 22:47
  • 2
    @user394334 local/global is not the important thing. What matters is *storage class.* Static variables are allocated in the data segment. They can both be local and global in C. Automatic variables are allocated on the stack. They can only be local. – fuz Nov 14 '22 at 22:59
  • This book is pretty high level, you need to learn more detail but understand that the detail can vary by tool/environment. In that you may have some IDE with a toolchain and sdk behind, specific C library, etc. And some dialog boxes with options that you choose and those options may be high level as well implying that the language or the architecture or toolchain, etc use those terms and such, but they do not. – old_timer Nov 15 '22 at 15:43
  • See: [ARM link and frame pointer](https://stackoverflow.com/questions/15752188/arm-link-register-and-frame-pointer), which will explain one part of 'dynamic data'. Ie, recursive functions, each **CAN** create local (distinct) variables on the stack. The other *dynamic* is malloc() termed the *heap*. text can include constant data. Your typical assembler variable and 'C' static or globals are also 'global data'. They are fixed and only one of them. However, there is a mode where you use 'sb' (R9) or static base and 'global data' is used via this pointer. – artless noise Nov 15 '22 at 19:27

1 Answers1

2

Placing things in segments depends on when the memory is allocated, not when it is used or gets its value.

What this book is referring to as the global data segment is all the variables which are allocated their address at compile time.

What this book is referring to as the dynamic data segment is things which do not get their address assigned until run time.

However, as I explained when you posted almost exactly this same question previously, this book is not very good! In the real world the stack and heap are not usually lumped together as a single segment, they are two very different things and should be handled separately.

Having the heap grow up and stack grow down towards each other in the same block used to be a common way of maximizing available memory, and is still often used in (non safety critical) embedded systems, but more commonly now they are completely separate.

Also, the global variables are in many categories. Read-only global variables may actually be in the text segment, or in their own read-only data segment that this book doesn't seem to know about. Writeable global variables fall into two or three categories depending on whether they are zero-initialized, non-zero initialized, or uninitialized, and each of these has a separate segment name (typically .bss and .data for the first two and non-standardized name for uninitialized data).

Tom V
  • 4,827
  • 2
  • 5
  • 22