pointers and vectors

This is a discussion on pointers and vectors within the C Programming forums, part of the General Programming Boards category; I cant manage to sum a few vectors. Values are gigantic and only iterates about 800 times until theres no ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41

    pointers and vectors

    I cant manage to sum a few vectors. Values are gigantic and only iterates about 800 times until theres no more memory. I guess I am not handling pointers correctly, but I cant work it out. anybody see anything strange?
    Code:
    float *amp, *sum_amp;
    
    SOME SKIPPED CODE
    
    // memory allocations
    	sum_amp = (float *)calloc(nx*ny, sizeof(float));
    	if (!sum_amp) perror("Insufficient memory!");
    
    	amp = (float *)calloc(nx*ny, sizeof(float));
    	if (!amp) perror("Insufficient memory!");
    
    	
    	// for each line
    	printf("Creating mean amplitude from files...\n"); //info
    	for (ii=0;ii<kk;ii++)
    	{
    	
    		fgets(line,sizeof(line),fp);
    		// remove newline
    		if ((p = strchr(line, '\n')) != NULL) *p = '\0';
    
    		//read headerfile
    		read_header(line);
    
    		// create amplitude filename to open
    		strcpy(amp_filename, path);
    		strcat(amp_filename, "amp_");
    		strcat(amp_filename, filename);
    		strcat(amp_filename, ".bin");
    
    
    		//try fopen amp_in
    		if (!(amp_in = fopen(amp_filename, "rb")))
    		{
    			printf("\nAmplitude file not found!\n");
    			exit(0);
    		}
    		
    		// read data
    		fread( (float *)amp, sizeof(float), nx*ny, amp_in);
    		
    		// accumulate amplitudes
    		for (i=0;i<nx*ny;i++)
    		{
    			sum_amp[i] += amp[i];
    			printf("i = %d, sum_amp = %f\n",i, sum_amp[i]); //trace
    		}
    
    		fclose(amp_in);
    
    	}//for
    	fclose(fp);
    If I put the calloc allocations inside the for loop, it seems to work (numbers dont get gigantic at least) but it doesnt solve my problem since I want sum up multiple vectors, so I dont want to reset sum_amp to 0 next iteration.

  2. #2
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    OK, managed to get it to work but I am puzzled. If I put my calloc memory allocations inside the for loop instead of before the loop it works. Like this:
    Code:
    		amp = (float *)calloc(nx*ny, sizeof(float));
    		if (!amp) perror("Insufficient memory!");
    		
    		if (ii==0)
    		{
    			sum_amp = (float *)calloc(nx*ny, sizeof(float));
    			if (!sum_amp) perror("Insufficient memory!");
    		}
    Why is this different?

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A difference is that you allocate with each iteration memory again, which isn't of course good for performance
    Code:
    fread( (float *)amp, sizeof(float), nx*ny, amp_in);
    why the casting? amp is already float *

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That doesn't make sense, and my best guess would be that you are somehow doing something else wrong in your code. I can't spot anything, but I'm sure that's what happens.

    Code:
    fread( (float *)amp, sizeof(float), nx*ny, amp_in);
    Why the cast here? It is already a float *, so the cast does absolutely nothing.


    One possibility, if you moved the calloc down to the lower part of the loop is that something in the upper part of the loop causes your amp/sum_amp variables to get clobbered, and that's what is causing the failure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Thanks for your replies! I realize now that my values nx and ny are read from the header which hasnt been read before the loop. I am tired. Thanks for the casting info aswell.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by larne View Post
    Thanks for your replies! I realize now that my values nx and ny are read from the header which hasnt been read before the loop. I am tired. Thanks for the casting info aswell.
    Ah, yes, that would also make that difference ;-)

    If you are doing this many times, you may want to free the memory in the loop as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    As a final note, you might want to consider realloc(), since you want to change the memory you have according to nx, ny.

    BUT, I understand that you want one sum_amp that adds the other amps. So what values of nx, ny are you going to use for that? You have to use the maximum values of nx, ny in order to have what you want, correct? I
    f that is true than you should allocate only ONCE amp with also the maximum values. Allocating takes time, don't do it if not necessary.

    Do you initialize sum_amp? If it is the sum then you should initialize it to 0, or random numbers automatically initialized will be given giving you gigantic results (it might be initialized at 2143244 for you).
    You can use a loop for that or memset() is even better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors using object pointers.
    By theJ89 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 11:29 PM
  2. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 07:23 PM
  3. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 12:39 PM
  4. vectors of pointers
    By Myownworstenemy in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2003, 09:46 AM
  5. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2003, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21