A code that doesn't work
Jul 28, 2014 at 6:47am UTC
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
Jul 28, 2014 at 7:23am UTC
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.
Jul 28, 2014 at 8:01am UTC
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 Jul 28, 2014 at 8:03am UTC
Jul 28, 2014 at 8:12am UTC
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.
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.
Jul 28, 2014 at 8:41am UTC
cout <<strlen(audioBuffer);
outputs
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
Last edited on Jul 28, 2014 at 8:43am UTC
Topic archived. No new replies allowed.