Realloc problem, environment variables Linux C

This is a discussion on Realloc problem, environment variables Linux C within the C Programming forums, part of the General Programming Boards category; Hey Community,
I have to write my own implementation of setenv, getenv and unsetenv on a linux system for school.
...

Realloc problem, environment variables Linux C

Hey Community,
I have to write my own implementation of setenv, getenv and unsetenv on a linux system for school.

Now if i'm adding an environment variable i have to copy all variables from stack to heap.
That works fine. But if i'm adding more variables to the list realloc throws some strange error.
I'm pretty shure the error occurs when realloc has to actually move the memory block. In most cases the error doesnt occure when calling realloc the first time

Here's my whole implementation. If you dont want to read all the code, just look at env.c on the bottom in the function enter_env (CASE3)

Well, for starters, you aren't allocating enough space here. If 'name' is four, and you allocate five spaces, to hold 'name', and then you strcat "=" to the end of it, you have just overrun the space you've allocated.

Well, for starters, you aren't allocating enough space here. If 'name' is four, and you allocate five spaces, to hold 'name', and then you strcat "=" to the end of it, you have just overrun the space you've allocated.

Quzah.

I'm sorry, i have posted my developement version. I have edited this back to my old version

Are the warnings turned all the way up on the compiler? Try gcc -Wall main.c env.c.
Why do you put so many statements on a single line (e.g. if and two subordinate statements)? Makes it very hard to read.