0

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.

  • #1 what shall your program accomplish #2 what is the error code – Sim Jul 31 '12 at 09:24
  • use __declspec(align(16)) char[] to make it faster – huseyin tugrul buyukisik Jul 31 '12 at 09:25
  • 2
    @tuğrulbüyükışık: wtf? First of all, that is very compiler specific, second of all forcing alignment will often make your program slower, not faster, third of all: this is a newbie question about a crash, not some advanced optimization question :s. – KillianDS Jul 31 '12 at 09:32
  • @KillianDS: OMG? He is newbie and started from vectors & pointers? VC++ 10.0 express 's strlen(char * str) includes a loop that checks if str is aligned. Making str aligned in the declaration makes strlen-like functions work faster for some small-length strings. – huseyin tugrul buyukisik Jul 31 '12 at 09:37
  • @tuğrulbüyükışık: automatic storage and dynamic storage (using new) should be aligned correctly without specification. specifying an alignment can result in something sub-optimal for your specific platform. – KillianDS Jul 31 '12 at 09:50
  • i didnt know that "new" was making it aligned. thanks. is it same with calloc malloc alloc? – huseyin tugrul buyukisik Jul 31 '12 at 09:52

6 Answers6

3

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"));
Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
2

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"));
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

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

Andrew
  • 24,218
  • 13
  • 61
  • 90
0

You are not allocating memory in s.str.

yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
0

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0

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.

Amit
  • 13,134
  • 17
  • 77
  • 148
  • 1
    mallocs and c-style casts in a C++ program? That's not something you should learn to a novice programmer. You even forgot the `free`'s. Also, you do not need to allocate memory for `s1`, it's an automatic storage variable. – KillianDS Jul 31 '12 at 09:33
  • @KillianDS good points... I actually didn't notice that its C++ program... #stupidMe... updating my answer for that.... Thanks – Amit Jul 31 '12 at 09:47