Skip to content

Exercise 6-3 #19

@ajay1135

Description

@ajay1135

Hi, I have a few quick questions about your implementation for this exercise.

For your implementation of setenv:

I think that the function as written currently could potentially cause a memory leak and/or shrink the existing environment list. If an environment variable is already defined in the list, and it's found as you iterate through the list, the new value for the environment variable is set appropriately (assuming overwrite != 0). When this happens, isn't there a memory leak that occurs? The memory allocated to store the old value of the environment variable isn't freed, but the pointed to C-string changes. And if this occurs in the middle of the list, the list seems to be truncated (the next element in the list is set to NULL, which denotes the end of the list). Is this intentional?

For your implementation of unsetenv:

I think that the same memory leak concern applies here as well.

Also, in my copy of the book, this exercise includes the following:

Your version of unsetenv() should check to see
whether there are multiple definitions of an environment variable, and remove
them all (which is what the glibc version of unsetenv() does).

But I think the current implementation will only remove one (the first) instance of the environment variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions