我有以下代码.
#include <iostream>
int * foo()
{
int a = 5;
return &a;
}
int main()
{
int* p = foo();
std::cout << *p;
*p = 8;
std::cout << *p;
}
Run Code Online (Sandbox Code Playgroud)
而代码只是运行而没有运行时异常!
输出是 58
怎么会这样?本地变量的内存不能在其功能之外无法访问吗?
为什么第一个函数返回字符串"Hello,World",但第二个函数不返回任何内容.我认为两个函数的返回值都是未定义的,因为它们返回的数据超出了范围.
#include <stdio.h>
// This successfully returns "Hello, World"
char* function1()
{
char* string = "Hello, World!";
return string;
}
// This returns nothing
char* function2()
{
char string[] = "Hello, World!";
return string;
}
int main()
{
char* foo1 = function1();
printf("%s\n", foo1); // Prints "Hello, World"
printf("------------\n");
char* foo2 = function2(); // Prints nothing
printf("%s\n", foo2);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这个问题可以作为所有常见问题的参考:
当我将数据复制/扫描到未初始化指针所指向的地址时,为什么会出现神秘崩溃或"分段错误"?
例如:
char* ptr;
strcpy(ptr, "hello world"); // crash here!
Run Code Online (Sandbox Code Playgroud)
要么
char* ptr;
scanf("%s", ptr); // crash here!
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int foo1(void)
{
int p;
p = 99;
return p;
}
char *foo2(void)
{
char buffer[] = "test_123";
return buffer;
}
int *foo3(void)
{
int t[3] = {1,2,3};
return t;
}
int main(void)
{
int *p;
char *s;
printf("foo1: %d\n", foo1());
printf("foo2: %s\n", foo2());
printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时gcc -ansi -pedantic -W -Wall,编译器会发出foo2()和foo3()的警告消息:
warning: function returns address of local variable
Run Code Online (Sandbox Code Playgroud)
我认为不允许返回局部变量,但foo1()工作正常,似乎返回指向本地对象的指针和对象本身之间存在巨大差异.
任何人都能对这个问题有所了解吗?提前致谢!
我是一名Ruby程序员,最终为C开发代码生成.就像要求Limo拖着一辆20世纪60年代的卡车一样.无论如何.
这是我认为应该工作,但不起作用.
float[][] pixels()
{
float x[][]= { {1,1},{2,2} };
return x
}
void drawLine(float x[][2])
{
//drawing the line
}
//inside main
drawLine(pixels());
Run Code Online (Sandbox Code Playgroud)
我把头撞在桌子上,试图让这件事情发挥作用.请帮忙.
我不知道为什么会这样.由于x是局部变量,我以为在尝试返回时会出错.然而,第一个printf工作正常,但它只是打印出来0.任何人都可以解释这里发生了什么?
#include <stdio.h>
int* func1() {
int x = 123123;
int *y = &x;
return y;
}
int main()
{
int* c = func1();
printf("%d\n", *c); // output: 123123
printf("%d\n", *c); // output: 0
return 0;
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码.
#include<stdio.h>
int *abc(); // this function returns a pointer of type int
int main()
{
int *ptr;
ptr = abc();
printf("%d", *ptr);
return 0;
}
int *abc()
{
int i = 45500, *p;
p = &i;
return p;
}
Run Code Online (Sandbox Code Playgroud)
输出:
45500
我知道根据链接这种行为是未定义的.但是为什么每次运行程序时我都能获得正确的值.
从c中的函数返回多维数组的最佳方法是什么?
说,我们需要一个函数生成多维数组并调用它的主,是它最好能包住它在一个结构或只返回一个指向内存堆?
int *create_array(int rows, int columns){
int array[rows][columns] = {0};
return array;
}
int main(){
int row = 10;
int columns = 2;
create_array(row,columns);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码,只是为了勾勒出我想到的基本程序.
可能重复:
指向局部变量的指针
可以在其范围之外访问局部变量的内存吗?
我有一个有趣的问题.我有一个返回指针的read函数:
char * myReadFunc() {
char r [10];
//some code that reads data into r.
return r;
}
Run Code Online (Sandbox Code Playgroud)
现在,我调用此函数将信息分配给我的一些变量:
char * s;
//Some code to specify where to read from.
s = myReadFunc();
Run Code Online (Sandbox Code Playgroud)
这会产生我想要的结果.
但是,当我这样做时:
char * s1;
char * s2;
//Some code to specify where to read from.
s1 = myReadFunc();
//Some code to change the read location.
s2 = myReadFunc();
Run Code Online (Sandbox Code Playgroud)
我得到一些奇怪的结果.两者的数据相同,并且始终来自第二个指定的读取位置.
所以我尝试了一些备用代码:
char * s1;
char * s2;
//Some code to specify where to read …Run Code Online (Sandbox Code Playgroud)