1

Suppose I ve

int *a,*b;  
a= malloc(5*sizeof(int));  
b= malloc(5*sizeof(int)); 

and subsequently assign values.

Let a - 1, 2, 3, 4, 5
b - 6, 7, 8, 9, 10

Is there a method to concatenate both these malloced arrays without using further malloc,realloc or memcpy? There shud not be a malloc of 10 locations!

I must be able to get a[8]=9 after executing, without the overhead of moving the arrays. The language is C

3
  • 1
    Your question is meaningless. If you want to allocate 10 adjacent cells, why aren't you doing exactly that? Commented Feb 7, 2012 at 9:17
  • @EJP This is a simplified form of my prob. I ve task of combining 2 sets of data whose sizes are guided by some other factors. Commented Feb 7, 2012 at 9:25
  • Your question remains meaningless. malloc() allocates a new block of memory. A 2nd malloc() allocates another one. If you want them to be contiguous, allocate them both at the same time. There is no other answer. Commented Feb 8, 2012 at 9:18

3 Answers 3

6
a= malloc(5*sizeof(int));

You only allocated 5 ints to a, so no, you can't do it without some form or memory allocation (malloc / realloc), since a[8] would be illegal to begin with.

I must be able to get a[8]=9 after executing, without the overhead of moving the arrays

Since since you are working with contiguous memory regions (which you are calling arrays) you will always have some overhead when moving elements around. If you don't need to access elements by their indexes just use linked lists.

Sign up to request clarification or add additional context in comments.

1 Comment

By overhead I mean, doing a malloc of 10 locations, copying the elements from a and b and subsequently freeing the initial locations.
4

If you don't need strict array indexing, you could make a pseudo-linked-list (I know there's a name for this data type but I can't remember it right now):

struct listish {
  int *arr
  size_t size;
  struct listish *next;
};

The "indexing" function would look like this:

int *index(struct listish *list, size_t i)
{
    if(list == NULL) return NULL; // index out of bounds
    if(i < list->size) return list->arr + i; // return a pointer to the element
    else return index(list->next, i - list->size); // not in this array - go to next node
}

The idea is to combine the in-place reordering of a linked list with the contiguous space of an array. In this case, index(list, 4) would return &a[4], and index(list, 5) would return &b[0], simulating continuous indexing without reallocating and moving your entire array - all you need to do is allocate a few small struct listish objects and set them up properly, a task I leave to you.

1 Comment

@tss - If you want more info, the name of this data structure appears to be an unrolled linked list, though this is a slight variation that uses externally-managed arrays. Still, same concept.
2

What you ask can't be done.

You have, maybe, another option.
Just allocate space for 10 values, and make b point to the correct element

int *a = malloc(10 * sizeof *a);
/* error checking missing */
int *b = a + 5;

a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5;
b[0] = 6; b[1] = 7; b[2] = 8; b[3] = 9; b[4] = 10;

printf("a[8] is %d\n", a[8]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.