Thread: pointer exception???

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    pointer exception???

    Hi
    I tried malloc a big chunk of memory and use one pointer and tried with a secondary
    buffer2=buffer+numberofelement;
    *buffer2 = result;
    I got a pointer exception inside this loop,despite I have malloc several megabytes
    so I went back what I am used to,declare a huge buffer[] and use buffer[numberofelement]
    but I want to learn to get dynamic allocation right
    you tell me you can C,why dont you C your own bugs?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Show some code.

    Did you forget to multiply by sizeof(type) when you called malloc?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    214
    Are you saying you tried realloc?

    Show the code.

  4. #4
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by Salem View Post
    Show some code.

    Did you forget to multiply by sizeof(type) when you called malloc?
    Realize problem is allocate 20000000000 ints are too much,its not bytes
    Need to add IF to check malloc fails
    Pointer math needs to use unsigned ints only?
    you tell me you can C,why dont you C your own bugs?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by I C everything
    Pointer math needs to use unsigned ints only?
    No, you can use negative integers, e.g., to traverse an array in reverse, but you should keep in mind that the valid pointers are those that point to the first element to one past the last element inclusive, and that you may only dereference valid pointers except the one past the end pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks laserlight,probably good if you code that way if its on purpose

    ok I post my messy looking code so far,with working code with buffer3 and with not working is buffer and buffer2,
    first need to figure out to make it work,try floats later
    Code:
    #include "pch.h"
    #include <iostream>
    using namespace std;
    int maxram;
    int *buffer;
    int *buffer2;
    int buffer3[1024*1048];
    float a, b, c, d, e, f, g, h;
    float r, r2, r3, r4;
    int i, j, k, l,composite;
    
    
    int main()
    {
        maxram = 0x2000000000;
        buffer=(int*)malloc(maxram);
        memset(buffer, -1, maxram);
        
    
    
        cout << "Primes and composites !\n";
         
        for (i = 0; i < 1024000; i++) { buffer3[i] = 0;
        }
        
        for (i = 2; i < 1023;i=i+1 ) {
            for (j = 2; j < 1023; j=j+1) {
                composite = i * j;
                buffer2 = buffer + composite;
                buffer3[composite] = composite;
                //*buffer2 = composite;
            }
        }
        cout << "composites and primes:";
        
            for (k = 2; k < 121; k=k+1) {
                i = buffer3[k];
                j = k;
                if (i == 0) { 
                    cout << "            prime " << j << "\n";
                }
                else cout << "composite " << i << "\n";
            }
        
        free(buffer);
        system("PAUSE");
    
    
    }
    121 in the last loop is on purpose temporary easier to check smaller printout
    I have tried with optimizations in i,j forloop to step thru all odd numbers,but that breaks code so it prints lots of nonprimes as primes
    Last edited by I C everything; 06-10-2019 at 07:19 AM.
    you tell me you can C,why dont you C your own bugs?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    That is C++ and you are on the C forum

  8. #8
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by Click_here View Post
    That is C++ and you are on the C forum
    is that why IDE shows malloc,free,system,memset in pink,those are C code,C++ uses new,create,delete?
    you tell me you can C,why dont you C your own bugs?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What makes you think that you can't use them in C++?

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    All this is exclusively C++
    Code:
    #include <iostream> 
    using namespace std;
    
    ... 
    // All cout
    cout << "Primes and composites !\n";

  11. #11
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Can I access mellowed memory just like I want?
    For example mix handle 32bit sized pixels,it's,with unsigned chars when handle a color channel separately,for example blue in argb format?

    Or long long 64 bitcombined with 32 bit into?
    Last edited by I C everything; 06-14-2019 at 01:21 PM.
    you tell me you can C,why dont you C your own bugs?

  12. #12
    Registered User
    Join Date
    May 2019
    Posts
    214
    @I C everything, sorry but I couldn't quite understand what you are saying in that post.

    I assume that "mellowed" memory is "malloc'd", but....

    Was "it's" supposed to be "int's"?

    If you really understand pointers, no matter how you allocate the memory, you can pretty much do anything you want as described (best as I can make out what that means), but you're taking full responsibility for ensuring that it is correct, doesn't overrun, isn't clobbering neighboring data, etc.

  13. #13
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    yes,sorry,wrote it on a device with autocorrect
    does this mean I need to have one *int,one *char, one *long pointer to the same buffer,sometimes I copy whole buffer,sometimes I want to move 32bit pixels,sometimes I want to manipulate channels,for example light up rgb or blend together
    I more understand really lowlevel pointers,rusty when it comes to C/C++
    you tell me you can C,why dont you C your own bugs?

  14. #14
    Registered User
    Join Date
    May 2019
    Posts
    214
    Pointers in C++ are exactly the same as pointers in C. They are there to use the exactly as you already know.

    Factually, the whole reason C++ was an expansion on C is so C developers could gradually integrate new features in existing development work.

    That same idea applies to the new standards as they are released, as the decisions favor NOT breaking older code.

    Do you have a specific C example to contemplate (as in C code in a C++ context)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception errors OR exception handling :S
    By cruiser in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2011, 05:30 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. std::exception and STL
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2008, 10:16 PM
  4. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread