0

I am working on a small project and I am absolutely stuck. The purpose of the function I'm working on is to rearrange and change a Cstring based on a few preset rules. Where my issue lies is within the second portion of my swapping algorithm I came up with.

for(int i = 0; i < len; i++)
{
  if(sentence[i] == SPACE)
  {
    space++;
    spacePlace[counter] = i;
    counter++;
  }
}
for(int i = 0; i < space; i++)
{
  if(i == 0)
  {
    count2 = 0;
    for(int h = 0; h < 20; h++)
    {
      temp1[h] = NUL;
      temp2[h] = NUL;
    }

    for(int j = 0; j < spacePlace[0]; j++)
      temp1[j] = sentence[j];
    for(int m = spacePlace[0]; m < spacePlace[1]; m++)
    {
      temp2[count2] = sentence[m];
      count2++;
    }
.
. 
.

the first for loops executes perfectly and the output is great, but the second for loop always messes up and ends up sending me a stack smashing error. For more reference, sentence is a cstring passed to the function, and temp1 and temp2 are also cstrings. Any help or points in the right direction would be a godsend. Thanks!

dirtydan
  • 1
  • 1
  • ...do you have a constant `ZERO` and `ONE`? – François Andrieux Nov 03 '17 at 15:42
  • yes in my header file i have ZERO = 0 and ONE = 1. the outline for the project requires literally everything to be constants and they are defined as such. – dirtydan Nov 03 '17 at 15:43
  • 2
    That is unfortunate. But about the question, you should provide a [MCVE]. The present snippet doesn't seem to be sufficient to identify the problem. – François Andrieux Nov 03 '17 at 15:44
  • 2
    I feel bad for you if you have to do `#define ZERO 0`. – Hatted Rooster Nov 03 '17 at 15:47
  • You're most likely writing outside the bounds of a local array. Add some bounds-checking. – molbdnilo Nov 03 '17 at 16:14
  • That's just the thing though. Above it in the code I have bound checking and clear the contents of whatever was in the array before. The big issue im getting is that an input of say "You" passes perfectly fine through the first for loop and to temp1, but if i pass the same "you" or something like "if" to it, it will take it but also pin on whatever the contents of temp1 are and some random characters. – dirtydan Nov 03 '17 at 16:18
  • Re your edit: in `for(int h = 0; h < 20; h++)`, `20` is a "magic number", but `0` isn't. – molbdnilo Nov 03 '17 at 16:18
  • 2
    Have you tried stepping through the code with a debugger? – Cameron Nov 03 '17 at 16:18
  • I've tried to use GDB but got confused and couldn't tell where it was going wrong – dirtydan Nov 03 '17 at 16:20
  • @dirtydan There is no bounds checking on any of your array assignments. – molbdnilo Nov 03 '17 at 16:22
  • What would be the best way to go about that then? The max size of each temp array is hard capped to be 20. It seems like its still within the bounds but there's a good chance I'm missing something simple. I've been up all night doing this thing. – dirtydan Nov 03 '17 at 16:27
  • What is the value of `spacePlace[0]` ? If it is more than 19 then it would try to write to memory outside of temp1. – nyemul Nov 03 '17 at 16:36
  • Can count2 exceed 19? If that is the case then again it would be outside of temp2. – nyemul Nov 03 '17 at 16:38
  • spacePlace[0] is 3, and spacePlace[1] is 6. the difference between each of the spacePlace values is designed to be less than 20. Well, in theory. Idk why this is making me struggle so hard – dirtydan Nov 03 '17 at 16:39
  • you never know, ZERO might be 42 in some cases, slippery things these numbers – pm100 Nov 03 '17 at 16:44

0 Answers0