How to solve error 6011 when cheking for-loop condition? - c

I am new in C and tried checking the loop condition as to find on the internet, but I get this error I am not able to solve (no other questions/answers were helpful):
void main() {
char* insert = malloc(30);
printf("Insert a Molecular Formula:\n");
gets(insert);
if (insert) {
for (int i = 0; insert[i] != '\0'; i++) {
}
} }
I get the error 6011 in VS inside the for-loop when checking insert[i] != '\0'.
I haven't found a good fix, I have tried cheking return of malloc like if(!insert){ //above code here}
but this didn't help.
Thanks in advance.

Error C6011 is a warning, not an error, so your code will run, but it's not bad to handle these issues if Visual Studio is indicating them.
To get the warning to go away, fix your loop like so:
if (insert)
{
for (int i = 0; insert[i] != '\0'; i++) {
}
}

Related

Segmentation error, what is missing in my code?

I'm trying to get my code to convert a text file with 3 columns, xcoor, ycoor, and a symbol with 2 characters into a 30x30 map that prints the 2nd character of the symbol with the rest of the spaces being filled with a '.' However, my code doesn't seem to run, and I get a segmentation error when I try inputting the text file, what am I doing wrong? Thanks in advance
int main(void)
{
char grid[30][30];
for(int i=0;i<30;i++){
for(int j=0;j<30;j++){
grid[i][j]='.';
}
}
int xcoor,ycoor;
char symbol[2];
while((xcoor!=0)||(scanf("%d",&xcoor)))
{
while(xcoor==0){
scanf("%d",&xcoor);
}
scanf("%d %c%c",&ycoor,&symbol[0],&symbol[1]);
grid[xcoor-1][ycoor-1]=symbol[1];
}
for(int i=0;i<30;i++){
for(int j=0;j<30;j++){
printf("%c ",grid[i][j]);
}
printf("\n");
}
return 0;
}
This may not cover ALL of your errors, but immediately I see this:
int xcoor,ycoor;
char symbol[2];
while((xcoor!=0)
Do you think xcoor has a valid value right now? Should it? Because it doesn't. You've created a variable, then before actually setting it to anything, you are checking its value.
It's more than likely your scanf call that's giving you trouble. Regardless, try actually setting these variables. It will most likely fix your issues.
See here for more info: Is reading from unallocated memory safe?
You are using an uninitialized variable xcoor in the conditional of the while statement.
You can fix that by initializing xcoor.
More importantly, you can simplify the code for reading user data and the related error checks. Here's what I suggest:
while ( scanf("%d%d %c%c", &xcoor, &ycoor, &symbol[0], &symbol[1]) == 4 )
{
if ( xcoor < 0 || xcoor >= 30 )
{
// Deal with problem.
fprintf(stderr, "Out or range value of xcoor: %d\n", xcoor);
exit(1);
}
if ( ycoor < 0 || ycoor >= 30 )
{
// Deal with problem.
fprintf(stderr, "Out or range value of ycoor: %d\n", ycoor);
exit(1);
}
grid[xcoor-1][ycoor-1] = symbol[1];
}

SIGSEGV error on gcc (Ideone, codechef) works fine on pc

This is the code for a problem on codechef.
#include<stdio.h>
inline int chkIsDiv(int n1, char* ptr)
{
int i=0, b=0;
while(ptr[i] != '\0')
{
b=b*10+(ptr[i]-48);
b%=n1;
i++;
}
if(b==0)
return 1;
return 0;
}
int main()
{
int t, a, b=0, i;
char c[252];
scanf("%d",&t);
while(t>0)
{
scanf("%d",&a);
i=0;
getchar();
while((c[i++]=getchar()) !='\n');
c[i-1]='\0';
if(a!=0 && chkIsDiv(a,c)) printf("%d",a);
else if(a==0) {
i=0;
while(c[i] !='\0') printf("%d",c[i++]-48);
}
else
{
for(i=a-1; i>=1; i--)
{
if(a%i==0) {
if(chkIsDiv(i,c)) {
printf("%d",i);
break;
}
}
}
}
printf("\n");
t--;
}
//getch();
return 0;
}
The problem is when I run the above code on ideone, it compiles the code successfully, but when I put input test cases, it gives segmentation fault (SIGSEGV) Runtime Error.
My submission link on ideone: http://ideone.com/qGclvK
Similarly, on codechef when I submit my problem it gives same error.(I guess since both uses the same compiler from SPOJ).
But when I run the same code on my machine it works fine with every input condition specified in the problem and also for corner cases. The code is running fine in both windows and linux. And also I believe the algorithm I used is correct.
I used Dev-C++ default compiler in windows and gcc in linux.
I know the error is occurring due to some invalid memory reference, but i'm not able to find where the problem is, which statement is causing problem, as it is running fine on my system.
Can anyone help me with this, I'm kind of beginner??
[SOLVED] #thank_to_MayankJain.
Got it solved. The problem was that I was assuming a '\n' at the end of every line, but in this case the last line will not contain any '\n' so I tested for EOF marker now, and works fine.
Here's the modified code http://ideone.com/qGclvK
Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.
#MayankJain OK, solved now thanx. I was so dumb, didn't thought about it. The inputs are given from file and the last line will not contain any '\n'. Therefore I checked for EOF marker in last case. – abhishekkannojia

While loop not behaving as expected

I am working in an assignment and am experiencing some weird stuff. I have this while loop in my program that does not seem to branch into the for loop. I have placed two print statements and only the "1" prints over and over again. Note that this only happens when I compile and run from the linux terminal. Now what seem weird is that if i run the exact same code (while loop plus everything else) in Netbeans it seems to compile and behave as expected. Anyone know what might be wrong. Here is the code. I appreciate your help.
while(strstr(p,string_a)!= NULL)
{
p = trailerp + pholderp;
long int index = strstr(p,string_a) - (p+1); // -1 where it hits
printf("1");
for( i = 0; i <= index; i++)
{
printf("2");
p2[trailerp2] = pholderp[trailerp];
trailerp++;
trailerp2++;
if(i == index)
{
int j;
for(j=0; j <= lenb-1; j++) // insert the new string
{
p2[trailerp2] = string_b[j];
trailerp2++;
}
trailerp++;
}
}
}
Edit: I have found the problem. Netbeans seems to be broken in this OS.
This is because strstr(p,string_a) returns either p or 0 in this part:
long int index = strstr(p,string_a) - (p+1); // -1 where it hits
which results in index < 0 and prevents going into the loop.
You must print both p and string_a immediately before this statement to see what is going wrong there.

For-loop Syntax Error in Sqlite3.c

cppcheck has determined that the following statement produces a syntax error in sqlite3.c:
for(i=0; i<db->nDb; i++){
Full function:
SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
int i;
Btree *p;
assert( sqlite3_mutex_held(db->mutex) );
for(i=0; i<db->nDb; i++){
p = db->aDb[i].pBt;
if( p && p->sharable ){
assert( p->wantToLock>0 );
p->wantToLock--;
if( p->wantToLock==0 ){
unlockBtreeMutex(p);
}
}
}
}
I do not see how it is a syntax error. Please explain. Is this a false positive?
Looks like a false positive, however I can't reproduce it using Cppcheck 1.48 and C source code for SQLite 3.7.6.3.
If you're using different source or a different version, please log it as a bug.
CppCheck may be parsing the comparison expression incorrectly.
Try adding some spaces or parenthesis to help out,
Original:
i<db->nDb
Modified:
i < db->nDb
This is just my guess.

not declared in this scope creating a loop

I am tying to make a loop that repeats itself for 100 seconds, but I keep on getting the same error and I am getting so frustrated that even if it is the most basic thing I will not notice, could someone please tell me what I am doing wrong? I would really appreciate, thanks.
void loop(void) {
for ( int i = 0; i <= 100; i++) {
getFingerprintIDez();
delay (50)
}
}
uint8_t getFingerprintID() {
uint8_t attmpet = data.getImage();
switch (attempt) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_NOFINGER:
Serial.println("No fingerprint detected");
return attempt;
delay (500);
}
attempt = data.image2Tz();
switch (attempt) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return attempt;
}
attempt = data.fingerFastSearch();
if (attempt == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (attmpt == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return attempt;
}
Serial.print("Found ID #"); Serial.print(data.fingerID);
Serial.print(" with confidence of "); Serial.println(data.confidence);
return data.fingerID;
}
int getFingerprintIDez() {
uint8_t attempt = data.getImage();
if (attempt != FINGERPRINT_OK) return -1;
attempt = data.image2Tz();
if (attempt != FINGERPRINT_OK) return -1;
attempt = data.fingerFastSearch();
if (attempt != FINGERPRINT_OK) return -1;
Serial.print("Found ID #"); Serial.print(data.fingerID);
Serial.print(" with confidence of "); Serial.println(data.confidence);
return data.fingerID;
delay (1000);
}
The message I keep getting is:
exit status 1
'getFingerprintIDez' was not declared in this scope
Thank you all
In general, your indentation is a mess, and that's making it hard for you to see where the problem is. It looks to me like this line:
int getFingerprintIDez()
is a likely culprit for the error you're getting. I haven't counted braces, but I think your getFingerprintIDez() function might actually be defined inside the loop() function, and C doesn't allow that sort of thing.
So take care in formatting your code so that the various blocks are carefully indented the right amount -- C doesn't care about indentation, but it'll make it easier for you to see what blocks are inside what other blocks. Count open and close braces if you need to, and make sure that the definition of loop() ends before the definition of getFingerprintIDez() begins.
missing ; after delay(50)
unused variable 'attmpet'
'attmpt' was not declared in this scope
read the error messages from the first, not the last. the last error is only a consequence of the previous errors

Resources