Tag Archives: C language

The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648

Today, I encountered the need to use qsort function in the force deduction problem. As a result, there was no test case. The error reports are as follows

signed integer overflow: 0 – -2147483648 cannot be represented in type ‘int

Signed integer overflow: 0 — 2147483648 cannot be represented in type ‘Int’

The error was reported in the CMP function. At that time, I wondered if it was still within the range. I thought about it and found the problem

This is my original CMP function  

int cmp(const void *a,const void *b){
    return (*(const int*)a > *(const int*)b);
}

At this time, a is – 2147483648. Subtract B. as long as B is greater than 0, it overflows

terms of settlement:

Change the minus sign to the greater than sign

int cmp(const void *a,const void *b){
    return (*(const int*)a > *(const int*)b);
}

So it passed smoothly.

There are few questions about this on the Internet. Maybe it’s too stupid. Send a post to help Xiaobai like me

error: initializer element is not constant [How to Solve]

1. Background

C language compilation error: error: initializer element is not constant. The error code is as follows:

    char *info = (char *)malloc(len);
    static char *info_t = info;

The above error is caused by trying to initialize a static variable with the memory pointer allocated by malloc.

2. Error reason

Static variable is a global variable. The value of the global variable cannot be determined at compile time, but at run time (compilation principle). Therefore, the global variable cannot be initialized directly with the return value of malloc at the time of declaration. Replace with the following:

    char *info = (char *)malloc(len);
    static char *info_t; 
    info_t = info;

Compilation passed.

Debug error: abort() has been called error causes and Solutions

report errors

Error reporting reason

    pointer problems, such as illegal pointer access, pointer access out of bounds, memory leakage, memory out of bounds and insufficient memory allocation of multithreaded access resources. Check whether exe and DLL are mixed with different versions of CRT

    terms of settlement

      check that the requested space is not released. Check whether the stack space has been fully allocated. It is recommended that each memory allocation should not be too large, and remember that the release pointer points to an unexpected memory location

error: unknown type name ‘Q_DECLARE_METATYPE‘

Recently, when developing with QT4, there was a problem that Q could not be recognized_ DECLARE_ The error code of the metatype macro was transplanted from Qt5. I thought it was not supported by QT4. After checking later, I found that the qmetatype. H header file must be included in QT4, and only qvariant needs to be included in Qt5.

