user47376
user47376

Reputation: 2273

Prevent memory overwrite when using pointers in C

I'm new to C, and I'm getting started on programming a simple program that runs after boot, without an OS, so I don't have malloc and such.

I'm trying to understand memory management; look at this example:

char *a = read_string(); // gets user input string, the length is unknown.
char *b = read_string();

How would I know that b doesn't overwrite a?

Especially when writing a function that knows the string length only at runtime?

Upvotes: 1

Views: 436

Answers (2)

user47376
user47376

Reputation: 2273

I'll have to write my own memory manager to track allocated memory.

Thx everyone.

Upvotes: 0

user4658736
user4658736

Reputation:

In your question, everything depends on the read_string() function implementation. If that function correctly allocates memory for each new string, your a and b pointers will point to different strings.

Upvotes: 1

Related Questions