2

I want to dynamically allocate a 2d array to store strings.

I originally declared the array like this:

char lines[numlines][maxlinelength];

This however gives me a stack overflow when numlines is very huge.

How can I dynamically allocate it to prevent stack overflow?

gsamaras
73.7k50 gold badges210 silver badges330 bronze badges
asked Apr 1, 2016 at 11:08
1
  • arr = malloc(numlines[star]maxlinelength[star]sizeof(char)); Commented Apr 1, 2016 at 11:16

3 Answers 3

3

Use a pointer to an array:

#define maxlinelength 10
char (*lines)[maxlinelength] = malloc( sizeof( char[maxlinelength] ) * numlines ) ;
lines[0][0] = 'A' ;

This requires that the inner most size, maxlinelength, is constant.

You can avoid this limitation if you use pointers to variable-length arrays in which case the syntax remains the same and maxlinelength doesn't have to be a constant. Standards that supports this feature, are C99 and optionally C11.

( A constant is a variable whose value is known at compile time.)

( And to clarify: sizeof( *lines ) is identical to sizeof( char[maxlinelength] ) )

answered Apr 1, 2016 at 11:26
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code:

char **arr;
int i;
arr = malloc(numlines*sizeof(char*));
for (i = 0; i < numlines; i++)
 arr[i] = malloc(maxlinelength*sizeof(char));

This will give you a pointer to pointer, but you can handle it as if it was a 2D array.

Also see this very useful link in order to understand what dynamic allocation for 2D arrays actually does in the memory.

answered Apr 1, 2016 at 11:11

14 Comments

That is not a 2D array. It's an array of pointers to 1D arrays.
@MartinJames but the only way to allocate a 2D array dynamically is to actually allocate a double pointer..
@Marievi: That is wrong! You can very well have int (*p)[varcols] = malloc(...);
@Olaf - I didn't write this answer. I'm expressing disagreement with you that there is a single right answer (the one you're advocating) and that all others are wrong. With EVERY possible solution, it is possible to find a test case that shows a difference from an actual 2D array.
My disagreement stands. You are dismissing anything that doesn't comply with your narrow perspective, so no point in my discussing further.
|
-1

You may allocate a contiguous block of memory with the following:

const int numlines = 10;
const int maxlinelength = 120;
const int arrlen = sizeof(char) * numlines * maxlinelength;
const char **lines = (const char**)malloc(arrlen);
fprintf(stderr, "Allocated %d bytes\n", arrlen);

Add/remove const and typecasting according to taste.

answered Apr 1, 2016 at 11:16

1 Comment

The problem with this is that lines[i] does not point to (the first character of) an array of char, so lines[i][j] doesn't work as if it is accessing a 2D array.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.