[Solved] Dev-C++ [Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode

When writing C language program with DEV-C + +:

[Error] ‘for’ loop initial declarations are only allowed in C99 or C11 mode

Variables declared in the for loop are only allowed in C99 or C11 mode. You need to select C99 in the language standard under tools/compiler option/code generation.

The results are as follows:

Qttssessionbegin failed, error code: 25000 solution for Ubuntu development of iFLYTEK voice wake-up function

        The official error code is described as “the engine is not initialized”, so please first check whether it has been initialized, and then confirm that the link to libmsc.os and libw is entered at compile time_ ivw.os。 At this time, if 25000 is still reported as initialization error, you can set variables   LD_ LIBRARY_ Path solution:

1 open the. Bashrc file

sudo ./bashrc

2 add at the end of the file

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/PATH

Replace/path with libmsc.os and libw_ Ivw.os is the directory of two dynamic libraries. Save the changes.

Then close the command line window and reopen it to run.

How to Solve Nginx cross compilation Error

1.checking for C compiler … found but is not working
Modify auto/feature
find if [ -x $NGX_AUTOTEST ]; then
Assign the value in front of ngx_feature_run=no
2./configure: 1: auto/types/sizeof: objs/autotest: Exec format error
change auto/types/sizeof
find ngx_test=”$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
-o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs”
change $CC to gcc
3.objs/src/os/unix/ngx_user.o: In function `ngx_libc_crypt’: ngx_user.c:(.text+0x20): undefined reference to `crypt’
Find the ngx_libc_crypt function in src/os/unix/ngx_user.c:
change value = crypt((char *) key, (char *) salt);
to
value = DES_crypt((char *) key, (char *) salt);

Solution to the segmentation fault of single chain table in C language

The title is a collection of single linked list operations, including the establishment, deletion, insertion, printing, release and other operations of linked list.

The code runs correctly in CodeBlocks, and an error is displayed when it is submitted to the OJ platform of the school.

The segmentation fault section is wrong, mainly because the array is out of bounds, the pointer is abnormal, and the memory area that should not be accessed is accessed. Single linked list mainly involves pointer operation. After checking that the array is correct (not used in this topic), it mainly checks the pointer.

This is the previous code:

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

typedef struct student
{
    long int num;
    float score;
    struct student *next;
}Node,*Link;

Link creatlink(void)
{
    Link head,node,rear;
    long int p;
    float q;
    head = (Link)malloc(sizeof(Node));
    head -> next = NULL;
    rear = head;
    while(scanf("%ld %f",&p,&q))
    {
        if(p == 0&&q == 0)
        {
            break;
        }
        node = (Link)malloc(sizeof(Node));
        node -> num = p;
        node -> score = q;
        rear -> next = node;
        rear = node;
    }
    rear -> next = NULL;
    return head;
}

void printlink(struct student *head)
{
    Link p;
    p = head -> next;
    while(p != NULL)
    {
        printf("%ld %.2f\n",p -> num,p -> score);
        p = p -> next;
    }
}

Link dellink(Link head,long int k)
{
    if(head == NULL||head -> next == NULL)
        exit(0);
    Link p,q;
    p = head -> next;
    q = head;
    while(p != NULL)
    {
        if(p -> num == k)
        {
            q -> next = p -> next;
            free(p);
        }
        else
        {
            q = p;
            p = p -> next;
        }
    }
    return head;
}

Link insertlink(Link head,Link stu)
{
    Link p,node,q;
    p = head;
    q = p -> next;
    while(q != NULL&&q -> num < stu -> num)
    {
        p = p -> next;
        q = p -> next;
    }
    if(q == NULL)
        p -> next = NULL;
    node = (Link)malloc(sizeof(Node));
    node -> num = stu -> num;
    node -> score = stu -> score;
    node -> next = p -> next;
    p -> next = node;
    return head;
}

void freelink(Link head)
{
    Link p;
    while(p != NULL)
    {
        p = head;
        head = head -> next;
        free(p);
    }
}

int main()
{
    struct student *createlink(void);
    struct student *dellink(struct student *, long);
    struct student *insertlink(struct student *, struct student *);
    void printlink(struct student *);
    void freelink(struct student *);
    struct student *head, stu;
    long del_num;
    head= creatlink();
    scanf("%ld", &del_num);
    head= dellink(head, del_num);
    scanf("%ld%f", &stu.num, &stu.score);
    head= insertlink(head, &stu);
    scanf("%ld%f", &stu.num, &stu.score);
    head= insertlink(head, &stu);
    printlink(head);
    freelink(head);
    return 0;
}

After excluding the error of other functions, it is finally found that the error occurs in the most insignificant release linked list function. It is found that the while loop end flag is wrong, and the head is always one step faster than the P pointer. If P= If NULL is the loop end flag, the loop is completed when the point pointed by the head is null and P points to the last node. At this time, P is not null, so the loop has to continue and the head has to move back. At this time, the head pointer becomes a wild pointer and has no point, so an error will be reported.

After modification, transpose the while loop end flag to head= Null, when p points to the last node and head is null, the node pointed to by P will be released, and the loop will end without wild pointer.

The modified release single linked list function is:

void freelink(Link head)
{
    Link p;
    while(head != NULL)
    {
        p = head;
        head = p -> next;
        free(p);
    }
}

After modification, submit it to OJ platform again and pass it correctly.

Exception from HRESULT: 0x80041FE2

Installation vs community version, new project appears:

Exception from HRESULT: 0x80041FE2

Reasons for the problem:
when installing, the Linux related environment was selected. When creating the project, the visual c + + project was created, because these components were not installed, an error was reported
Creating Linux related projects will not.

#During OpenGL development, fatal error C1083: can’t open include file: “GL / glut. H”: no such file or directory

During OpenGL development, fatal error C1083: can’t open include file: “GL/glut. H”: no such file or directory

I use vs2012 to write a simple OpenGL program, which always prompts when running:

Fatal error C1083: unable to open include file: “GL/glut. H”: no such file or directory;

Or fatal error C1083: cannot open include file: “GL/glaux. H”: no such file or directory;

At this time, the corresponding operations are as follows:

1. Download glut Toolkit: glut_ 37beta. Rar
2. Untie the downloaded compressed package and you will get five files, including glu32. Lib; glut32.dll,glut32.lib; Glut.dll, glut. H, glut. Lib
3. Put the extracted glut. H and glux. H in the folder C: program files (x86) and windows kits (8.0) and include (UM)( Note: the directory where each person installs vs is different, and the operation is based on the actual situation)
4. Put the extracted glut.lib, glut32.lib and glaux.lib in the static function library, which is located in the folder C: program files (x86) – Windows kits (8.0) – lib (win8) – um (x86)
5. If it still can’t work, put glut32.dll back to C: Windows/syswow64
6. Compile again. If there is still an error, change the original name of the header file # include & lt; GL/glut.h> Include & lt; glut.h>。
///

Download address of glut in Windows Environment: (about 150k in size)

http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

This is the first article I saw when I configured the environment. Later, I made a mistake and used the method of this article to solve the problem https://www.pianshen.com/article/78231370291/

How to Solve Int Data overflow error

int type maximum ten digits 2147483647
int m=1;
int n=1345;
for(m=1;m<=1000000000;m*=10)
{ int d=n/m%10; printf(“%d\n “,d); } will report an error overflow, because the maximum m is, and then execute 1000000000*10 to overflow

 

Improve

int m=1;
int n=1345;
for(m=1000000000;m>
=1;m/=10)
{ int d=n/m%10; printf(“%d\n”,d); } this Hour output from high output 1 3 4 5

C language problem: 0xc0000005: access conflict occurred when writing to location 0xffffcc.

Recently, I began to learn C language systematically and use scanf in vs2019_ An error occurred when s assigned a value to a string. The error is as follows:
the exception raised at the location of 0x7837ef8c (ucrtbased. DLL) (located in project2.exe): 0xc0000005: an access conflict occurred when writing to the location of 0x01342000
*
after querying some data, we find that this problem should be solved because scanf is encouraged in vs2019 compiler_ S function to prevent the original scanf function array out of bounds( Using scanf function in vs2019 will report an error (compilation failed)

#define WORD_SIZE 26
char name[WORD_SIZE];
scanf_s("%s",name);

If so, an error will be reported.

If we provide the array name and length, we can compile it.

#define WORD_SIZE 26
char name[WORD_SIZE];
scanf_s("%s",name,WORD_SIZE);