1

This program is crashing. Please tell me what's wrong with it. When I use an array instead of a pointer like Name[12] in the structure it doesn't crash. I guess there is some problem in dynamic memory allocation. Help please.

#include <stdio.h>

struct struct_tag
{
    int number;
    char *Name;
} struct_name;


main()
{
    struct_name.number = 34;

    char *Name = (char *) malloc(sizeof(char));
    strcpy(struct_name.Name,"A");

    printf("%d", struct_name.number);
}
Jay
  • 9,585
  • 6
  • 49
  • 72
user3995169
  • 145
  • 1
  • 9
  • 3
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Sep 02 '14 at 12:56
  • 3
    `char *Name` is not the same as `struct_name.Name`. – DCoder Sep 02 '14 at 12:58
  • 2
    @unwind sir my teacher told me malloc return void pointer its important to cast it tell what should the return value – user3995169 Sep 02 '14 at 13:01
  • 3
    @StackOverflow Then I don't agree with your teacher. :) Consider showing the linked-to answer. Casting `void *` to a different pointer type in C is pointless; it's not needed and it can hide actual errors, all the while making the code harder to read. Don't do it. – unwind Sep 02 '14 at 13:02
  • @unwind can you give me an example when it can hide errors & why it is pointless refer me to some question or text that i can read & understand your point of pointless casting...Thanks – user3995169 Sep 02 '14 at 13:53
  • @unwind got it : http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc ........just googled why not to cast malloc. Thanks – user3995169 Sep 02 '14 at 13:56
  • 2
    @StackOverflow You googled, to find the link I gave you when I first said something about casting? Full circle! :) – unwind Sep 02 '14 at 13:57
  • @unwind sorry i somehow didn't saw that comment. Something is really wrong today. My mind is not working the way it show be :( Thanks anyways having people like you around is no more than blessing for Noobs & leaners like me. – user3995169 Sep 02 '14 at 21:40
  • & the best thing is you answered it there. However i can't thumbs up your answer :( coz of low reputation...so well explained – user3995169 Sep 02 '14 at 21:46

3 Answers3

4

You're allocating a single character:

char *Name = (char *) malloc(sizeof(char));

And then never using that memory for anything. You meant to allocate memory for struct_name.Name, undoubtedly. But even if you had done so, you're then filling it with two characters ('a' and '\0'):

strcpy(struct_name.Name,"A");

which will cause an entirely different bug.

You want to say:

struct_name.Name = malloc( 2 );

Since (a) you shouldn't cast the result of malloc() and (b) sizeof(char) is always 1 and (c) you need room for the 0 at the end of your string.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
2

For errors:

  • You are allocating memeory for *Name however you are not allocating memory for struct_name.Name. So first thing is you need to allocate memory for struct_name.Name

  • As you already know that you'll be storing "A" in
    struct_name.Name you should allocate memory for 2 char.("A" is string i.e 'A' and '\0')

For warnings:

  • If you want to use strcpy function include string.h in your code.

  • Also if you are using malloc include stdlib.h in your code.

Try this fixed code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct struct_tag
{
    int number;
    char *Name;
}struct_name;

int main()
{
    struct_name.number = 34;
    struct_name.Name = malloc(sizeof(char)*2); // As you will store "A" 
    strcpy(struct_name.Name,"A");

    printf("%d \t", struct_name.number);
    printf("%s \n", struct_name.Name);

    return 0;
}
ani627
  • 5,578
  • 8
  • 39
  • 45
  • The OP asked "Please tell me whats wrong with this program". – Jongware Sep 02 '14 at 13:37
  • @Jongware: I have also removed all the warning from OP's code. Please explain why you downvote my answer? – ani627 Sep 02 '14 at 13:49
  • 1
    "Try this", followed by just a piece of code with minimal commenting (only one single comment). Compare the other answers, which *explain* what the OP did wrong, and where, and what it should be. – Jongware Sep 02 '14 at 14:00
1

first look code carefully.

 char *Name = (char *) malloc(sizeof(char));
 strcpy(struct_name.Name,"A");

Hare for what you allocated memory (char *Name) and in which you copied string(struct_name.Name)?

here you not allocate memory for struct_name.Name. Also you have allocate memory for one character and you tried to copy two characters.('A' and '\0').

It should be

struct_name.Name = malloc(2); 
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73