0

Okay, I am very confused and desperately need help. I'm working on an assignment to dynamically allocate an array of strings, and I've been having trouble with realloc the entire time. I finally got it to the point where it seemed to be working, I just needed to write the for-loop to properly display the results.

Except then, the code would stop working. It'd let me put in the string, the exact same string I'd been using to test it before, and the program would crash saying that realloc was having an issue with an invalid pointer. Whenever I comment the for-loop out again, everything seems to be working just fine.

This is probably either something dumb or the result of something I don't understand about realloc, but I'm running out of time and various searches haven't returned any answers I think are applicable, so i figured I'd ask here.

The input I've been using to test it has been the line "hello my one true friend".

Here's my code (Yes, I know, it's a mess, sorry. Normally I'm a little better about these things but I've been in a very big hurry):

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int MAXSTRING = 255;

int makearg(char s[], char **args[]);

int main()
{
   char **tokenArray;
   char strInput[MAXSTRING];
   int tokenResult;
   int i = 0;

   printf("Input String to be Parsed: ");
   scanf("%[^\n]%*c", strInput);

   tokenResult = makearg(strInput, &tokenArray);

   printf("argc: %d\n", tokenResult);
   for (i < tokenResult; i++;)
   {
      printf("arg(%d): %s\n", i, tokenArray[i][1]);
   }
}

int makearg(char s[], char **args[])
{
   int numTokens = 0;
   int lastSpace = 0;
   int i = 0;
   char token[MAXSTRING];
   int subFromPos = 0;
   while ((s[i] != '\n') && (s[i] != '\0'))
   {
      token[i - lastSpace - subFromPos] = s[i];
      if (s[i] == ' ')
      {
         token[i - lastSpace] = '\0';
         *args = realloc(*args, (numTokens + 1));
         //printf("the seg fault hasnt happened yet 1\n");
         args[numTokens] = NULL;
         args[numTokens] = realloc(args[numTokens], (i - lastSpace + 1));
         //printf("the seg fault hasnt happened yet 2\n");
         *args[numTokens] = token;
         printf("Saved Token: %s\n", *args[numTokens]); //test to see if the token got written properly
         numTokens++;
         lastSpace = i;
         subFromPos = 1;
      }
      //printf("%c\n", s[i]);
      //printf("Token: %s\n", token);
      //printf("the seg fault hasnt happened yet\n");
      i++;
   }
   numTokens++;
   return numTokens;
}
  • 2
    You should check the syntax of a `for` loop again: `for (i < tokenResult; i++;)` Check again, what is the role of `i – Gerhardh Feb 22 '22 at 07:13
  • What is your input? [Edit] and clarify. – Jabberwocky Feb 22 '22 at 07:40
  • 1
    What did your debugging session reveal? When exactly does the crash occur? -- Side note: Especially when you're in a hurry, you should strive to write good code, as it shows here. Bad code leads to unnecessary errors that take much more time to resolve than doing it right. – the busybee Feb 22 '22 at 07:59
  • For SO to work properly, you are supposed to respond to comments. – Gerhardh Feb 22 '22 at 10:56
  • @Gerhardh what do you mean i – Ian Finnigan Feb 22 '22 at 19:48
  • that for loop is wrong `for (i < tokenResult; i++;)` – pm100 Feb 22 '22 at 19:49
  • @IanFinnigan *You* think that `i – Steve Summit Feb 22 '22 at 20:27
  • A `for` loop is driven by three expressions, which we might call *initialize*, *test*, and *increment*. For example, in `for(i = 0; i < 10; i++)`, the initialization expression is `i = 0`, the test expression is `i < 10`, and the increment expression is `i++`. Now, in your loop `for(i < tokenResult; i++; )`, what are the three expressions? – Steve Summit Feb 22 '22 at 20:29
  • If you think, `i – Gerhardh Feb 23 '22 at 07:48

1 Answers1

0

You have not initialized tokenarray

char **tokenArray;

so when you call realloc on it you are invoking UB. the argument to realloc must either be NULL or a valid heap pointer. Do this

char **tokenArray = NULL;

and fix that for loop

pm100
  • 48,078
  • 23
  • 82
  • 145