1

I have a problem with this C program. I don't understand why even though i initialized my array with a malloc() instruction, I am having the same size (4bytes) whatever is the second parameter i pass to my function initialize.

#include <stdio.h>
#include <stdlib.h>

typedef struct stack{
    int size;
    int *tab;
    int top;
} stack;

void initialize(stack* st, int sizeArray);
int pop(stack* stack);
void push(stack* stack, int number);


int main(){
    int sized;
    stack S;
    stack* ptr_S = &S;
    printf("Enter the size of your stack please: \n");
    scanf("%d", &sized);
    //We send the pointer of the stack to initialise
    initialize(ptr_S, sized);
    printf("%d\t%d", S.size, S.top);
    //printf("\nThe size of the array is: %d\n", sizeof(S.tab)/sizeof(int));
    printf("\nThe size of the array is: %d\n", sizeof(S.tab));
    pop(ptr_S);
    return 0;
}

void initialize(stack* st, int sizeArray){
    st->size = sizeArray;
    st->top = 0;
    st->tab = (int*)malloc(sizeof(int) * sizeArray);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
franckstifler
  • 396
  • 2
  • 11
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jan 14 '16 at 16:32
  • The size of the array is `sizeof(int) * sized`; it has `sized` elements. – pmg Jan 14 '16 at 16:43

1 Answers1

4

First of all, arrays are not pointers, ane vice-versa.

On your code, S.tab is a pointer, and using sizeof on a pointer will evaluate to the size of the pointer itself, not the amount of allocated memory to that pointer.

On your platform, a pointer (int *) is having a size of 4 bytes, so you always see the output being 4.

In case, you have a properly null-terminated char array, you can use strlen() to get the length of the elements of the string, however, still that may not give you the actual size of the memory allocated, anyway. You need to keep track of the size yourself. Normally, you cannot expect to extract the information from the pointer itself.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks for the explanation, could you now help me with the syntax i should use to get the good result? Because I actually agree it is a pointer but how using the unary (*) is not still resolving my problem. – franckstifler Jan 14 '16 at 16:39
  • @franckstifler which unary * you're talking about, please? – Sourav Ghosh Jan 14 '16 at 16:41
  • 1
    `strlen` *definitely* won't give you the allocated size, which is at least `strlen(...) + 1`. – Keith Thompson Jan 14 '16 at 16:50
  • Sorry for my english, I am not native english speaker. Just got confused in the `strlen()` is it not meant for Strings?. My program is meant to simulate stacks of integers with an array of integers. I wished my user should choose the size of the stack, and i allocate memory it on my pointer `int* tab`. – franckstifler Jan 14 '16 at 16:55
  • @KeithThompson Right sir, that's why I mentioned about the size of the elements, not the memory. :) – Sourav Ghosh Jan 14 '16 at 17:01
  • @franckstifler Sorry, I still did not get you. Leave about the `strlen()` part, that was just an example. As far as memory allocation is concerned, it is good. Just don't try to retrieve the size from the pointer using `sizeof`. You need to make use of the `size` variable itself go get the size. – Sourav Ghosh Jan 14 '16 at 17:04
  • My code works. Although I could not get the size. I did some testing and it works smoothly. – franckstifler Jan 14 '16 at 17:08
  • 1
    @franckstifler That is what, allocation is fine, just don't expect `sizeof` will give you the size, it will not. That's all – Sourav Ghosh Jan 14 '16 at 17:09