Get size of a two-dimensional matrix

Asked

Viewed 107 times

-2

I have a dynamically allocated two-dimensional char matrix, as I do to get its size?

would use sizeof(client);

for example, I instated a client struct, Client **client; then in the code I’ll allocate it as it goes (tamVetor ++ and realloc); receiving data in its structure:

int tamVetor = 0; example: client = realloc(sizeof(char*)tamVetor); client[i] = malloc(sizeof(char)*150);

Within the function this code is implemented I can print the x indices of the struct wherever, for(int i = 0; i < tamVetor; i++);

However, this function returns the struct to another function that called it, example:

char **result = (function that receives return to struct);

In that function now I want to print the fields of the structs, but doing sizeof(result); always gives a very low value that I do not know giving came .

And for example, if I know that I have 12 clients of my struct, and in this function, when printing the fields of the struct on the screen I place(int i = 0; i < 12; i++) printa correctly, but if I place it is(int i = 0; i < sizeof(result); i++); print only a few clients, but no error at all.

  • sizeof() is calculated in compilation time. It does not help at all to calculate the size of a memory block allocated in running time.

1 answer

1

It is not possible to determine how much memory was allocated from the pointer only.

sizeof(p) is not able to determine at runtime the size of the previously allocated memory block. sizeof(p) returns the size of the pointer variable p, which in turn is calculated at compilation time.

p stores only initial address of the memory block that was allocated.

Whenever you allocate a memory block, malloc() returns the initial address of that block, but the end of the block cannot be determined from it, since there is no terminator type to delimit the end of that block.

You’re the one who should know where is the end of the block, therefore you should store the length of the block somewhere to remember your exact size.

On the other hand, you can implement a mechanism capable of retaining the size of the allocated block along with its respective pointer, for example:

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

struct pointer_s
{
    size_t size;
    void * p;
};

typedef struct pointer_s pointer_t;

pointer_t * my_malloc( size_t s )
{
    pointer_t * ptr = malloc(sizeof(pointer_t));
    ptr->p = malloc(s);
    ptr->size = s;
    return ptr;
}

void my_free( pointer_t * ptr )
{
    free(ptr->p);
    free(ptr);
}

int main( void )
{
    pointer_t * p = my_malloc( 123 );

    printf( "%ld\n", p->size );

    my_free( p );

    return 0;
}
  • Hi Lacobus, just clarifying that I’m a beginner in programming, and some concepts are the first time I see them, and I haven’t completely mastered the subject of pointer. I did not understand the example Enerico gave me, I did not understand how I would implement in my code if you have another, or some material to read specifically about it I am grateful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.