简体   繁体   中英

program crashing

I m newbie to programming. Please help me with this question.

when i execute this program, program will crash. can anybody tell me the exact reason for the crash?

#include<stdio.h>    
#include<string.h>    
#include<vector>    
using namespace std;    

struct s  
{  
    char *str;  
};  

std::vector<struct s> v;  
int main()  
{  
    struct s s1;  
    strcpy(s1.str,"hi");  
    v.push_back(s1);  
    strcpy(s1.str,"hello");  
    v.push_back(s1);  
    strcpy(s1.str,"How are you");  
    v.push_back(s1);  
    strcpy(s1.str,"AMAZING");  
    v.push_back(s1);  
    for (int i=0;i<(int)v.size();i++)  
    {           
        printf("%s\n",v[i].str);  
    }  
    return 0;  
}  

i am compiling it in devc++. pls help.

No memory has been allocated for str and struct s is violating the rule of three : use std::string instead.

You don't need to specify struct s (you do in C) when declaring a type of s , just use s :

std::vector<s> v;

s s1;

You could make struct s more convenient to use by providing a constructor:

struct s
{
    s(const std::string& a_s) : str(a_s) {}
    std::string str;
};

v.push_back(s("hi"));

You haven't allocated any memory for s1.str to point at. You're writing via an unitialized pointer, giving undefined behavior.

Try something like:

struct s { 
    std::string str;
};

s s1;

s1.str = "hi";
v.push_back(s1);
// etc.

better still, just use a string directly, and a C++11 initializer list:

std::vector<std::string> v{"hi", "hello", "how are you", "AMAZING"};

std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

You don't allocated memory for char *str; in your struct

just take a look at the strcpy documentation: http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

The memory were you are copying must be allocated. But when you write:

char *str; 

memory is not allocated. You've just created a pointer (to garbage)

You can allocated the memory or better use std::string , because memory management will be done for you in that case

您没有在s.str分配内存。

You are experiencing Undefined Behavior. Your char * str is just a pointe,r with no memory allocated. Nevertheless, you try to modify the memory it points to. Since that memory doesn't belong to your program, you are getting a crash.

Since it's UB, you can't really tell what will happen. For example, for me this code runs.

You need to allocate memory for s1, before using it (uniniatialized pointers point to garbage).. use new for that once done don't forget to release the memory using delete operator..

I suggest you to have a look in some good C++ tutorials,

  1. see this link
  2. Another good C++ resource

Note I have edited (changed) this answer since all others answer correctly address the problem. I just wanted to add few cents to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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