小编use*_*406的帖子

什么时候printf("%s",char*)停止打印?

在我的课上,我们正在编写自己的C的malloc()函数副本.为了测试我的代码(当前可以分配空间很好)我正在使用:

char* ptr = my_malloc(6*sizeof(char));
memcpy(ptr, "Hello\n", 6*sizeof(char));
printf("%s", ptr);
Run Code Online (Sandbox Code Playgroud)

输出通常是这样的:

Hello
Unprintable character
Run Code Online (Sandbox Code Playgroud)

有些调试认为我的代码本身并没有造成这种情况,因为ptr的内存如下:

[24个字节的元信息] [请求的字节数] [填充]

所以我认为printf正在进入填充,这只是垃圾.所以我进行了测试: printf("%s", "test\nd");得到:

test
d
Run Code Online (Sandbox Code Playgroud)

这让我想知道,当DOES printf("%s",char*)停止打印字符时?

c printf

15
推荐指数
2
解决办法
3万
查看次数

在Linux上编程C++时的非法指令

我的程序,每次运行时都会执行完全相同的操作(将点精灵移动到距离中)将随机终止"非法指令"上的文本失败.我的谷歌搜索发现人们在编写汇编时会遇到这种情况,因为汇编会抛出这些错误.

但是为什么g ++会产生这样的非法指令呢?这不像我正在为Windows编译然后在Linux上运行(即便如此,只要两者都在x86上就不应该AFAIK导致非法指令).我将在下面发布主文件.

我无法可靠地重现错误.虽然,如果我进行随机更改(在这里添加一个空格,在那里更改一个常量)强制重新编译,我可以得到一个二进制文件,每次运行时都会失败并返回非法指令,直到我尝试设置一个断点,这使得非法指令'消失'.:(

#include <stdio.h>
#include <stdlib.h> 
#include <GL/gl.h>
#include <GL/glu.h>

#include <SDL/SDL.h>

#include "Screen.h"  //Simple SDL wrapper
#include "Textures.h" //Simple OpenGL texture wrapper 
#include "PointSprites.h" //Simple point sprites wrapper


double counter = 0;
/* Here goes our drawing code */
int drawGLScene()
{
    /* These are to calculate our fps */
    static GLint T0     = 0;
    static GLint Frames = 0;

    /* Move Left 1.5 Units And Into The Screen 6.0 */
    glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -6);
 glClear(GL_COLOR_BUFFER_BIT | …
Run Code Online (Sandbox Code Playgroud)

c++ gcc g++

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

标签 统计

c ×1

c++ ×1

g++ ×1

gcc ×1

printf ×1