0

How can I create an array of pointers that can store more than 1,047,141 pointers? I calculated this number using the following code:

int main(int argc, char const *argv[]) {
  long a = 0;
  while(1==1){
    char * str[a];
    printf("%ld is good.\n", a);
    a++;
    //Loop ends on Segmentation fault
  }

  return 0;
}

I am using the array of pointers to store strings. What are the alternatives?

Edit

The code above is just a way of finding the max size of an array of pointers.

One pointer holds one string, so the max number of strings I can store is 1,047,141. I need a way of storing more than 1,047,141 strings.

2

3 Answers 3

4

Allocate the array dynamically via malloc().

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

int main(int argc, char const *argv[]) {
  long a = 0;
  while(1==1){
    char ** str = malloc(sizeof(char*) * a);
    if (str != NULL){
      printf("%ld is good.\n", a);
      free(str);
    } else {
      break;
    }
    a++;
  }

  return 0;
}
5
  • I also need to be able to access previously stored strings.
    – olfek
    Jul 14, 2016 at 14:49
  • Each pointer in the pointer array is assigned a string, I need to be able to access each and every one later on.
    – olfek
    Jul 14, 2016 at 14:52
  • @sudoman When is "later"? Where will you assign strings and where will you use them? Write code to do what you want. If you don't know how to do, please ask questions, not just telling what you want to do.
    – MikeCAT
    Jul 14, 2016 at 14:54
  • @MikeCAT The code in my question is simply a way to find out the max size of an array of pointers. What I want is a way to store more than the max amount of pointers (which will be used to store strings) in a pointer array. And if thats not possible then another way of storing more than 1,047,141 strings.
    – olfek
    Jul 14, 2016 at 14:59
  • @sudoman Files on disks are commonly used to store data that is too big to store in the (main) memory.
    – MikeCAT
    Jul 14, 2016 at 15:02
2

You have to allocate the arrays on the heap with malloc. This code will allocate an array of pointers long how_many_strings; and for each pointer it will allocate a string long str_length.

char** str = malloc(sizeof(char*)*how_many_strings);
for(int i = 0; i < how_many_strings; i++)
{
    str[i] = malloc(sizeof(char)*str_length);
}

The size is limited to your RAM capacity.

0

The OP code has undefined behavior. The array isn't used, so if you use -O2 (gcc), you are just printing a as it increments. Gcc generates:

.L2:
    movq    %rbx, %rdx
    movl    $.LC0, %esi
    movl    $1, %edi
    xorl    %eax, %eax
    addq    $1, %rbx
    call    __printf_chk
    jmp .L2

It won't segfault, but the output will be quite boring.

However, with -O0, gcc generates a much longer loop (that I don't want to paste) that creates larger and larger str buffers on the stack. At some point when running this you will run out of stack space, which can cause a segfault.

3
  • Which part of the OP's code provokes "undefined behavior", and how, please?
    – alk
    Jul 14, 2016 at 15:30
  • Every time through the loop the buffer may grow (with -O0 on gcc). Eventually this causes a stack overflow.
    – evaitl
    Jul 14, 2016 at 15:32
  • Ahok, sure, I understood you were referring to something else on top of the SO provoking UB.
    – alk
    Jul 14, 2016 at 15:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.