0

I'm actually learning C and I got a "problem".

I created a pointer to a structure with a char* and int with malloc(1). The pointer works and I can edit structure children without problems.

I also created a pointer to a int (still with malloc(1)) and it works. Another thing is I didn't get core dump error when I tried to access *(pointer + 33780) (Core dump comes when the value is a bit higher) it worked, but default value was 0.

Thank you, that's not a "problem" but I'd like to know why is that doing like this.

Sorry for being the English's murderer.

EDIT : Here the code

struct Personne
{
  char *name;
  int age;
};

int main(int argc, char *argv[])
{
    printf("%ld\n", sizeof(struct Personne));
    struct Personne *testPoint = malloc(1);
    printf("testPoint : %p\n", testPoint);
    printf("testPoint : %p\n", testPoint->name);
    testPoint->name = "UnNomInconnu";
    testPoint->age = 20;

    free(testPoint);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    You really should show the actual code. But understand: if you access memory outside of your application's valid memory space, *you may or may not get a segmentation fault or any other error*. The lack of a fault is no way indicative of a lack of a problem in the code. You might have accessed remote memory that, for now, doesn't cause any ill effect. In this particular case with `malloc` it might be some other location in the memory heap that belongs to another process or is still free for another process (or yours) to allocate. It would be an invalid access. – lurker Nov 24 '15 at 17:34
  • You're invoking undefined behavior because you're accessing memory that you haven't properly allocated, using pointers of different types etc etc etc. Undefined behavior means that anything can happen, for example a crash, but it could also be anything else - the program could even finish executing and produce correct results. – user4520 Nov 24 '15 at 17:40
  • Two niggles: use `printf("%zu\n", sizeof(struct Personne));` and use `printf("testPoint : %p\n", (void*)testPoint);` – Weather Vane Nov 24 '15 at 17:42
  • There really should be a canonical duplicate question for all the questions that ask why they don't get an error when they do something they know is invalid. – Barmar Nov 24 '15 at 17:48

3 Answers3

2

Actually function malloc does not allocate a memory extent exactly of the size of 1 byte or of a similar small value. Usually the minimum size of an allocated extent is equal to the size of the paragraph that is to 16 bytes.

So if you will write for example

char *p = malloc( 1 );

then the actual size of the allocated extent can be equal to 16 bytes.

Nevertheles you should not rely on this feature becuase in general that is according to the C Standard this is undefined behaviour.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks, I tried `printf("%ld\n", sizeof(*testPoint));` and it printed me 16 bytes, problem solved. – Juju17ification Nov 24 '15 at 17:50
  • 1
    @Juju17ification You made a wrong conclusion.:) This expression sizeof(*testPoint) is unevaluated. Simply the size of your structure is equal to 16 bytes. It is not the size of the allocated extent.:) – Vlad from Moscow Nov 24 '15 at 17:54
  • Well, I'll trust you about the face that the size of the allocated memory will be higher than 1 then (If I correctly understood). – Juju17ification Nov 24 '15 at 18:10
  • @Juju17ification You understood correctly. Nevertheless in any case you may not do such a way because in general it is undefined behaviour. The Standard does not says when the actual size of the extent. It only says that you will get memory of the required size. – Vlad from Moscow Nov 24 '15 at 18:23
2
 struct Personne *testPoint = malloc(1);

puts you into the realm of undefined behavior. Unfortunately UB can include "the program runs correctly in all test environments but fails in production at worst possible time" - and frequently does.

If you are on linux run your program under valgrind and/or electric fence. You will see why those tools exist.

pm100
  • 48,078
  • 23
  • 82
  • 145
0

You need:

struct Personne *testPoint = malloc(sizeof(*testPoint));

1 byte is not enough memory for your structure. The meaning of malloc() argument is the number of bytes. However, malloc() does not necessarily protect the memory beyond the allocated segment, so in some cases it is possible to overwrite the boundary without fatal consequences.

Sasha Pachev
  • 5,162
  • 3
  • 20
  • 20
  • I think the OP is complaining that it *didn't* crash when using the above (obviously incorrect) code. – DaoWen Nov 24 '15 at 17:42
  • I think OP is aware of this; they ask why using `1` as the argument still gave them seemingly correct behavior. – user4520 Nov 24 '15 at 17:42