123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|655|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> Malloc

Sun, 03 Feb 2008, 12:44
HoboBen
I'm trying to get a simple command line C text editor working (it's less featured than "echo foo > file.txt", but I'm learning! Ed, the UNIX editor, is my inspiration here)

It worked great when I had a fixed-sized string, but that's hardly ideal.

So instead, I'm trying to malloc it bigger each time.

I've got this, which runs but gives garbage to usrin. It also crashes whenever I type more than 8 letters. Confusing, because it makes me think my bytes are being bitty, or something.

Can anyone point me in the right direction?





-=-=-
blog | work | code | more code
Sun, 03 Feb 2008, 14:40
Phoenix
I don't understand what you mean, but I'll take a wild guess: wouldn't it be easier to just use strcat() instead of doing... whatever you're doing now?
Sun, 03 Feb 2008, 14:45
HoboBen
That doesn't resize the max string length though, does it?

I know my C is a bit of a monstrosity

-=-=-
blog | work | code | more code
Sun, 03 Feb 2008, 14:56
mike_g
Well first off if you want to use malloc you will need to include stdlib.h

Then theres a bit of a problem with the way you are using malloc. You are only doing allocation when userin == NULL, however you never initialized it as such nor set to it, also for strings '\0' is used to in place of NULL. NULL is used for pointers. Theres also no need for the (char*) cast as thats what userin is anyway.

like phoenix, I'm not sure what exactly you are trying to achieve here. I think you would be much better of getting the size of the file first, then allocating space before you read the data in.

something like:


Anyway, good luck with it. I'm sure you can get it working
Sun, 03 Feb 2008, 16:05
HoboBen
Thanks mike,

Got a few ideas from there.

I'm trying to get input from the keyboard, char by char, but not be limited to a fixed-sized array (overflows/crashes)

I have no idea what the input length will be, so I'm increasing it char by char.

I'll have another stab.

-=-=-
blog | work | code | more code
Sun, 03 Feb 2008, 16:47
mike_g
Ok cool, for some reason I assumed you were reading from a file.

To get user input I'd use fgets as it prevents buffer overflows. Basically I'd have a local predetermined array to get the input to. Something like:

You may also want to chop the \n character off the end of the input as that gets stored as will.

Then to store it, allocate space according to the string length. the Temp array will be destroyed when it goes out of scope. If you want to add the input to a big block of text you could use realloc on your existing stuff.