Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2012
  • Last Modified:

C char array size limitation

What is the size limitation on a C char array? Does it depend on the compiler? I'm using the Ubuntu console g++ compiler. Is there some standard that defines the minimum max size a compiler must allow?
0
ehensens
Asked:
ehensens
  • 4
  • 3
1 Solution
 
Infinity08Commented:
There is no such limitation imposed by the standard.

It basically depends on the amount of memory you have available for use for the array.


There is however a limit on the length of a string literal - that can be maximum 4095 characters long.
0
 
tfalasCommented:
A limitation is imposed by each compiler on the data type used to represent the index (array subscript). For a 16-bit subscript you have 65536 locations, for a 32-bit subscript you have approximately 4.3 billion locations.
0
 
ehensensAuthor Commented:
-- I'm increasing the point value --

Silly question, but how do I know which subscript (16-bit or 32-bit) I have in my case? If I have a 16-bit subscript, can I change it to a 32-bit? I need to have a char array larger than 65536 characters.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Infinity08Commented:
It's not a limitation with the subscript - it's a limitation of the addressable memory. You cannot change that, as it depends on the hardware.

In any case, for an array, the size you can allocate will likely be far lower than the addressable memory size, since you're allocating it on the stack, which usually has a limited size of its own.


Maybe you can explain why you need this information, and we can give you a more specific example ?
0
 
ehensensAuthor Commented:
-- I am again increasing the point value to 500 --

Sure, I will more than happy to provide my specific needs:

I have a C client that sends an HTTP post request and I need to send huge payloads (much bigger than 65kB). When I send about 60kB or less the process goes through painlessly and I get an HTTP 400 OK response back, but when I make the payload greater than around 60kB or so it doesn't go through all the way and I get an HTTP 200 Bad Request back.

I am using a char array to contain all the post information. Please see the attached code snippet, the char array in question is "sendline"

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
 
#define SA struct 	sockaddr
#define MAXLINE   	4096
#define MAXSUB    	200
#define LISTENQ   	1024
 
/* Define default values */
#define defHOST      "10.2.2.61"
#define defDIRECTORY "/Service/SaveString"
#define defPORT      8004
 
extern int h_errno;
 
ssize_t processHttp(int sockfd, char directory[], char host[], char xmlString[])
{
 
	/* Encode the string for proper sending format */ 
	for (int i = 0; i < strlen(xmlString); i++)
		if (xmlString[i] == '\n')
			xmlString[i] = '*';
 
	char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
	ssize_t n;
 
	snprintf(sendline, MAXSUB,
 
		/* Actual HTTP POST text */
		"POST %s HTTP/1.0\r\n"
		"Host: %s\r\n"
		"Content-type: text/plain\r\n"
		"Content-length: %d\r\n\r\n"
		"%s", directory, host, strlen(xmlString), xmlString);
 
	write(sockfd, sendline, strlen(sendline));
	
	/* Optional: Exit program before receiving HTTP Response */
	//exit(0);
 
	/* Receive HTTP Response */
	printf("\n\nPOST Payload Expected File Size: %d bytes\n\n", (strlen(xmlString) + 2));
	printf("Waiting for Server Response...\n\n");
	while ((n = read(sockfd, recvline, MAXLINE)) > 0)
	{
		recvline[n] = '\0';
		printf("%s", recvline);
	}
	return n;
}
 
int main(void)
{
	int sockfd;
	struct sockaddr_in servaddr;
	char **pptr;
	char str[50];
	struct hostent *hptr;
 
	/* On first run, define the values as default values */
	char host[] = defHOST;
	char directory[] = defDIRECTORY;
	int port = defPORT;
 
	/* userChange bool is true if the user decides to change the default values */
	bool userChange = false;
 
	/* Define the string to be sent */	
	char xmlString[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>Twice more time...</xml>";
 
	printf("\nHTTP POST Payload\n");
	printf("\nDefault Target URI: http://%s:%d%s\n\n1 -             Change\nAny Other Key - Execute\n", host, port, directory);
 
	switch (getchar())
	{
		case '1':	userChange = true;
						break;
		default:		break;
	}
 
	if (userChange == true)
	{
		printf("\nIPv4:      ");
		scanf("%s", host);
		printf("Port:      ");
		scanf("%d", &port);
		printf("Directory: ");
		scanf("%s", directory);
		printf("\nNew Target URI: http://%s:%d%s\n\n", host, port, directory);
	}
	
	hptr = gethostbyname(host);
	if (hptr == NULL)
	{
		fprintf(stderr, " gethostbyname error for host: %s: %s", host, hstrerror(h_errno));
		exit(1);
	}
 
	printf("hostname: %s\n", hptr->h_name);
 
	if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL)
		printf("address:  %s", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
	else
		fprintf(stderr, "Error call inet_ntop\n");
 
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	inet_pton(AF_INET, str, &servaddr.sin_addr);
	connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
	processHttp(sockfd, directory, host, xmlString);
	close(sockfd);
	exit(0);
}

Open in new window

0
 
Infinity08Commented:
>> I am using a char array to contain all the post information.

First of all, I suggest allocating that array dynamically.



A few comments about your code (specifically related to char arrays) :

>>         char host[] = defHOST;
>>         char directory[] = defDIRECTORY;

This is dangerous, since you give the user the possibility to enter new values for these two. The size of the host and directory arrays will be limited to the sizes of the defHOST and defDIRECTORY string literals respectively (9 and 19 characters resp.). So, if the user chooses to enter his own values, and one of these values is longer than the mentioned limit, you have a buffer overflow, with all bad consequences that come with that.


>>         char sendline[MAXLINE + 1], recvline[MAXLINE + 1];

You limit the size of these arrays to MAXLINE characters. Make sure that that's sufficient, and that you never write past the boundaries of these arrays.
In your code, it looks fine, but you were speaking about much larger payloads than the one in your code sample, so, it's worth to check it.


>>         char xmlString[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>Twice more time...</xml>";

I assume that for testing with larger payloads, you simply assign a longer string here ?
Keep in mind what I said earlier about the limitation on string literal length.
0
 
ehensensAuthor Commented:
Yes I kind of posted a bad example, sorry. For my later versions of this code the user can upload the xml file to send directly and it takes the entire character contents of the file and saves it to a char array of its own.

What do you mean allocate that array directly?
0
 
Infinity08Commented:
>> For my later versions of this code the user can upload the xml file to send directly and it takes the entire character contents of the file and saves it to a char array of its own.

Make sure that your buffer is big enough to hold the whole file. The same goes for your send buffer.



>> What do you mean allocate that array directly?

Dynamically.

It means allocating the array on the heap rather than on the stack. The heap usually has a lot more room than the stack.

In C, you use malloc (or calloc) to allocate a block of memory of a given size, and free to de-allocate it :

        http://www.cplusplus.com/reference/clibrary/cstdlib/malloc.html
        http://www.cplusplus.com/reference/clibrary/cstdlib/calloc.html
        http://www.cplusplus.com/reference/clibrary/cstdlib/free.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now