小编Mik*_*ike的帖子

创建文件时出现分段错误11

我正在做一个简单的项目,但我遇到了一个错误.我在Unix中编码并使用终端执行代码.

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
    int atis;
    char *weather;

    //Création du fichier ATIS
    if((atis = creat("atis", 0666)) == -1)
    {
        printf("Error while creating the ATIS file\n");
        exit(-1);
    }

    //Ouverture du fichier ATIS
    if((atis = open("atis", 0666)) == -1)
    {
        printf("Permission denied\n");
        exit(-1);
    }

    //Mise à jour du fichier ATIS
    printf("OK or KO for a take-off? ");
    gets(weather);
    if(write(atis, weather, sizeof(weather))==-1)
    {
        printf("Write error\n");
        exit(-1);
    }


    close(atis);
    return 0;
}**
Run Code Online (Sandbox Code Playgroud)

错误是分段错误11.

先感谢您!(抱歉我的英文,真的很糟糕^^)

c unix segmentation-fault

0
推荐指数
1
解决办法
377
查看次数

如何将char*复制到vector <char>并检索它?

我有char *RecBuffer,int *packetLength指向数据和大小

int i=0;
vector<char> temp;//copy the buffer
while(i<*packetLength)
{
temp.push_back(*(RecBuffer+i));
i++;
}

    ...do something 

//retrieve it now
RecBuffer = temp ????
Run Code Online (Sandbox Code Playgroud)

c++ stl

0
推荐指数
1
解决办法
4864
查看次数

对象创建和绑定的奇怪结果

我错误地写了一些愚蠢的东西,令我惊讶的是.

class A
{    public:
        void print()
        {
            std::cout << "You can't/won't see me !!!" << std::endl;
        }  
        A* get_me_the_current_object()
        {
            return this;
        }
};  
int main()
{
    A* abc = dynamic_cast<A*>(abc);
    abc->print();
}
Run Code Online (Sandbox Code Playgroud)

在这里,A* abc = dynamic_cast<A*>(abc)我正在对一个未声明的指针进行dynamic_cast.但是,它有效,所以我认为上述陈述被打破为:

A* abc;
abc = dynamic_cast<A*>(abc);
Run Code Online (Sandbox Code Playgroud)

因此,它的工作原理.但是,在尝试一些更奇怪的场景时,例如:

A* abc;
abc->print();
Run Code Online (Sandbox Code Playgroud)

并进一步

A* abc = abc->get_me_the_current_object(); 
abc->print();
Run Code Online (Sandbox Code Playgroud)

我惊呆了,看着这些例子是如何运作的,并且映射已经完成.
有人可以详细说明这些是如何工作的吗?提前致谢.

c++ dynamic-cast this

0
推荐指数
1
解决办法
75
查看次数

如何确保指针不指向同一个地方

运行以下代码,它打印出57 57,我需要的是56 57.我知道这是因为指针指向同一位置,但我该如何避免这种情况?

int (*spriteArray[10])[8][8];
int i = 0;

void setup()
{
   Serial.begin(9600);
}

void loop()
{

someOtherMethod();
i++;
someOtherMethod();

someMethod();

  delay(100000);
}

void someOtherMethod()
{
    int sprite[8][8];
    spriteArray[i]= &sprite;

    sprite[0][0] = 56+i;

}
void someMethod()
{

  Serial.println(*spriteArray[0][0][0]);   
  Serial.println(*spriteArray[1][0][0]); 

}
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

0
推荐指数
1
解决办法
72
查看次数

C编程 - XOR按位运算

以下'C'语句执行什么操作?

star = star ^ 0b00100100;

(A)切换变星的第2位和第5位.

(B)清除变量星的第2位和第5位以外的所有位.

(C)设置变量星的第2位和第5位以外的所有位.

(D)将变量星中的值与0b00100100相乘.

我仍然对此毫无头绪.有人可以帮我吗?

c bit-manipulation xor

0
推荐指数
2
解决办法
6万
查看次数

x86程序集从32位寄存器获取一个字节

我是x86汇编语言的programmig,我发现了一个问题.我调用C函数getch从stdin获取单个字符.但问题是返回值存储在寄存器EAX中.我想知道如何获得一个角色.不是4字节值.谢谢.

c x86 assembly getch

0
推荐指数
1
解决办法
2803
查看次数

我需要知道二进制堆代码的大O时间以及是否有改进

我需要知道我的二进制堆代码的大O时间是什么,以及如何改进它?

