user1768079
user1768079

Reputation: 713

Will successive calls to malloc allocate space directly after the previous call in c?

I am talking about using char* and dynamic arrays. Specifically I want to use a 2d array to break up a line of commands and options into their components

For example:

char *dynamicArr = malloc(1 * sizeof(char));//allocate first letter
int i = 0;
while(string[i] != '\0'){
    dynamicArr[i] = string[i];
    //would I be able to allocate more memory onto the end of dynamicArr here?
}

Obviously I could just use strlen and strcpy in this case, but I am trying to parse a line of commands using various tokens, ' ' '\"' and '\''. So the individual strings are not null terminated. I have written it using Static 2d arrays but I want to make it dynamic so that it can handle commands of any size.

Upvotes: 1

Views: 1467

Answers (3)

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6013

ORIGIONAL QUESTION:

//would I be able to allocate more memory onto the end of dynamicArr here?

Yes. The size of dynamicArr can be expanded using realloc().

And, with some cautions (as pointed out by André Puel and DevSolar comments):

The realloc(ptr, size) call first attempts to change the size of the allocation pointed to by ptr to size, and returns ptr. However, if it is unable to change the size sufficiently, it malloc()s a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.

If ptr is NULL, realloc() is identical to a call to malloc() for size bytes.

If size is zero and ptr is not NULL, a new, minimum sized object is allocated and the original object is freed.

When extending a region allocated with calloc(3), realloc(3) does not guarantee that the additional memory is also zero-filled.

MODIFIED QUESTION:

// Will successive calls to malloc allocate space directly after the previous call in c?

No. You can't, and shouldn't, count on that behavior.

Upvotes: 1

david.pfx
david.pfx

Reputation: 10863

No. Definitely not.

A block of memory allocated by malloc comes with no guarantees as to its location or its relationship with other blocks. There could be free space immediately after it, but just as likely there is not. There is no way to extend it, and no way to determine whether it could be extended.

What you can do instead is to use realloc(). A call to realloc will return a pointer to a block of memory just like malloc, while preserving the contents of the block passed in, but not necessarily in the same location. Depending on the size requested (smaller, same size, or bigger) and the availability of space in the heap, realloc may return the original block adjusted in size as needed, or it may return a new block in a different location. In the latter case realloc will copy the old contents into the new block.

The important thing to remember is that the pointer returned by realloc could be different, so you must assume that it will be and code accordingly.

However, realloc is relatively expensive and should not be called character by character. A typical solution to your problem is to read into a large enough temporary buffer, allocation memory of the correct size and copy the data. In C++ you would use strings or vector of char and save yourself a lot of grief.

Upvotes: 1

haccks
haccks

Reputation: 106102

Will successive calls to malloc allocate space directly after the previous call in c?

No. Not necessary. malloc allocates contiguous chunk of memory but it doesn't guarantee that the next call to malloc will allocate the chunk just after the previous one.

I have written it using Static 2d arrays but I want to make it dynamic so that it can handle commands of any size.

Use realloc() function.

Upvotes: 2

Related Questions