1

I have a bit of an issue. I'll explain, I'm trying to create a dynamic memory allocation for a string inside a struct and print it. The thing is, it doesn't care about the size I dynamically allocate.

For example, I create a string in the size of size 6, but it let's me insert 15 chars and prints them all.

So basically, it doesn't limit me on the string size, why's so?

typedef struct{
    int grade;
    int id;
    int arr[5];
    char *str;
}student;

int main(){
    puts("How many many letters in char?\n");
    scanf("%d", &num);
    getchar();
    student1.str = (char *)malloc(sizeof(char)*num+1);
    gets(buffer);
    strcopy(student1.str, buffer);
}

BTW, I tried to keep the code clear as possible for you guys/ladies, just with the main things I need. I know I didn't free the memory or checked if allocation failed etc...

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Isan Rivkin
  • 177
  • 1
  • 15
  • 3
    `strcopy`? seriously? – Sourav Ghosh Jan 03 '16 at 21:35
  • This is just how C works. There are no automatic bounds checking in the language. By accessing out of bounds memory you are invoking Undefined Behaviour. UB means that no specific behaviour can be relied on (it may appear to work, it may output garbage, it may crash, it may freeze, etc). – kaylum Jan 03 '16 at 21:37
  • This is nothing to do with structs, or even dynamic allocation. Try `char s[5]; strcpy(s, "Hello world! This is a really long string, much longer than the 5 characters allocated for this variable."); printf("%s\n", s);` - there's a *reasonable chance* it will print the complete string, then crash. (Of course it's not guaranteed) – user253751 Jan 03 '16 at 23:58

3 Answers3

4

First to say, please see this discussion on why not to cast the return value of malloc() and family in C..

Coming to the main issue here, out of bound memory access causes undefined behavior.

So basically, it doesn't limit me on the string size, why's so?

There is nothing in the C standard itself to prevent you from accessing out of bound memory (i.e, accessing the memory which is not allocated to your process), but any attempt to do so will lead to UB. Don't do that.

That said,

  1. Never use gets(), it suffers from buffer overrun issues, use fgets() instead.

  2. sizeof(char) is defined to be 1 in C. Using that as a multiplier is redundant.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

The malloc is giving you the permission to write to the memory but you can try and write without permission. That is what you just did. It may work and it may not, but If you will use malloc properly you should be able to run this code run-time-error-free

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
1

When you use C, a major part of the job of programming, is to make sure that any data that hits your allocated memory will actually fit. This is why C programming can be such a pain. It also requires you to free your memory when you don't use it anymore, which you forgot in your example.

Kavli
  • 304
  • 1
  • 5