Initializing strings in C

0

I've started to learn C and just learned that strings are just arrays of chars. I wanted to see values are in the strings at every given moment so I set a breakpoint in vscode, stepped through the execution, and looked at all the values.

int main()
{
    char a[4] = "foo";
    char b[4] = "bar";

    return 0;
}

I saw that before initializing the a[] array, there are already values in memory. screenshot of vscode while debugging.

My question is, what are those values? Why are they there? What do they represent?

c
2021-11-24 06:29:21
4

1

When defining a value, it takes a part of memory that could have contained other numbers. It's basically those numbers being shown in your debug tab.

2021-11-24 14:32:02
0

When you first declare array or variable, it's assigned memory and that memory can contain some garbage values already, so it prints like this way

Garbage value can be anything, the language standard doesn't specify what it should be

2021-11-24 12:55:09
0

Memory is memory, it could be uninitialized (aka filled with garbage) or it could be initialized with data.

In your case, when your program jumped to your main function a stack was created for it that would contain your local variables (your local variable being the char array you declared, basically a pointer to some place in memory). Before you initialized said pointers a and b to point at your string, they could have contained any old numbers and so trying to read the "string" at that address would give you more random garbage.

For example, if you wrote int a = 5; in your program and stepped through, you would similarly see that a might contain some random number before you assign it 5.

2021-11-24 07:01:48

I see. But the thing that I can't seem to understand is why do I see the same values everytime I debug the program?
jon doe
-1

When you create a new value like integers or arrays, system will give these values an address for saving its data on that address. There could be some data on that address for other applications and after closing that application, RAM won't remove them, so before initializing, you will see those data.

2021-11-24 07:08:54

In general-purpose multiuser operating systems, the memory of one process never contains data from another process, except for data that is deliberately shared. The operating system clears memory before reassigning it to another process, for security purposes.
Eric Postpischil

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................