0

I have an array allocated with malloc:

char *aStr1 = (char* ) malloc (10);

And then I filled this memory:

strcpy(aStr1, "ABCDEFGHI");

After that I created a new pointer aStr2:

char *aStr2 = aStr1 + 5;

And i set fourth element of memory to '\0':

*(aStr1 + 4) = '\0';

And finally, using these two pointers in a simple function:

int checkMem(char *aStr1, char *aStr2);

This function returns true (some none zero value) if aStr1 and aStr2 pointed to one memory block, and returns zero in another case.

How i can implement this function? (I read many linux mans about allocs function and haven't found any information about such problem).

//Added

I need this to do something like that:

char *aStr1 = (char *) malloc (10);
char *aStr2 = aStr1 + 5;
strcpy(aStr1, "ABCDEFGHI");
*(aStr1 + 4) = '\0';

and than:

my_strcat(aStr1, aStr2);

I do not ask for help to implement my_strcat, but maybe, get some hint how i can resolve its problem

//Updated

thanx, for all. I solved it.

Without any low level functions you cannot correctly know, how many memory allocate (maybe on some platform or realization you can do this:

size_t ptr_size = *((size_t *)ptr - 1);

but maybe not for all it will be correct).

And solving is simple: i create local copy of aSrc2, then realloc aSrc1 and copy aSrc2 to new aSrc1.

  • 3
    Define 'Memory Block' in this context. – Sinkingpoint Dec 08 '13 at 19:13
  • If you just want to determine if the two strings are end-to-end in memory, compare the address of the null terminator in one string with the address of the start of the other string. – lurker Dec 08 '13 at 19:14
  • Wild guess, you want to know if you can call `free` on your second pointer or not ? – Johan Dec 08 '13 at 19:15
  • @Quirliom 'Memory Block' - memory that allocated with one call of some allocation function. – Dumbledore Dec 08 '13 at 19:17
  • @mbratch Now, it would be very simple. Imagine if i allocate bigAmount bytes of memory. And set aStr[10] = '\0'; and aStr2 = aStr1 + someBigVallue – Dumbledore Dec 08 '13 at 19:22
  • 1
    Related: [How to get memory block length after malloc?](http://stackoverflow.com/q/5451104/509868) and [How can I get the size of a memory block allocated using malloc()?](http://stackoverflow.com/questions/1208644/how-can-i-get-the-size-of-a-memory-block-allocated-using-malloc?rq=1) (the latter with more links) – anatolyg Dec 08 '13 at 19:29
  • The do what @GlennTeitelbaum suggests in his answer. The question does arise as to why this feature is needed, and that might dictate how you implement it. – lurker Dec 08 '13 at 19:29
  • @mbratch to do something like that: char aStr1 = (char *) malloc (10); char *aStr2 = aStr1 + 5; strcpy(aStr1, "ABCDEFGHI"); *(aStr1 + 4) = '\0'; and than: my_strcat(aStr1, aStr2) I do not ask for help to implement my_strcat, but maybe, get some hint how i can resolve its problem. – Dumbledore Dec 08 '13 at 19:45

2 Answers2

3

Unfortunately you cannot tell if two pointers point to memory that belonged to the same initial allocation.

You can create classes/structures that, for instance, save the initial allocation and then you could compare them.

But without added information, you simply cannot tell.

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • And if i reallocate memory to smaller size, this destroy aStr2 data? or its undefined behavior? (Maybe i mean: will the pointer moves to another place, if i trim the memory?) – Dumbledore Dec 08 '13 at 19:33
  • if you realloc or do anything to pointer A, there is no way for pointer B to know or be automagically moved. You hit undefined behaviour with a high likelihood of data curruption and crashes – Glenn Teitelbaum Dec 08 '13 at 19:51
2

There is no provided standard mechanism for doing this, its up to you to track the memory you received and how big those allocations are, so you'd probably want to provide your own malloc wrapper that tracks what's allocd. Store the pointers in a map so you can use lower_bound to find the nearest allocate to the first string and then check if the second string is in the same allocation.

kfsone
  • 23,617
  • 2
  • 42
  • 74