How to fix 'access past the end of a local variable' in cHow do I use extern to share variables between source files?Improve INSERT-per-second performance of SQLite?Printing Arrays in separate Function in CHow Backtrace works on Linux x86_64?In C, unable to get an array to save new valuesBuffer overflow that overwrites local variablesModern stack-overflow (stack smash) exploit in C on Linux?Which return address am I corrupting?How to properly sum the elements of an array w/out getting large random outputsHow to fix pass by reference not changing values properly
What was Bran's plan to kill the Night King?
How can internet speed be 10 times slower without a router than when using a router?
Can muons decay into quarks?
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
What do I do if my advisor made a mistake?
Is Soreness in Middle Knuckle of Fretting Hand Index Finger Normal for Beginners?
Manager is threatening to grade me poorly if I don't complete the project
What do "Sech" and "Vich" mean in this sentence?
Is the book wrong about the Nyquist Sampling Criterion?
How can I get people to remember my character's gender?
Nested loops to process groups of pictures
Should I simplify my writing in a foreign country?
Why do people keep telling me that I am a bad photographer?
Typeset year in old-style numbers with biblatex
getline() vs. fgets(): Control memory allocation
When an imagined world resembles or has similarities with a famous world
Which Sphere is Fastest?
Latex & Markdown files
How to pass hash as password to ssh server
Is Benjen dead?
Would a small hole in a Faraday cage drastically reduce its effectiveness at blocking interference?
What is the closest airport to the center of the city it serves?
Is there an age requirement to play in Adventurers League?
To kill a cuckoo
How to fix 'access past the end of a local variable' in c
How do I use extern to share variables between source files?Improve INSERT-per-second performance of SQLite?Printing Arrays in separate Function in CHow Backtrace works on Linux x86_64?In C, unable to get an array to save new valuesBuffer overflow that overwrites local variablesModern stack-overflow (stack smash) exploit in C on Linux?Which return address am I corrupting?How to properly sum the elements of an array w/out getting large random outputsHow to fix pass by reference not changing values properly
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I made a function that checks to see if 6 elements of an array are equal, and if they are it changes a value of a different array. It compiles, but when I run it I get a 'stack buffer overflow' and my compiler says that my array is unintialized even though I initialized it.
I tried redefining the whole array in the function but that gave and "expected expression" error.
int match(int numbers[], int matchHighest)
int matchArray[] = 0, 0, 0, 0, 0, 0;
int i = 0;
match6(&numbers[i], &matchArray[5]);
matchHighest = matchArray[5];
return matchHighest;
int match6 (int numbers[], int matchArray[5])
int i=0;
matchArray[5] = 0;
while((numbers[i]==numbers[i+1]) && (i<4))
i++;
if(i == 4)
matchArray[5] = 6 * numbers[0] + 27;
return matchArray[5];
I want it to change the value of matchArray[5] if the if statement is met.
Edit:
My main is:
int main(void)
int numbers[6]=0,0,0,0,0,0;
int matchHighest = 0;
inputArray(numbers);
match(numbers, matchHighest);
printf("test worked n");
return 0;
c
add a comment |
I made a function that checks to see if 6 elements of an array are equal, and if they are it changes a value of a different array. It compiles, but when I run it I get a 'stack buffer overflow' and my compiler says that my array is unintialized even though I initialized it.
I tried redefining the whole array in the function but that gave and "expected expression" error.
int match(int numbers[], int matchHighest)
int matchArray[] = 0, 0, 0, 0, 0, 0;
int i = 0;
match6(&numbers[i], &matchArray[5]);
matchHighest = matchArray[5];
return matchHighest;
int match6 (int numbers[], int matchArray[5])
int i=0;
matchArray[5] = 0;
while((numbers[i]==numbers[i+1]) && (i<4))
i++;
if(i == 4)
matchArray[5] = 6 * numbers[0] + 27;
return matchArray[5];
I want it to change the value of matchArray[5] if the if statement is met.
Edit:
My main is:
int main(void)
int numbers[6]=0,0,0,0,0,0;
int matchHighest = 0;
inputArray(numbers);
match(numbers, matchHighest);
printf("test worked n");
return 0;
c
1
Please give us your main as well.
– brothir
Mar 23 at 0:56
1
This code seems to be a bit confused about whethermatchArraycontains 5 members or 6. What are you intending here? Also, why do you call two different thingsmatchArray? That's almost certainly creating serious confusion.
– David Schwartz
Mar 23 at 1:03
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15
add a comment |
I made a function that checks to see if 6 elements of an array are equal, and if they are it changes a value of a different array. It compiles, but when I run it I get a 'stack buffer overflow' and my compiler says that my array is unintialized even though I initialized it.
I tried redefining the whole array in the function but that gave and "expected expression" error.
int match(int numbers[], int matchHighest)
int matchArray[] = 0, 0, 0, 0, 0, 0;
int i = 0;
match6(&numbers[i], &matchArray[5]);
matchHighest = matchArray[5];
return matchHighest;
int match6 (int numbers[], int matchArray[5])
int i=0;
matchArray[5] = 0;
while((numbers[i]==numbers[i+1]) && (i<4))
i++;
if(i == 4)
matchArray[5] = 6 * numbers[0] + 27;
return matchArray[5];
I want it to change the value of matchArray[5] if the if statement is met.
Edit:
My main is:
int main(void)
int numbers[6]=0,0,0,0,0,0;
int matchHighest = 0;
inputArray(numbers);
match(numbers, matchHighest);
printf("test worked n");
return 0;
c
I made a function that checks to see if 6 elements of an array are equal, and if they are it changes a value of a different array. It compiles, but when I run it I get a 'stack buffer overflow' and my compiler says that my array is unintialized even though I initialized it.
I tried redefining the whole array in the function but that gave and "expected expression" error.
int match(int numbers[], int matchHighest)
int matchArray[] = 0, 0, 0, 0, 0, 0;
int i = 0;
match6(&numbers[i], &matchArray[5]);
matchHighest = matchArray[5];
return matchHighest;
int match6 (int numbers[], int matchArray[5])
int i=0;
matchArray[5] = 0;
while((numbers[i]==numbers[i+1]) && (i<4))
i++;
if(i == 4)
matchArray[5] = 6 * numbers[0] + 27;
return matchArray[5];
I want it to change the value of matchArray[5] if the if statement is met.
Edit:
My main is:
int main(void)
int numbers[6]=0,0,0,0,0,0;
int matchHighest = 0;
inputArray(numbers);
match(numbers, matchHighest);
printf("test worked n");
return 0;
c
c
edited Mar 23 at 1:09
Peter Brown
asked Mar 23 at 0:53
Peter BrownPeter Brown
133
133
1
Please give us your main as well.
– brothir
Mar 23 at 0:56
1
This code seems to be a bit confused about whethermatchArraycontains 5 members or 6. What are you intending here? Also, why do you call two different thingsmatchArray? That's almost certainly creating serious confusion.
– David Schwartz
Mar 23 at 1:03
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15
add a comment |
1
Please give us your main as well.
– brothir
Mar 23 at 0:56
1
This code seems to be a bit confused about whethermatchArraycontains 5 members or 6. What are you intending here? Also, why do you call two different thingsmatchArray? That's almost certainly creating serious confusion.
– David Schwartz
Mar 23 at 1:03
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15
1
1
Please give us your main as well.
– brothir
Mar 23 at 0:56
Please give us your main as well.
– brothir
Mar 23 at 0:56
1
1
This code seems to be a bit confused about whether
matchArray contains 5 members or 6. What are you intending here? Also, why do you call two different things matchArray? That's almost certainly creating serious confusion.– David Schwartz
Mar 23 at 1:03
This code seems to be a bit confused about whether
matchArray contains 5 members or 6. What are you intending here? Also, why do you call two different things matchArray? That's almost certainly creating serious confusion.– David Schwartz
Mar 23 at 1:03
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15
add a comment |
2 Answers
2
active
oldest
votes
You're passing &matchArray[5], which is the address of the 5th element of matchArray as the matchArray argument of match6. Since match's matchArray only has 6 elements, this is a slice of only 1 element, which is too small.
You probably want to pass matchArray -- a pointer to the 0th element, in which case you'll get a slice of all 6 elements. Then you also probably want to declare the argument to match6 as int matchArray[6]
add a comment |
matchArray is an array of 6 elements but the parameter is declared as having only 5 int matchArray[5]. So the assignment within the called function matchArray[5] = 0 appears to be out of bounds given how the parameter is declared.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309570%2fhow-to-fix-access-past-the-end-of-a-local-variable-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're passing &matchArray[5], which is the address of the 5th element of matchArray as the matchArray argument of match6. Since match's matchArray only has 6 elements, this is a slice of only 1 element, which is too small.
You probably want to pass matchArray -- a pointer to the 0th element, in which case you'll get a slice of all 6 elements. Then you also probably want to declare the argument to match6 as int matchArray[6]
add a comment |
You're passing &matchArray[5], which is the address of the 5th element of matchArray as the matchArray argument of match6. Since match's matchArray only has 6 elements, this is a slice of only 1 element, which is too small.
You probably want to pass matchArray -- a pointer to the 0th element, in which case you'll get a slice of all 6 elements. Then you also probably want to declare the argument to match6 as int matchArray[6]
add a comment |
You're passing &matchArray[5], which is the address of the 5th element of matchArray as the matchArray argument of match6. Since match's matchArray only has 6 elements, this is a slice of only 1 element, which is too small.
You probably want to pass matchArray -- a pointer to the 0th element, in which case you'll get a slice of all 6 elements. Then you also probably want to declare the argument to match6 as int matchArray[6]
You're passing &matchArray[5], which is the address of the 5th element of matchArray as the matchArray argument of match6. Since match's matchArray only has 6 elements, this is a slice of only 1 element, which is too small.
You probably want to pass matchArray -- a pointer to the 0th element, in which case you'll get a slice of all 6 elements. Then you also probably want to declare the argument to match6 as int matchArray[6]
answered Mar 23 at 1:00
Chris DoddChris Dodd
83.1k783163
83.1k783163
add a comment |
add a comment |
matchArray is an array of 6 elements but the parameter is declared as having only 5 int matchArray[5]. So the assignment within the called function matchArray[5] = 0 appears to be out of bounds given how the parameter is declared.
add a comment |
matchArray is an array of 6 elements but the parameter is declared as having only 5 int matchArray[5]. So the assignment within the called function matchArray[5] = 0 appears to be out of bounds given how the parameter is declared.
add a comment |
matchArray is an array of 6 elements but the parameter is declared as having only 5 int matchArray[5]. So the assignment within the called function matchArray[5] = 0 appears to be out of bounds given how the parameter is declared.
matchArray is an array of 6 elements but the parameter is declared as having only 5 int matchArray[5]. So the assignment within the called function matchArray[5] = 0 appears to be out of bounds given how the parameter is declared.
answered Mar 23 at 0:57
jspcaljspcal
41k45264
41k45264
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309570%2fhow-to-fix-access-past-the-end-of-a-local-variable-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please give us your main as well.
– brothir
Mar 23 at 0:56
1
This code seems to be a bit confused about whether
matchArraycontains 5 members or 6. What are you intending here? Also, why do you call two different thingsmatchArray? That's almost certainly creating serious confusion.– David Schwartz
Mar 23 at 1:03
I meant for matchArray to have 6 members, but I wanted to change the value of the 5th element
– Peter Brown
Mar 23 at 1:11
5th element has index 4.
– Kamil Cuk
Mar 23 at 1:15