![]() |
|
[C] [Help] User Input Function returns Gibberish - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] [Help] User Input Function returns Gibberish (/showthread.php?tid=5930201) |
[Help] User Input Function returns Gibberish - lockhead - Jan 16, 2019 Hey so I wanted to write a function that reads user input (of unknown length) and returns it to the caller. Sounds simple, right? Well I can't figure out why it (kinda) doesn't work. Here's the code Code #include <stdio.h>The reason why i say "kinda" doesn't work is because it does work when I run it through gdb, but doesn't when I run it normally (terminal) and enter a string equal to or longer than seven characters. The reason for that is that I have 8 byte buffer, if that is filled str get's realloc'ed to an additional 8 bytes. So if the input string is smaller than seven characters in length this works fine, but if it is larger or equal to seven this works only in gdb and return gibberish through normal program execution and I don't know why. Realloc shouldn't be messing with my string, right? And it get's passed as a function argument, so it's not a local variable either... Sorry for the low quality post but I just can't figure it out. RE: [Help] User Input Function returns Gibberish - Auriana - Jan 16, 2019 If you're going to reallocate the pointer, you need to pass the pointer to the pointer into the function. Or you could just make it easier and return an allocated pointer from the getUserInput function. This should work. Code #include <stdio.h>RE: [Help] User Input Function returns Gibberish - lockhead - Jan 16, 2019 (Jan 16, 2019 08:20 AM)Auriana Wrote: If you're going to reallocate the pointer, you need to pass the pointer to the pointer into the function. Why do I have to pass a pointer to the pointer? Does altering the pointer within a function somehow make it a local variable? Anyway, thank you for your help! I appreciate it 🤝 RE: [Help] User Input Function returns Gibberish - Auriana - Jan 16, 2019 (Jan 16, 2019 08:38 AM)lockhead Wrote: Why do I have to pass a pointer to the pointer? Does altering the pointer within a function somehow make it a local variable? You're reassigning it's value in another function. Code void reassignpointer(char** pointertobereassigned);A pointer is just a memory address so when you pass a pointer, you're allowing the function to change whatever value is at that memory address. But you're not allowing the function to assign that pointer itself because it's just holding a copy. So in order to reassign a pointer you need to pass the address of the pointer itself on the stack, allowing the function to change the pointer, that the pointer you passed on the stack, points to... If that makes any sense. RE: [Help] User Input Function returns Gibberish - lockhead - Jan 16, 2019 (Jan 16, 2019 09:49 AM)Auriana Wrote: You're reassigning it's value in another function. Ok, thank you again! RE: [Help] User Input Function returns Gibberish - you_can_never_hack_me - Jan 16, 2019 I'm not goin' to help you with your idea, 'cause that is one of the most insecure ways in dealin' with inputs - in making functions. Consider the following as your function's parameters:
Code unsigned int getuserinput (char *buf1,Good luck. RE: [Help] User Input Function returns Gibberish - lockhead - Jan 17, 2019 (Jan 16, 2019 04:50 PM)you_can_never_hack_me Wrote: I'm not goin' to help you with your idea, 'cause that is one of the most insecure ways in dealin' with inputs - in making functions. Security is not a factor. Also I really don't get the point you're trying to make RE: [Help] User Input Function returns Gibberish - you_can_never_hack_me - Jan 17, 2019 (Jan 17, 2019 03:40 AM)lockhead Wrote: Security is not a factor.LOL! Sure - on your planet! (Jan 17, 2019 03:40 AM)lockhead Wrote: Also I really don't get the point you're trying to makeI don't expect you to, kid. Good luck! RE: [Help] User Input Function returns Gibberish - lockhead - Jan 17, 2019 (Jan 17, 2019 03:54 AM)you_can_never_hack_me Wrote: LOL! Sure - on your planet! Helpful member of the community! +1 RE: [Help] User Input Function returns Gibberish - you_can_never_hack_me - Jan 17, 2019 (Jan 17, 2019 04:49 AM)lockhead Wrote: Helpful member of the community! +1Thank you! |