[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Don't yell at me.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 8
Thread images: 1

File: please help.png (107KB, 792x1023px) Image search: [Google]
please help.png
107KB, 792x1023px
I am not an expert with C by any means and I need some assistance.
When I run this code it crashes and says that realloc has an invalid next size.
This issue is most likely related to the new_command function but I cant figure it out.
Please help me /g/ I need to learn (I would post in wsr but they dont have code tags)
Pls no bully.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
char* alias;
void (*function)();
}command;

command* global_commands;
int total_commands = 0;

void echo()
{
puts("hello");
}

void execute(char* command_alias)
{
for(int current_command = 0; current_command < total_commands; current_command++)
{
if(!strcmp(global_commands[current_command].alias, command_alias))
{
puts("Exists, running...");
global_commands[current_command].function();
break;
}
}
}

void new_command(char* new_alias, void* new_function)
{
command new_command = {new_alias, new_function};
if(!(global_commands = realloc(global_commands, sizeof(global_commands) + sizeof(new_command))))
{
puts("Unable to add command!");
return;
}
global_commands[total_commands] = new_command;
total_commands++;
}

char* getstr()
{
char* str = calloc(sizeof(char), 1);
char character;
while((character = getchar()) != '\n')
asprintf(&str, "%s%c", str, character);
return str;
}

int main()
{
if(!(global_commands = malloc(sizeof(command))))
{
puts("Failed to allocate space for command list!");
return 1;
}
fprintf(stdout, "> ");
new_command("exit", echo);
new_command("test", echo);
new_command("cd", echo);
for(;;)
{
char* command_alias = getstr();
execute(command_alias);
free(command_alias);
fprintf(stdout, "> ");
}
}
>>
>>58700919
This doesn't demand a thread of its own.
Use these instead:
>>58697071
>>58694939
>>
>>58700919
Can you use a debugger? If you can, use it. If not, you should learn.
>>
global_commands is always the same size, learn the difference between arrays and pointers.

oh, and use valgrind. you will see that the issue happens much earlier than the point of the crash.
>>
>>58700949
sorry, ill keep this in mind in the future.
>>58700957
no, but ill do that.
>>58700959
if you don't mind can you elaborate, sorry if im asking too much.
>>
>>58700919
sizeof(global_commands) + sizeof(newcommand) is a constant (size of a pointer + the struct), you never grow the array.
>>
>>58700978
>>no, but ill do that.
it's really one of the most important habits to pick up. When you're writing small toy programs you can solve problems in your head, or by filling your code with print statements to see what variables are. But this doesn't scale well. To work on programs of any significant size, a proper debugger is absolutely invaluable, and too many new programmers are too intimidated by them to use them.

If you're a Winfag Visual Studio has a nice one built in. If you're on Linux, gdb is the cousin to gcc. It's command-line, and you can either use it directly like that, or get a graphical front-end for it, of which there are many to choose from.
>>
>>58701052
Yeah I only use linux for the most part, so ill learn gdb.
Thread posts: 8
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.