小编Geo*_*ton的帖子

联合中的C对齐

在K&R C编程代码中,给出了以下代码

typedef long Align;     /* for alignment to long boundary */

union header {          /* block header: */
    struct {
        union header *ptr;     /* next block if on free list */
        unsigned size;         /* size of this block */
    } s;
    Align x;            /* force alignment of blocks */
};

typedef union header Header;
Run Code Online (Sandbox Code Playgroud)

然后它继续说"从未使用Align字段;它只是强制每个标题在最坏情况边界上对齐".我已多次阅读该文本但仍不明白为什么需要长时间进行对齐.SIZE是一个int,PTR是4个字节,所以它不应该对齐吗?为什么要使用long,而不是int?谢谢.

c alignment unions

3
推荐指数
1
解决办法
961
查看次数

这是C中的定义吗?

根据C标准,这被认为是一个定义

int x; 
Run Code Online (Sandbox Code Playgroud)

因为它声明x并分配存储空间.但这也是一个定义吗?

int *x;
Run Code Online (Sandbox Code Playgroud)

也就是说,声明指针变量或数组变量是否分配存储?我猜不是因为我们必须使用malloc.

c declaration definition

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

无法将类强制转换为其子类?

class Mid1Prob1 {
    public static void main(String[] args) {
        System.out.println("Testing...");
        int x = 3;
        System.out.println(3);
        System.out.println((int) x); // legal to cast primitive value to the same type as itself
        System.out.println((long) x); // legal to cast primitive value to a super type
        System.out.println((short) x); // legal to cast a primitive value to a sub type, but may lose information

        Actual act = new Actual();
        System.out.println(((Actual) act) .x); // legal to cast a reference value to the same class as itself
        System.out.println(((Super) …
Run Code Online (Sandbox Code Playgroud)

java casting

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

C中指针变量的数组初始值设定项

函数不能返回数组,只能返回指向数组的指针.所以我尝试这样做:

int *arr = {-1, -1};
Run Code Online (Sandbox Code Playgroud)

这是有效的语法吗?我得到了编译器的警告.如果没有,是否有更好的方法将指针变量设置为数组而不进行循环?

c

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

具有可变函数的char之前的预期表达式

我得到错误表达式"char之前的预期表达式"

char *set_buffer(char *buf, int num_str, ...) { 
    va_list args;
    va_start(args, num_str); 
    for (int i = 0, offset = 0; i < num_str; ++i) {
         char *str = va_args(args, char *); // error here
         offset += snprintf(buf+offset, strlen(str), str); 
    }
    va_end(args);
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么?c

c variadic-functions

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

更高效地创建新阵列或重置阵列

我有一个包含6个元素的数组.将这些元素设置为null或创建新数组是否更有效(按时间)?我将使用这个数组数百次.

java

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

Java 中的内存分配 - 声明与定义

在 C 中,当我们声明某些内容时,我们告诉编译器该变量包含什么类型。只有在定义期间我们才为其分配内存空间。然而,在Java中,当我们声明一个变量时,内存空间就被分配了

int x; // allocates space for an int
Run Code Online (Sandbox Code Playgroud)

我的前提正确吗?这是否意味着我们应该尽可能稀疏声明?

c java

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

外景没有区别

我正在定义一个全局变量 test2.h

#ifndef TEST2_H
#define TEST2_H

int test_var;

void use_it(void);

#endif
Run Code Online (Sandbox Code Playgroud)

并在两个不同的文件中再次定义它, test.c

#include <stdio.h>
#include "test2.h"

int test_var;

int main() {
    printf("The test_var is: %d\n", ++test_var); // prints 1
    use_it(); // prints 2
}
Run Code Online (Sandbox Code Playgroud)

test2.c

#include <stdio.h>
#include "test2.h"

int test_var;

void use_it() {
    printf("The test_var is: %d", ++test_var);
}
Run Code Online (Sandbox Code Playgroud)

我替换了test_varwith 的定义extern int test_var并得到了相同的结果.也就是说,在两种情况下都是两个文件,test.c并且test2.c可以访问全局变量test_var.我的印象是,没有extern,每个文件都有自己的副本test_var.观察表明情况并非如此.那么什么时候extern真的做了什么?

c

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

避免在构造函数中重复代码

我的Java代码看起来像这样:

public class Animal {
    Animal(String name) {
    // some code
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样的子类:

public class Dog extends Animal {
    Dog(String name) {
    // SAME code as Animal constructor
    }
}
Run Code Online (Sandbox Code Playgroud)

Dog和the Animal之间的唯一区别是Dog有一些方法可以覆盖超类.他们的构造函数具有完全相同的代码.我怎样才能避免这个重复的代码?我知道对象不能继承构造函数.

java constructor

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

在java中转义变量

你可以在Java中转义变量吗?例如,在PHP中,我可以这样做:

$test = 3;
echo "He is {$test} years old";
Run Code Online (Sandbox Code Playgroud)

哪个会输出:

 He is 3 years old
Run Code Online (Sandbox Code Playgroud)

你能用Java做类似的事吗?

java escaping

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

div内的跨度包裹

我在div中有一个span元素,就像这样

<div id='news_ticker'><span id='news_ticker_text'>this is some long string that is long</span></div>
Run Code Online (Sandbox Code Playgroud)

当我使用JQuery的动画在文档中移动跨度时,跨度及其文本将转到div中的下一行.我尝试将div宽度设置为一个非常大的宽度,并且它有效,但是有更好的解决方案吗?

我的代码就像

 #news_ticker_text { 
   visibility: hidden;
}
#news_ticker {
    background-color:black;
    position:fixed;
    width: 9999%;
    overflow: hidden;
}


/** Slides ticker text right. */
function slide_ticker_text() {
    var width = $(document).width();
    reset_ticker_text();
    $("#news_ticker_text").animate({
    marginLeft: width + "px",
    }, 25000, 'linear' /* Progresses linearly instead of easing.*/,
    function() {
        setTimeout(slide_ticker_text, 50);
    });
}
/** Resets ticker text to starting position. */
function reset_ticker_text() {
    var onset = -1 * $('#news_ticker_text').width() +"px";
    $('#news_ticker_text').css({ …
Run Code Online (Sandbox Code Playgroud)

html css jquery

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