0

I DO UNDERSTAND THAT THIS PROGRAM IS NOT ALLOCATING ENOUGH MEMORY.

What I need help with is describing an explanation of what happens when this code is executed.

I put "Since only 4 spaces are allocated it is not given enough space so it causes an error." Which doesn't sound right to me. Thanks.

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

int main()
{ 
    char word1[20];
    char *word2;

    word2 = (char*)malloc(sizeof(char)*20);

    printf("Sizeof word 1: %d\n", sizeof (word1));  //This line outputs 20
    printf("Sizeof word 2: %d\n", sizeof (word2));  //This line outputs 4
                                                    //before & after I used malloc
    strcpy(word1, "string number 1");
    strcpy(word2, "string number 2"); <---- What is this doing

    printf("%s\n", word1);
    printf("%s\n", word2);
}
ShadyBears
  • 3,955
  • 13
  • 44
  • 66

3 Answers3

4

word2 is an uninitialised pointer. Writing data to it has undefined consequences but will probably crash. You need to allocate memory for it on the stack (as for word1) or dynamically, using malloc.

char *word2 = malloc(20); // arbitrary value. could use strlen(some_str)+1 also
strcpy(word2, "string number 2"); // works now

or, for posix systems

char *word2 = strdup("string number 2");

In either case, make sure to later call free to return this memory to the system.

Note that even after allocating memory, sizeof(word2) will remain 4. This is because word2 has type char* so sizeof is reporting the size of char* rather than the memory it points to.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • Ok one more question... does the code allocate memory exactly the same for word1/word2? Added in dynamic allocation. – ShadyBears Feb 12 '13 at 16:29
  • Sorry, I'm not sure I understand. In your current code, both `word1` and `word2` are allocated on the stack and will be automatically reclaimed when they go out of scope. If you use `malloc` (or any intermediate function which uses it), the memory allocated will remain allocated until you call `free`. If this doesn't answer your question, can you try re-stating it please? – simonc Feb 12 '13 at 16:32
  • Basically when I allocate word1 to size 20, is it the same as dynamically allocating *word2 to 20? – ShadyBears Feb 12 '13 at 16:36
  • the first 3 lines of the int main function basically. – ShadyBears Feb 12 '13 at 16:36
  • Either approach gives you a 20 byte buffer. `word1` is a stack based buffer so its memory is returned to the system (so unsafe for you to use) when `word1` goes out of scope. If `word2` is dynamically allocated, you control how long its memory is available for (by deciding when to call `free`) – simonc Feb 12 '13 at 16:44
2

sizeof( word2 ) returns 4 because that is the size of the pointer

char *word2;

is a pointer and there is 0 Bytes allocated for it ( not 4 as you mentioned)

sizeof( word1 ) returns 20 becuase that is the size of array

char word1[20]

is an array and there is 20 Bytes reserved for it

  • Ok one more question... does the code allocate memory exactly the same for word1/word2? Added in dynamic allocation. – ShadyBears Feb 12 '13 at 16:32
0

In your program word2 will have some previous value or probably a junk value. When you perform strcpy(word2, "string number 2");, you are trying to write to a location to which you don't have access to and hence, your program crashes. So, you need to allocate sufficient memory into which your program can write into.

Ganesh
  • 5,880
  • 2
  • 36
  • 54