Jump to content

How do you make a dynamically allocated pointer to a static array in C?

- - - - -

  • Please log in to reply
9 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I want to create a pointer to a static character array. The array has to be INT16_MAX characters long, so I would like to be able to get rid of it later in the program. The only way I can think of to do this is to dynamically allocate a pointer to it, using malloc. I tried:

char[INT16_MAX]  *strptr = (char[] *) malloc( sizeof(char[] *) );
and

char st[INT16_MAX] *strptr ...;
Neither of them works. What is the data type for a pointer to an array? I can't use char **, because the array has to be a fixed width. Otherwise the compiler won't let me read a line from a file into it and I get a segfault. Please don't tell me why the code I gave is wrong. I know perfectly well that it doesn't make sense; I just wanted to provide some examples to illustrate. You can ignore the malloc part. It really isn't important. All I want to know is how to make a pointer to a fixed width array so that I can deallocate the array later and not clog up memory.

Also, please no "You're not supposed to use INT16_MAX" or any of that crap that has nothing to do with the problem.
Life's too short to be cool. Be a nerd.

#2
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
So you have an char array char st[INT16_MAX] and you want a pointer to it?
char st[INT16_MAX];
char *ptr = st;

Depends on your system but you do know that this is probably allocating about 32000 chars?
char st[INT16_MAX];

You could use malloc to allocate the memory needed by the char ptr which is probably 4 bytes or sizeof(char *). Deallocating that pointer won't clean up the array it's pointing at however.
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#3
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts

Sysop_fb said:

So you have an char array char st[INT16_MAX] and you want a pointer to it?
char st[INT16_MAX];
char *ptr = st;
Depends on your system but you do know that this is probably allocating about 32000 chars?
char st[INT16_MAX];

I am well aware that INT16_MAX is really huge. Some files happen to have really long lines. That's not the issue here, and, like I specifically stated in my last sentence, it has nothing to do with the problem. Also, the first piece of code you gave me is exactly what I'm trying to avoid, and I think I made that fairly clear. What I want is a fixed length array that I can deallocate later. You can not deallocate an array that is declared that way.

By the way it should be:

char st[INT16_MAX];
char *ptr = &st;

Life's too short to be cool. Be a nerd.

#4
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
So you want to dynamically allocate an array of chars that is INT16_MAX long?
char *ptr = malloc(sizeof(char) * INT16_MAX);

"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#5
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts

Sysop_fb said:

So you want to dynamically allocate an array of chars that is INT16_MAX long?
char *ptr = malloc(sizeof(char) * INT16_MAX);

No. I want a fixed length array. Like I said in my original post, a dynamic array leads to a segfault when I try to read lines from a file into it and then copy it to another string. And your code won't create an array that is INT16_MAX characters wide. All it does is allocate INT16_MAX bytes of memory, when, since this is a pointer we're talking about here, you only need 1.
Life's too short to be cool. Be a nerd.

#6
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
It is a fixed length array of however many bytes long INT16_MAX is on your system dynamic just means it was allocated dynamically. You are allocating a rather large chunk of memory so I guess the possibility of writing to an area of memory that the O/S doesn't want you to write to could be a problem eh. But just reading in a file and loading it into that array and then copying it? Where are you copying it to though? So you have another string that's allocated big enough to be able to hold potentially sizeof(char)*INT16_MAX long?
Also if you read in from a file exactly INT16_MAX bytes you'll overwrite the last byte since the function you're using to read from the file is probably taking in newline characters and whatever else depending on the O/S and which function you're using.
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#7
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Wait, actually, I just tried that code and it does work. I just thought it wouldn't because I tried using a similar piece of code for another program and gcc puked. Thanks for your help. :)
Life's too short to be cool. Be a nerd.

#8
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
np :o running on two days without sleep and now I shall crash!
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#9
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
The program never segfaults on me when I allocate that much memory. What I do is I read a line from a file into that string using strcpy( str, INT16_MAX, fp ), which reads characters until either the maximum is reached or a newline is reached. The latter almost invariably happens within the first few hundred characters, so I'm not actually copying the entire INT16_MAX bytes. Since I free the array immediately after copying it (which was what I was having a problem with), I am only allocating one INT16_MAX byte chunk of memory at a time.
Life's too short to be cool. Be a nerd.

#10
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
You should probably check to make sure the memory was allocated correctly. An example:
char *ptr = malloc(sizeof(char) * INT16_MAX);
if (ptr==NULL) {
    printf("Error allocating memory\n");
    return 1;
}
This is almost never a problem on computers today (which have plenty of memory), but I always add it to my code anyway.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users