4 Replies - 773 Views - Last Post: 27 June 2009 - 10:24 PM Rate Topic: -----

#1 larry_lalonde  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 24-March 09

[C]Question about dynamically allocating an array of ints

Post icon  Posted 26 June 2009 - 01:17 PM

So I wrote a program that accepts command line arguments and then reverses them. Nothing special.
I have this bit of code, which I created to store the length of each argument in a dynamically created array:
int *inputStrLen;
inputStrLen = malloc(argc * sizeof(int));


I am just wondering if I am misunderstanding how this works. Does it actually create a new array?

The program works perfectly as far as I can tell. And I ran tests with code like this to display the length of each argument, and it works fine.

// this is what would be entered in command prompt
c:\blahblah\programs\> stringrev brian no

// this is the code to test them
printf("Length: %d ", inputStrLen[1]);
printf("Length: %d ", inputStrLen[2]);

// this is the ouput
Length: 5 Length: 2



Sorry if this is a dumb question, but I'm only familiar really with the "new" operator in C++ with which one may unambiguously create a new array.

Is This A Good Question/Topic? 0
  • +

Replies To: [C]Question about dynamically allocating an array of ints

#2 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

Re: [C]Question about dynamically allocating an array of ints

Posted 26 June 2009 - 01:27 PM

It does not actually create an array what it does is returns a pointer to the added memory. Here check this out it's pretty informative.
http://en.wikipedia.org/wiki/Malloc
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: [C]Question about dynamically allocating an array of ints

Posted 26 June 2009 - 01:27 PM

It sets aside enough room in memory to hold argc contiguous integers and returns a pointer to the first element, which you can then access as if it was an array. Close enough for government work :)
Was This Post Helpful? 0
  • +
  • -

#4 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: [C]Question about dynamically allocating an array of ints

Posted 26 June 2009 - 01:28 PM

C or C++?

malloc I think is from C, correct me if I am mistaken.

If your looking on how to create and manage dynamic data in C++ I can help, but if you are looking for C help hopefully someone else knows.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: [C]Question about dynamically allocating an array of ints

Posted 27 June 2009 - 10:24 PM

	int *inputStrLen;

	inputStrLen = malloc((argc-1) * sizeof(int));


first element is the program name (with its path often)
it will allocate an array of ints with (argc-1) elements and address of this space will assign to pointer inputStrLen
then
free(inputStrLen);

	int *ip;
	
	ip = new int [5];
	ip[0] = 1;
	ip[1] = 2;
	ip[2] = 3;
	cout << ip[0]
		 << ip[1]
		 << ip[2]
		 << endl;
	delete [] ip;



I tryed delete ip; it was ok too

This post has been edited by c.user: 27 June 2009 - 10:26 PM

Was This Post Helpful? 0

Page 1 of 1