这是代码:

public static void CreateMaxHeap(int[] a)
{
    for (int heapsize = 0; heapsize < a.Length; heapsize++)
    {
        int n, p;
        n = heapsize;
        while (n > 0)
        {
            p = (n - 1) / 2;
            if(a[n]>a[p])
                Swap(a,n,p);
            n = p;
        }
    }
} // end of create heap
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
829
查看次数

捕获异常只是懒惰的错误检查?

我确定我会为此感到沮丧...但我来自一个没有try/ catch块的语言,而且我正在学习Java异常捕获,我很难看到它不是只是一个懒惰的验证版本.我见过几个例子:

String Box [] = {"Book", "Pen", "Pencil"};
while(i<4)
{
    try
    {
        System.out.println(Box[i]);     
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        System.out.println("Subscript Problem " + e);
        i++;
    }
Run Code Online (Sandbox Code Playgroud)

和:

int y = 0;
int x = 1;
// try block to "SEE" if an exception occurs
try
{
    int z = x/y;
    System.out.println("after division");
} 
catch (ArithmeticException ae) 
{
    System.out.println(" attempt to divide by 0");
}
Run Code Online (Sandbox Code Playgroud)

这些显然是非常基本的例子......但IMO要么是捕捉错误的可怕例子,要么没有充分的理由去做.在示例一中,如果我们循环元素的数量,Box我们不需要检查越界索引.在示例二中,如果我们检查0的除数,我们不需要检查它ArithmeticException.

是否有一个很好的例子或理由为什么使用try/ catchblocks比仅仅是旧时尚变量验证更好?还是仅仅是"Java"方式?

java exception-handling try-catch

-1
推荐指数
1
解决办法
746
查看次数

可以在switch语句中使用if else语句吗?

我试图在switch语句中插入一个if else语句,但只打印出else语句.有什么帮助吗?我指的是第三个switch语句.

        num_remain= num_temp/10;

    switch (num_remain)
    {

    case 0: printf(" "); break;
    case 2: if (num=0)
                        printf("Twenty");
                        else printf(" And Twenty"); break; // If num= 0 or not, it just prints "And Twenty".
    case 3: printf(" Thirty"); break;
    case 4: printf(" Fourty"); break;
    case 5: printf(" Fifty"); break;
    case 6: printf(" Sixty"); break;
    case 7: printf(" Seventy"); break;
    case 8: printf(" Eighty"); break;
    case 9: printf(" Ninety"); break;

    }
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
1
解决办法
139
查看次数

for statement和i.find in list

for a in ('90','52.6', '26.5'):
    if a == '90':
        z = (' 0',)
    elif a == '52.6':
        z = ('0', '5')
    else:
        z = ('25')

    for b in z:

        cmd = exepath + ' -a ' + str(a) + ' -b ' + str(b) 
        process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)

        outputstring = process.communicate()[0]
        outputlist = outputstring.splitlines()

        for i in outputlist:
            if i.find('The student says') != -1:
                print i
Run Code Online (Sandbox Code Playgroud)

处理作业,这是我的代码片段.这个代码上方有一部分,但它所做的只是定义exepath并只是在屏幕上打印exepath.当我运行它时,我没有得到任何错误或任何东西,但程序刚进入命令提示符时结束.为什么?以及如何解决?

编辑:抱歉报价但问题.我更新了代码以解决这个问题,但它仍然没有给我任何回复它只是退出...问题是什么?

python

-2
推荐指数
1
解决办法
198
查看次数

为什么这不会进入while循环?

我一直在试图弄清楚为什么它不打印测试信息并进入while循环.有任何想法吗?

void getInput(char * string)
{
  char * tempString;
  int maxLength = 1026; // Accounts for NULL and \n.
  tempString = malloc(maxLength * sizeof(char));  

  fgets(tempString, maxLength, stdin);
  size_t len = strlen(tempString);


  while ((int)len > maxLength)
  {  
    printf("Test");
    if (tempString[len-1] == '\n')
    {
      tempString[len-1] = '\0';
      len = strlen(tempString); 
    } // if
  } // while
Run Code Online (Sandbox Code Playgroud)

c while-loop

-2
推荐指数
1
解决办法
155
查看次数

条件是如何使用的?

以下C代码中的以下条件是什么意思?

if (line[currChar] == '\"')
Run Code Online (Sandbox Code Playgroud)

c if-statement escaping

-5
推荐指数
1
解决办法
85
查看次数