0

I was getting realloc(): invalid next size for a program. So I just coded this to understand what's happening.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *inp;
    printf("%lu ",sizeof(inp));
    char *res = (char*)malloc(15*sizeof(char*));
    printf("%lu ",sizeof(res));
    res = "hello world";
    printf("%lu\n",sizeof(res));
    return 0;
}

And surprisingly it outputs 8 8 8. Can anyone explain why is it like that? Why is it 8 by default? And how malloc() effects size of inp?

vinayawsm
  • 845
  • 9
  • 28
  • 1
    You have a memory leak in your code, when you assign the string literal to the pointer, you *overwrite* the previous address stored in the pointer and loose the memory you previously allocated. After that a call to `free` or `realloc` will not work. – Some programmer dude Apr 01 '15 at 13:02
  • 1
    @JoachimPileborg actually, `inp` was never assigned to begin with. The `malloc` return value is stored in `res` and is not lost, aka no memory leak. – Drew McGowen Apr 01 '15 at 13:04
  • 1
    Also, [in C you should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Some programmer dude Apr 01 '15 at 13:04
  • @DrewMcGowen Ah, missed that. Well, I'll leave my comment in case the OP desiced to "fix" that assignment. – Some programmer dude Apr 01 '15 at 13:05
  • @JoachimPileborg now your first comment makes sense sir. :-) – Sourav Ghosh Apr 01 '15 at 15:27

4 Answers4

4

sizeof(inp) gives you the size of pointer (8 bytes, 64-bits), not the storage under that pointer.

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
1

You are using sizeof.returns size in bytes of the object representation of type. Remember sizeof is an operator.

Now sizeof is returning here 8 a constant type of size_t

Why isn't it changing? because in all tha cases you are using the same type that is char *.

Here 8 bytes is the size of the character pointer that is 64 bit.

You can probably have a look at this-

printf("size of array of 10 int: %d\n" sizeof(int[10]));

This will give the output:40. 40 bytes.

And malloc will never affect the size of a char*. It still needs 64 bits to store an address of a dynamically allocated space from heap.

Is it possible to determine size of dynamically allocated memory in c?

There is no standard way to do this. You have to take this overhead on your own. You may find some extensions to the compiler that may accomplish this.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • Are you trying to solve a problem in C language using C++ constructs? What the hell means `"size of array of 10 int:" << sizeof(int[10]) << '\n'` in C ???????????? Could you give an explanation for python language instead? (look at the tags, please) – Luis Colorado Apr 02 '15 at 11:35
  • @LuisColorado.: I myself ran the code in C++,then just post the line..sorry I will correct it. thx for pointing it out. – user2736738 Apr 02 '15 at 15:48
0

sizeof(inp) in your case is sizeof(pointer) and hence sizeof(char-pointer) on your system is 8 bytes which is a constant. While allocating memory using malloc() you would have specified the size so why do you need to get the size of the allocated space again?

And I see res is being initialized and not inp

Gopi
  • 19,784
  • 4
  • 24
  • 36
-1

****EDIT**** : The below post was written before the edit of the question.

You're missing stdlib.h, to the most, for function malloc() to work properly. After that,

Point 1:

char *res = (char*)malloc(15*sizeof(char*));

should be

char *res = malloc(15);   //will also issue warning to resolve missing stdlib.h

Point no note: you should be allocating memory for chars, not char *s. Then , you should write sizeof(char), not sizeof(char *). Also, sizeof(char) is always 1 in C. So, can omit that part.

Please read: do not cast the return value of malloc() and family in C.

Point 2:

strings are not supposed to be assigned to already malloc()ed pointers. use strcpy() instead.

 inp = "hello world";

should be

strcpy(inp, "hello world");

Otherwise, you'll overwrite the previously allocated memory, returned by malloc(). The assignment will overwrite the memory location held by inp, causing memory leak.

Point 3.

sizeof() operator returns a value of size_t. To print that, you need %zu format specifier.

Related, from C11 standard document, chapter §7.21.6.1, paragraph 7,

z

Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument.


Then, to answer the query about the output, in all the cases, you're printing sizeof(inp) which is essentially sizeof(char *) and that value is fixed for a particular platform (8, in your case).

Just FYI, sizeof() is an operator, it's not a function. It returns the size of the datatype, not the amount of space pointed by the variable.

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