简体   繁体   English

堆栈溢出,指针在C中

[英]Stack overflow with pointers in C

I'm new in C, and I'm trying some exercises that I found. 我是C的新手,我正在尝试一些我发现的练习。

In one of the exercises I'm trying to use a pointer to a string (a char array), but it doesn't work. 在其中一个练习中,我正在尝试使用指向字符串(char数组)的指针,但它不起作用。 It compiles, but when is executed, it throws "stack overflow" (well, I think is an "stack overflow" because I have it in spanish). 它编译,但是当执行时,它会抛出“堆栈溢出”(好吧,我认为是“堆栈溢出”,因为我用西班牙语)。

These are the problematic lines: 这些是有问题的线:

//This is the variable declaration, before this, there is the "main function" declaration
char entrada[100];
char *ult=entrada;
char cantidadstr[10];
int i,j,k = 0;
int res;

scanf ("%s",entrada);
printf ("\n%s",entrada);

//Here crashes
printf ("Hola %s",ult);
while (*ult != "\0"){

//And here there's more code

Thank you in advance!! 先感谢您!!

EDIT 编辑

(I can't answer me :)) Then, I'll post a bit more of code. (我不能回答:))然后,我会发布一些代码。

When I execute, after inserting data, it throws "Violación de segmento", and google says that means Stack Overflow 当我执行时,插入数据后,它会抛出“Violacióndesegmento”,谷歌说这意味着Stack Overflow

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

int main(void){
char entrada[1001*11*101];
/*Asi tenemos el tamano maximo:
1001 por las 1000 posibles lineas, mas la primera
11 por el tamano maximo del numero (1 + 9 ceros), mas el espacio o salto de linea siguiente
101 por el numero de numeros por linea, mas el primero
*/
char *ult=entrada;
char cantidadstr[10];
int i,j,k = 0;
int res;

memset (entrada,'\0',1001*11*101);
scanf ("%s",entrada);
printf ("\n%s",entrada);


//poniendo ese print ahi arriba, ese me lo muestra, por tanto, el fallo esta en el puntero de debajo de esta linea
printf ("Hola %s",ult);
while (*ult != "\0"){
    if(*ult == "\n"){
        if(i != 0){
            printf("\n");
        }
        i++;
        j = 0;
    }
    else if(i != 0){
        if(*ult == " "){
            j++;
            k=0;
            res = atoi(cantidadstr);
            printf("%d ",res*2);
            //Este es el otro cambio que hablaba
            cantidadstr[10] = '\0';             
        }
        else if(j != 0){
            cantidadstr[k] = *ult;
        }

    }
    k++;
    *ult++;
}
return 0;

} }

This is the exact and full code, with comments in spanish for another forum. 这是完整的代码,其他论坛的西班牙语评论。 The size of "entrada" is big enough for any data send in the exercise. “entrada”的大小足以支持练习中发送的任何数据。 The "memset" is just added. 刚刚添加了“memset”。 The second comment shows where it crashes 第二条评论显示它崩溃的地方

Thank you for your quick answer!! 谢谢你们的快速响应!!

The code before the while loop is fine as it compiles and runs correctly(as far as i can think) while循环之前的代码很好,因为它编译并正确运行(据我所知)

But the while loop has an error i'm not sure how it compiled in your case. 但是while循环有一个错误,我不确定它是如何在你的情况下编译的。 because you have written 因为你写过

while (*ult != "\\0"){

which gives compiler error as 这给出了编译错误

*ult is of type char
"\0" is of type const char*

you have to convert "\\0" to '\\0' 你必须将“\\ 0”转换为'\\ 0'

The following line: 以下行:

cantidadstr[10] = '\0';

will write past the end of cantidadstr , which is definitely bad and most likely causing your stack overflow. 将写过cantidadstr的结尾,这肯定是坏的,很可能导致你的堆栈溢出。 If you want to null terminate cantidadstr , use cantidadstr[9]= '\\0'; 如果要null终止cantidadstr ,请使用cantidadstr[9]= '\\0'; . Arrays in C are zero based, not one based, so the first element of an array of size N starts at [0] and the last referenceable element is [N-1] . C中的数组是基于零的,而不是基于数组的,因此大小为N的数组的第一个元素从[0]开始,最后一个可引用的元素是[N-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM