A code that doesn't work

Hello
.My program doesn't behave as I expect it.
This code doesn't prinnt 'c', why?
1
2
3
4
5
6
    char **word_music  = new char *[strlen(audioBuffer)];
    for(int k=0;k<strlen(audioBuffer); ++k)
      word_music[k] = new char[strlen(audioBuffer)];
      
      word_music[50000][50000] = 'c';
      cout << word_music[50000][50000];


Thanks in advance
Impossible to say for sure since you don't provide the necessary context, but it's a reasonable guess that word_music[50000][50000] is out of bounds.
then it would produce a fault?

OK, I changed it to:
1
2
      word_music[50][50] = 'c';
      cout << word_music[50][50];


Nothing prints yet
Last edited on
then it would produce a fault?

Then whatever happens is a product of undefined behavior, and while that could produce a segmentation fault it isn't required to.


OK, I changed it to:

Making random changes and hoping they reveal something isn't usually productive. What is the value returned by strlen(audioBuffer)? That looks rather suspicious since audio buffers are not typically C-style strings.
cout <<strlen(audioBuffer);
outputs
50972

and audioBuffer is defined as:
char audioBuffer[120000] = {0};

EDIT:
1
2
3
4
5
6
7
8
cout <<strlen(audioBuffer);
    char **word_music  = new char *[strlen(audioBuffer)];
    for(int k=0;k<strlen(audioBuffer); ++k)
      word_music[k] = new char[100];
      
      word_music[50][50] = 'c';
      cout << word_music[50][50];
        

outputs
50972c
Last edited on
Topic archived. No new replies allowed.