#filling array of strings

22 messages · Page 1 of 1 (latest)

wicked halo
#

hi i trying fill 2d array with strings (i want split a big input string by paragraphs) but i get abort trap 6:

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

int main() {
  char* input = getenv("AOC_INPUT");

  const char* paragraphs[8];
  int nextParagraphIndex = 0;

  const char* delimiter = "\n\n";

  char* paragraph = strtok(input, delimiter);

  while (paragraph != NULL) {
    paragraphs[nextParagraphIndex] = paragraph;
    ++nextParagraphIndex;
    paragraph = strtok(NULL, delimiter);
  }
}
shut otterBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

silk finch
#

In your while loop you should be checking for the null terminator ‘\0’

#

Oh never mind, you’re not looping through every character

vivid dirge
#

Where is your 2d array?
All you have is a 1d array of size 8 of char pointers.
I mean yes, that is a possible way to store strings, but it's fundamentally different from a 2d array.

Since I'm on my phone and can't test right now, please post the full error message. If that was the full error message then please compile with -fsanitize=address for a potentially better error message

silk finch
#

Yea I didn’t read it all the way

wicked halo
#

oh yes i meant array 1d of string

wicked halo
# vivid dirge Where is your 2d array? All you have is a 1d array of size 8 of char pointers. I...
main(65008,0x10e020600) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
=================================================================
==65008==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ff7ba3876a0 at pc 0x000105b79d76 bp 0x7ff7ba387630 sp 0x7ff7ba387628
WRITE of size 8 at 0x7ff7ba3876a0 thread T0
    #0 0x105b79d75 in main+0x165 (main:x86_64+0x100003d75)
    #1 0x10dfa552d in start+0x1cd (dyld:x86_64+0x552d)

Address 0x7ff7ba3876a0 is located in stack of thread T0 at offset 96 in frame
    #0 0x105b79c1f in main+0xf (main:x86_64+0x100003c1f)

  This frame has 1 object(s):
    [32, 96) 'paragraphs' <== Memory access at offset 96 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow (main:x86_64+0x100003d75) in main+0x165
Shadow bytes around the buggy address:
  0x1ffef7470e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470e90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470ea0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470eb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470ec0: 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00 00 00
=>0x1ffef7470ed0: 00 00 00 00[f3]f3 f3 f3 00 00 00 00 00 00 00 00
  0x1ffef7470ee0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470ef0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470f10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1ffef7470f20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
#
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==65008==ABORTING
sh: line 1: 65008 Abort trap: 6           ./main```
#

filling array of strings

vivid dirge
#

Put a print (and a fflush(stdout); afterwards to ensure the print happens) to observe how large your nextParagraphIndex value grows.

I have a feeling the array size of 8 may not be sufficiently large.

wicked halo
#

ok i tried const char** paragraphs = malloc(8 * sizeof(char*)); and it work thakns!

#

i didnt know malloc exist

#

!sloved

vivid dirge
#

How does that work but the version before didn't?

vivid dirge
shut sleet
#

strtok is going to modify input, so depending on where input points, you may have a write access permission problem. I am not certain about behaviour guarantees when modifying memory returned by getenv().

Secondarily the second parameter of strtok() is not a delimiter string, it is a list of delimiter characters.

If this was your input:

abc\n\ndef\nghi\njkl\n\n\n\nmno\npqr\nstu\n

Your loop would return an an array of pointers into this same string that would now look like this:

(gdb) inspect input
$3 = "abc\000\ndef\000ghi\000jkl\000\n\n\nmno\000pqr\000stu\000"

And your paragraphs array would look like this:

(gdb) inspect paragraphs
$4 = {0x7fffffffe470 "abc", 0x7fffffffe475 "def", 0x7fffffffe479 "ghi", 0x7fffffffe47d "jkl", 0x7fffffffe484 "mno", 0x7fffffffe488 "pqr", 0x7fffffffe48c "stu", 0x0}

This finds all non-null strings delimited by newlines rather than the paragraphs.

wicked halo
#

bruh i just want to fill an arrya with strings how is this not a solved problem 😭

shut sleet
#

no worries mate. if you are happy with the strings you got, good on-ya. It just doesn't match the description of the problem you provided. Disregard.

wicked halo
#

!solved